Add LM32 port.
[binutils.git] / bfd / elf-m10300.c
blobd41906ce1983645e6a6782c132675fa41f616a15
1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 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 asection * splt;
1279 splt = bfd_get_section_by_name (dynobj, ".plt");
1281 value = (splt->output_section->vma
1282 + splt->output_offset
1283 + h->plt.offset) - value;
1286 value -= (input_section->output_section->vma
1287 + input_section->output_offset);
1288 value -= offset;
1289 value += addend;
1291 bfd_put_32 (input_bfd, value, hit_data);
1292 return bfd_reloc_ok;
1294 case R_MN10300_PLT16:
1295 if (h != NULL
1296 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1297 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1298 && h->plt.offset != (bfd_vma) -1)
1300 asection * splt;
1302 splt = bfd_get_section_by_name (dynobj, ".plt");
1304 value = (splt->output_section->vma
1305 + splt->output_offset
1306 + h->plt.offset) - value;
1309 value -= (input_section->output_section->vma
1310 + input_section->output_offset);
1311 value -= offset;
1312 value += addend;
1314 if ((long) value > 0x7fff || (long) value < -0x8000)
1315 return bfd_reloc_overflow;
1317 bfd_put_16 (input_bfd, value, hit_data);
1318 return bfd_reloc_ok;
1320 case R_MN10300_GOT32:
1321 case R_MN10300_GOT24:
1322 case R_MN10300_GOT16:
1324 asection * sgot;
1326 sgot = bfd_get_section_by_name (dynobj, ".got");
1328 if (h != NULL)
1330 bfd_vma off;
1332 off = h->got.offset;
1333 BFD_ASSERT (off != (bfd_vma) -1);
1335 if (! elf_hash_table (info)->dynamic_sections_created
1336 || SYMBOL_REFERENCES_LOCAL (info, h))
1337 /* This is actually a static link, or it is a
1338 -Bsymbolic link and the symbol is defined
1339 locally, or the symbol was forced to be local
1340 because of a version file. We must initialize
1341 this entry in the global offset table.
1343 When doing a dynamic link, we create a .rela.got
1344 relocation entry to initialize the value. This
1345 is done in the finish_dynamic_symbol routine. */
1346 bfd_put_32 (output_bfd, value,
1347 sgot->contents + off);
1349 value = sgot->output_offset + off;
1351 else
1353 bfd_vma off;
1355 off = elf_local_got_offsets (input_bfd)[symndx];
1357 bfd_put_32 (output_bfd, value, sgot->contents + off);
1359 if (info->shared)
1361 asection * srelgot;
1362 Elf_Internal_Rela outrel;
1364 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1365 BFD_ASSERT (srelgot != NULL);
1367 outrel.r_offset = (sgot->output_section->vma
1368 + sgot->output_offset
1369 + off);
1370 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1371 outrel.r_addend = value;
1372 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1373 (bfd_byte *) (((Elf32_External_Rela *)
1374 srelgot->contents)
1375 + srelgot->reloc_count));
1376 ++ srelgot->reloc_count;
1379 value = sgot->output_offset + off;
1383 value += addend;
1385 if (r_type == R_MN10300_GOT32)
1387 bfd_put_32 (input_bfd, value, hit_data);
1388 return bfd_reloc_ok;
1390 else if (r_type == R_MN10300_GOT24)
1392 if ((long) value > 0x7fffff || (long) value < -0x800000)
1393 return bfd_reloc_overflow;
1395 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1396 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1397 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1398 return bfd_reloc_ok;
1400 else if (r_type == R_MN10300_GOT16)
1402 if ((long) value > 0x7fff || (long) value < -0x8000)
1403 return bfd_reloc_overflow;
1405 bfd_put_16 (input_bfd, value, hit_data);
1406 return bfd_reloc_ok;
1408 /* Fall through. */
1410 default:
1411 return bfd_reloc_notsupported;
1415 /* Relocate an MN10300 ELF section. */
1417 static bfd_boolean
1418 mn10300_elf_relocate_section (bfd *output_bfd,
1419 struct bfd_link_info *info,
1420 bfd *input_bfd,
1421 asection *input_section,
1422 bfd_byte *contents,
1423 Elf_Internal_Rela *relocs,
1424 Elf_Internal_Sym *local_syms,
1425 asection **local_sections)
1427 Elf_Internal_Shdr *symtab_hdr;
1428 struct elf_link_hash_entry **sym_hashes;
1429 Elf_Internal_Rela *rel, *relend;
1431 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1432 sym_hashes = elf_sym_hashes (input_bfd);
1434 rel = relocs;
1435 relend = relocs + input_section->reloc_count;
1436 for (; rel < relend; rel++)
1438 int r_type;
1439 reloc_howto_type *howto;
1440 unsigned long r_symndx;
1441 Elf_Internal_Sym *sym;
1442 asection *sec;
1443 struct elf32_mn10300_link_hash_entry *h;
1444 bfd_vma relocation;
1445 bfd_reloc_status_type r;
1447 r_symndx = ELF32_R_SYM (rel->r_info);
1448 r_type = ELF32_R_TYPE (rel->r_info);
1449 howto = elf_mn10300_howto_table + r_type;
1451 /* Just skip the vtable gc relocs. */
1452 if (r_type == R_MN10300_GNU_VTINHERIT
1453 || r_type == R_MN10300_GNU_VTENTRY)
1454 continue;
1456 h = NULL;
1457 sym = NULL;
1458 sec = NULL;
1459 if (r_symndx < symtab_hdr->sh_info)
1461 sym = local_syms + r_symndx;
1462 sec = local_sections[r_symndx];
1463 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1465 else
1467 bfd_boolean unresolved_reloc;
1468 bfd_boolean warned;
1469 struct elf_link_hash_entry *hh;
1471 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1472 r_symndx, symtab_hdr, sym_hashes,
1473 hh, sec, relocation,
1474 unresolved_reloc, warned);
1476 h = (struct elf32_mn10300_link_hash_entry *) hh;
1478 if ((h->root.root.type == bfd_link_hash_defined
1479 || h->root.root.type == bfd_link_hash_defweak)
1480 && ( r_type == R_MN10300_GOTPC32
1481 || r_type == R_MN10300_GOTPC16
1482 || (( r_type == R_MN10300_PLT32
1483 || r_type == R_MN10300_PLT16)
1484 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
1485 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
1486 && h->root.plt.offset != (bfd_vma) -1)
1487 || (( r_type == R_MN10300_GOT32
1488 || r_type == R_MN10300_GOT24
1489 || r_type == R_MN10300_GOT16)
1490 && elf_hash_table (info)->dynamic_sections_created
1491 && !SYMBOL_REFERENCES_LOCAL (info, hh))
1492 || (r_type == R_MN10300_32
1493 /* _32 relocs in executables force _COPY relocs,
1494 such that the address of the symbol ends up
1495 being local. */
1496 && !info->executable
1497 && !SYMBOL_REFERENCES_LOCAL (info, hh)
1498 && ((input_section->flags & SEC_ALLOC) != 0
1499 /* DWARF will emit R_MN10300_32 relocations
1500 in its sections against symbols defined
1501 externally in shared libraries. We can't
1502 do anything with them here. */
1503 || ((input_section->flags & SEC_DEBUGGING) != 0
1504 && h->root.def_dynamic)))))
1505 /* In these cases, we don't need the relocation
1506 value. We check specially because in some
1507 obscure cases sec->output_section will be NULL. */
1508 relocation = 0;
1510 else if (!info->relocatable && unresolved_reloc)
1511 (*_bfd_error_handler)
1512 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1513 input_bfd,
1514 input_section,
1515 (long) rel->r_offset,
1516 howto->name,
1517 h->root.root.root.string);
1520 if (sec != NULL && elf_discarded_section (sec))
1522 /* For relocs against symbols from removed linkonce sections,
1523 or sections discarded by a linker script, we just want the
1524 section contents zeroed. Avoid any special processing. */
1525 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1526 rel->r_info = 0;
1527 rel->r_addend = 0;
1528 continue;
1531 if (info->relocatable)
1532 continue;
1534 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1535 input_section,
1536 contents, rel->r_offset,
1537 relocation, rel->r_addend,
1538 (struct elf_link_hash_entry *) h,
1539 r_symndx,
1540 info, sec, h == NULL);
1542 if (r != bfd_reloc_ok)
1544 const char *name;
1545 const char *msg = NULL;
1547 if (h != NULL)
1548 name = h->root.root.root.string;
1549 else
1551 name = (bfd_elf_string_from_elf_section
1552 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1553 if (name == NULL || *name == '\0')
1554 name = bfd_section_name (input_bfd, sec);
1557 switch (r)
1559 case bfd_reloc_overflow:
1560 if (! ((*info->callbacks->reloc_overflow)
1561 (info, (h ? &h->root.root : NULL), name,
1562 howto->name, (bfd_vma) 0, input_bfd,
1563 input_section, rel->r_offset)))
1564 return FALSE;
1565 break;
1567 case bfd_reloc_undefined:
1568 if (! ((*info->callbacks->undefined_symbol)
1569 (info, name, input_bfd, input_section,
1570 rel->r_offset, TRUE)))
1571 return FALSE;
1572 break;
1574 case bfd_reloc_outofrange:
1575 msg = _("internal error: out of range error");
1576 goto common_error;
1578 case bfd_reloc_notsupported:
1579 msg = _("internal error: unsupported relocation error");
1580 goto common_error;
1582 case bfd_reloc_dangerous:
1583 if (r_type == R_MN10300_PCREL32)
1584 msg = _("error: inappropriate relocation type for shared"
1585 " library (did you forget -fpic?)");
1586 else
1587 msg = _("internal error: suspicious relocation type used"
1588 " in shared library");
1589 goto common_error;
1591 default:
1592 msg = _("internal error: unknown error");
1593 /* Fall through. */
1595 common_error:
1596 if (!((*info->callbacks->warning)
1597 (info, msg, name, input_bfd, input_section,
1598 rel->r_offset)))
1599 return FALSE;
1600 break;
1605 return TRUE;
1608 /* Finish initializing one hash table entry. */
1610 static bfd_boolean
1611 elf32_mn10300_finish_hash_table_entry (struct bfd_hash_entry *gen_entry,
1612 void * in_args)
1614 struct elf32_mn10300_link_hash_entry *entry;
1615 struct bfd_link_info *link_info = (struct bfd_link_info *) in_args;
1616 unsigned int byte_count = 0;
1618 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
1620 if (entry->root.root.type == bfd_link_hash_warning)
1621 entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
1623 /* If we already know we want to convert "call" to "calls" for calls
1624 to this symbol, then return now. */
1625 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
1626 return TRUE;
1628 /* If there are no named calls to this symbol, or there's nothing we
1629 can move from the function itself into the "call" instruction,
1630 then note that all "call" instructions should be converted into
1631 "calls" instructions and return. If a symbol is available for
1632 dynamic symbol resolution (overridable or overriding), avoid
1633 custom calling conventions. */
1634 if (entry->direct_calls == 0
1635 || (entry->stack_size == 0 && entry->movm_args == 0)
1636 || (elf_hash_table (link_info)->dynamic_sections_created
1637 && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
1638 && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
1640 /* Make a note that we should convert "call" instructions to "calls"
1641 instructions for calls to this symbol. */
1642 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1643 return TRUE;
1646 /* We may be able to move some instructions from the function itself into
1647 the "call" instruction. Count how many bytes we might be able to
1648 eliminate in the function itself. */
1650 /* A movm instruction is two bytes. */
1651 if (entry->movm_args)
1652 byte_count += 2;
1654 /* Count the insn to allocate stack space too. */
1655 if (entry->stack_size > 0)
1657 if (entry->stack_size <= 128)
1658 byte_count += 3;
1659 else
1660 byte_count += 4;
1663 /* If using "call" will result in larger code, then turn all
1664 the associated "call" instructions into "calls" instructions. */
1665 if (byte_count < entry->direct_calls)
1666 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1668 /* This routine never fails. */
1669 return TRUE;
1672 /* Used to count hash table entries. */
1674 static bfd_boolean
1675 elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry *gen_entry ATTRIBUTE_UNUSED,
1676 void * in_args)
1678 int *count = (int *) in_args;
1680 (*count) ++;
1681 return TRUE;
1684 /* Used to enumerate hash table entries into a linear array. */
1686 static bfd_boolean
1687 elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry *gen_entry,
1688 void * in_args)
1690 struct bfd_hash_entry ***ptr = (struct bfd_hash_entry ***) in_args;
1692 **ptr = gen_entry;
1693 (*ptr) ++;
1694 return TRUE;
1697 /* Used to sort the array created by the above. */
1699 static int
1700 sort_by_value (const void *va, const void *vb)
1702 struct elf32_mn10300_link_hash_entry *a
1703 = *(struct elf32_mn10300_link_hash_entry **) va;
1704 struct elf32_mn10300_link_hash_entry *b
1705 = *(struct elf32_mn10300_link_hash_entry **) vb;
1707 return a->value - b->value;
1710 /* Compute the stack size and movm arguments for the function
1711 referred to by HASH at address ADDR in section with
1712 contents CONTENTS, store the information in the hash table. */
1714 static void
1715 compute_function_info (bfd *abfd,
1716 struct elf32_mn10300_link_hash_entry *hash,
1717 bfd_vma addr,
1718 unsigned char *contents)
1720 unsigned char byte1, byte2;
1721 /* We only care about a very small subset of the possible prologue
1722 sequences here. Basically we look for:
1724 movm [d2,d3,a2,a3],sp (optional)
1725 add <size>,sp (optional, and only for sizes which fit in an unsigned
1726 8 bit number)
1728 If we find anything else, we quit. */
1730 /* Look for movm [regs],sp. */
1731 byte1 = bfd_get_8 (abfd, contents + addr);
1732 byte2 = bfd_get_8 (abfd, contents + addr + 1);
1734 if (byte1 == 0xcf)
1736 hash->movm_args = byte2;
1737 addr += 2;
1738 byte1 = bfd_get_8 (abfd, contents + addr);
1739 byte2 = bfd_get_8 (abfd, contents + addr + 1);
1742 /* Now figure out how much stack space will be allocated by the movm
1743 instruction. We need this kept separate from the function's normal
1744 stack space. */
1745 if (hash->movm_args)
1747 /* Space for d2. */
1748 if (hash->movm_args & 0x80)
1749 hash->movm_stack_size += 4;
1751 /* Space for d3. */
1752 if (hash->movm_args & 0x40)
1753 hash->movm_stack_size += 4;
1755 /* Space for a2. */
1756 if (hash->movm_args & 0x20)
1757 hash->movm_stack_size += 4;
1759 /* Space for a3. */
1760 if (hash->movm_args & 0x10)
1761 hash->movm_stack_size += 4;
1763 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
1764 if (hash->movm_args & 0x08)
1765 hash->movm_stack_size += 8 * 4;
1767 if (bfd_get_mach (abfd) == bfd_mach_am33
1768 || bfd_get_mach (abfd) == bfd_mach_am33_2)
1770 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
1771 if (hash->movm_args & 0x1)
1772 hash->movm_stack_size += 6 * 4;
1774 /* exreg1 space. e4, e5, e6, e7 */
1775 if (hash->movm_args & 0x2)
1776 hash->movm_stack_size += 4 * 4;
1778 /* exreg0 space. e2, e3 */
1779 if (hash->movm_args & 0x4)
1780 hash->movm_stack_size += 2 * 4;
1784 /* Now look for the two stack adjustment variants. */
1785 if (byte1 == 0xf8 && byte2 == 0xfe)
1787 int temp = bfd_get_8 (abfd, contents + addr + 2);
1788 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
1790 hash->stack_size = -temp;
1792 else if (byte1 == 0xfa && byte2 == 0xfe)
1794 int temp = bfd_get_16 (abfd, contents + addr + 2);
1795 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
1796 temp = -temp;
1798 if (temp < 255)
1799 hash->stack_size = temp;
1802 /* If the total stack to be allocated by the call instruction is more
1803 than 255 bytes, then we can't remove the stack adjustment by using
1804 "call" (we might still be able to remove the "movm" instruction. */
1805 if (hash->stack_size + hash->movm_stack_size > 255)
1806 hash->stack_size = 0;
1809 /* Delete some bytes from a section while relaxing. */
1811 static bfd_boolean
1812 mn10300_elf_relax_delete_bytes (bfd *abfd,
1813 asection *sec,
1814 bfd_vma addr,
1815 int count)
1817 Elf_Internal_Shdr *symtab_hdr;
1818 unsigned int sec_shndx;
1819 bfd_byte *contents;
1820 Elf_Internal_Rela *irel, *irelend;
1821 Elf_Internal_Rela *irelalign;
1822 bfd_vma toaddr;
1823 Elf_Internal_Sym *isym, *isymend;
1824 struct elf_link_hash_entry **sym_hashes;
1825 struct elf_link_hash_entry **end_hashes;
1826 unsigned int symcount;
1828 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1830 contents = elf_section_data (sec)->this_hdr.contents;
1832 irelalign = NULL;
1833 toaddr = sec->size;
1835 irel = elf_section_data (sec)->relocs;
1836 irelend = irel + sec->reloc_count;
1838 if (sec->reloc_count > 0)
1840 /* If there is an align reloc at the end of the section ignore it.
1841 GAS creates these relocs for reasons of its own, and they just
1842 serve to keep the section artifically inflated. */
1843 if (ELF32_R_TYPE ((irelend - 1)->r_info) == (int) R_MN10300_ALIGN)
1844 --irelend;
1846 /* The deletion must stop at the next ALIGN reloc for an aligment
1847 power larger than, or not a multiple of, the number of bytes we
1848 are deleting. */
1849 for (; irel < irelend; irel++)
1851 int alignment = 1 << irel->r_addend;
1853 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
1854 && irel->r_offset > addr
1855 && irel->r_offset < toaddr
1856 && (count < alignment
1857 || alignment % count != 0))
1859 irelalign = irel;
1860 toaddr = irel->r_offset;
1861 break;
1866 /* Actually delete the bytes. */
1867 memmove (contents + addr, contents + addr + count,
1868 (size_t) (toaddr - addr - count));
1870 /* Adjust the section's size if we are shrinking it, or else
1871 pad the bytes between the end of the shrunken region and
1872 the start of the next region with NOP codes. */
1873 if (irelalign == NULL)
1875 sec->size -= count;
1876 /* Include symbols at the end of the section, but
1877 not at the end of a sub-region of the section. */
1878 toaddr ++;
1880 else
1882 int i;
1884 #define NOP_OPCODE 0xcb
1886 for (i = 0; i < count; i ++)
1887 bfd_put_8 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
1890 /* Adjust all the relocs. */
1891 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1893 /* Get the new reloc address. */
1894 if ((irel->r_offset > addr
1895 && irel->r_offset < toaddr)
1896 || (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
1897 && irel->r_offset == toaddr))
1898 irel->r_offset -= count;
1901 /* Adjust the local symbols in the section, reducing their value
1902 by the number of bytes deleted. Note - symbols within the deleted
1903 region are moved to the address of the start of the region, which
1904 actually means that they will address the byte beyond the end of
1905 the region once the deletion has been completed. */
1906 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1907 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1908 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1910 if (isym->st_shndx == sec_shndx
1911 && isym->st_value > addr
1912 && isym->st_value < toaddr)
1914 if (isym->st_value < addr + count)
1915 isym->st_value = addr;
1916 else
1917 isym->st_value -= count;
1919 /* Adjust the function symbol's size as well. */
1920 else if (isym->st_shndx == sec_shndx
1921 && ELF_ST_TYPE (isym->st_info) == STT_FUNC
1922 && isym->st_value + isym->st_size > addr
1923 && isym->st_value + isym->st_size < toaddr)
1924 isym->st_size -= count;
1927 /* Now adjust the global symbols defined in this section. */
1928 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1929 - symtab_hdr->sh_info);
1930 sym_hashes = elf_sym_hashes (abfd);
1931 end_hashes = sym_hashes + symcount;
1932 for (; sym_hashes < end_hashes; sym_hashes++)
1934 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1936 if ((sym_hash->root.type == bfd_link_hash_defined
1937 || sym_hash->root.type == bfd_link_hash_defweak)
1938 && sym_hash->root.u.def.section == sec
1939 && sym_hash->root.u.def.value > addr
1940 && sym_hash->root.u.def.value < toaddr)
1942 if (sym_hash->root.u.def.value < addr + count)
1943 sym_hash->root.u.def.value = addr;
1944 else
1945 sym_hash->root.u.def.value -= count;
1947 /* Adjust the function symbol's size as well. */
1948 else if (sym_hash->root.type == bfd_link_hash_defined
1949 && sym_hash->root.u.def.section == sec
1950 && sym_hash->type == STT_FUNC
1951 && sym_hash->root.u.def.value + sym_hash->size > addr
1952 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1953 sym_hash->size -= count;
1956 /* See if we can move the ALIGN reloc forward.
1957 We have adjusted r_offset for it already. */
1958 if (irelalign != NULL)
1960 bfd_vma alignto, alignaddr;
1962 if ((int) irelalign->r_addend > 0)
1964 /* This is the old address. */
1965 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1966 /* This is where the align points to now. */
1967 alignaddr = BFD_ALIGN (irelalign->r_offset,
1968 1 << irelalign->r_addend);
1969 if (alignaddr < alignto)
1970 /* Tail recursion. */
1971 return mn10300_elf_relax_delete_bytes (abfd, sec, alignaddr,
1972 (int) (alignto - alignaddr));
1976 return TRUE;
1979 /* Return TRUE if a symbol exists at the given address, else return
1980 FALSE. */
1982 static bfd_boolean
1983 mn10300_elf_symbol_address_p (bfd *abfd,
1984 asection *sec,
1985 Elf_Internal_Sym *isym,
1986 bfd_vma addr)
1988 Elf_Internal_Shdr *symtab_hdr;
1989 unsigned int sec_shndx;
1990 Elf_Internal_Sym *isymend;
1991 struct elf_link_hash_entry **sym_hashes;
1992 struct elf_link_hash_entry **end_hashes;
1993 unsigned int symcount;
1995 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1997 /* Examine all the symbols. */
1998 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1999 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2000 if (isym->st_shndx == sec_shndx
2001 && isym->st_value == addr)
2002 return TRUE;
2004 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2005 - symtab_hdr->sh_info);
2006 sym_hashes = elf_sym_hashes (abfd);
2007 end_hashes = sym_hashes + symcount;
2008 for (; sym_hashes < end_hashes; sym_hashes++)
2010 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2012 if ((sym_hash->root.type == bfd_link_hash_defined
2013 || sym_hash->root.type == bfd_link_hash_defweak)
2014 && sym_hash->root.u.def.section == sec
2015 && sym_hash->root.u.def.value == addr)
2016 return TRUE;
2019 return FALSE;
2022 /* This function handles relaxing for the mn10300.
2024 There are quite a few relaxing opportunities available on the mn10300:
2026 * calls:32 -> calls:16 2 bytes
2027 * call:32 -> call:16 2 bytes
2029 * call:32 -> calls:32 1 byte
2030 * call:16 -> calls:16 1 byte
2031 * These are done anytime using "calls" would result
2032 in smaller code, or when necessary to preserve the
2033 meaning of the program.
2035 * call:32 varies
2036 * call:16
2037 * In some circumstances we can move instructions
2038 from a function prologue into a "call" instruction.
2039 This is only done if the resulting code is no larger
2040 than the original code.
2042 * jmp:32 -> jmp:16 2 bytes
2043 * jmp:16 -> bra:8 1 byte
2045 * If the previous instruction is a conditional branch
2046 around the jump/bra, we may be able to reverse its condition
2047 and change its target to the jump's target. The jump/bra
2048 can then be deleted. 2 bytes
2050 * mov abs32 -> mov abs16 1 or 2 bytes
2052 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
2053 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
2055 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
2056 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
2058 We don't handle imm16->imm8 or d16->d8 as they're very rare
2059 and somewhat more difficult to support. */
2061 static bfd_boolean
2062 mn10300_elf_relax_section (bfd *abfd,
2063 asection *sec,
2064 struct bfd_link_info *link_info,
2065 bfd_boolean *again)
2067 Elf_Internal_Shdr *symtab_hdr;
2068 Elf_Internal_Rela *internal_relocs = NULL;
2069 Elf_Internal_Rela *irel, *irelend;
2070 bfd_byte *contents = NULL;
2071 Elf_Internal_Sym *isymbuf = NULL;
2072 struct elf32_mn10300_link_hash_table *hash_table;
2073 asection *section = sec;
2074 bfd_vma align_gap_adjustment;
2076 /* Assume nothing changes. */
2077 *again = FALSE;
2079 /* We need a pointer to the mn10300 specific hash table. */
2080 hash_table = elf32_mn10300_hash_table (link_info);
2082 /* Initialize fields in each hash table entry the first time through. */
2083 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
2085 bfd *input_bfd;
2087 /* Iterate over all the input bfds. */
2088 for (input_bfd = link_info->input_bfds;
2089 input_bfd != NULL;
2090 input_bfd = input_bfd->link_next)
2092 /* We're going to need all the symbols for each bfd. */
2093 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2094 if (symtab_hdr->sh_info != 0)
2096 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2097 if (isymbuf == NULL)
2098 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2099 symtab_hdr->sh_info, 0,
2100 NULL, NULL, NULL);
2101 if (isymbuf == NULL)
2102 goto error_return;
2105 /* Iterate over each section in this bfd. */
2106 for (section = input_bfd->sections;
2107 section != NULL;
2108 section = section->next)
2110 struct elf32_mn10300_link_hash_entry *hash;
2111 Elf_Internal_Sym *sym;
2112 asection *sym_sec = NULL;
2113 const char *sym_name;
2114 char *new_name;
2116 /* If there's nothing to do in this section, skip it. */
2117 if (! ((section->flags & SEC_RELOC) != 0
2118 && section->reloc_count != 0))
2119 continue;
2120 if ((section->flags & SEC_ALLOC) == 0)
2121 continue;
2123 /* Get cached copy of section contents if it exists. */
2124 if (elf_section_data (section)->this_hdr.contents != NULL)
2125 contents = elf_section_data (section)->this_hdr.contents;
2126 else if (section->size != 0)
2128 /* Go get them off disk. */
2129 if (!bfd_malloc_and_get_section (input_bfd, section,
2130 &contents))
2131 goto error_return;
2133 else
2134 contents = NULL;
2136 /* If there aren't any relocs, then there's nothing to do. */
2137 if ((section->flags & SEC_RELOC) != 0
2138 && section->reloc_count != 0)
2140 /* Get a copy of the native relocations. */
2141 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2142 NULL, NULL,
2143 link_info->keep_memory);
2144 if (internal_relocs == NULL)
2145 goto error_return;
2147 /* Now examine each relocation. */
2148 irel = internal_relocs;
2149 irelend = irel + section->reloc_count;
2150 for (; irel < irelend; irel++)
2152 long r_type;
2153 unsigned long r_index;
2154 unsigned char code;
2156 r_type = ELF32_R_TYPE (irel->r_info);
2157 r_index = ELF32_R_SYM (irel->r_info);
2159 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
2160 goto error_return;
2162 /* We need the name and hash table entry of the target
2163 symbol! */
2164 hash = NULL;
2165 sym = NULL;
2166 sym_sec = NULL;
2168 if (r_index < symtab_hdr->sh_info)
2170 /* A local symbol. */
2171 Elf_Internal_Sym *isym;
2172 struct elf_link_hash_table *elftab;
2173 bfd_size_type amt;
2175 isym = isymbuf + r_index;
2176 if (isym->st_shndx == SHN_UNDEF)
2177 sym_sec = bfd_und_section_ptr;
2178 else if (isym->st_shndx == SHN_ABS)
2179 sym_sec = bfd_abs_section_ptr;
2180 else if (isym->st_shndx == SHN_COMMON)
2181 sym_sec = bfd_com_section_ptr;
2182 else
2183 sym_sec
2184 = bfd_section_from_elf_index (input_bfd,
2185 isym->st_shndx);
2187 sym_name
2188 = bfd_elf_string_from_elf_section (input_bfd,
2189 (symtab_hdr
2190 ->sh_link),
2191 isym->st_name);
2193 /* If it isn't a function, then we don't care
2194 about it. */
2195 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
2196 continue;
2198 /* Tack on an ID so we can uniquely identify this
2199 local symbol in the global hash table. */
2200 amt = strlen (sym_name) + 10;
2201 new_name = bfd_malloc (amt);
2202 if (new_name == NULL)
2203 goto error_return;
2205 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2206 sym_name = new_name;
2208 elftab = &hash_table->static_hash_table->root;
2209 hash = ((struct elf32_mn10300_link_hash_entry *)
2210 elf_link_hash_lookup (elftab, sym_name,
2211 TRUE, TRUE, FALSE));
2212 free (new_name);
2214 else
2216 r_index -= symtab_hdr->sh_info;
2217 hash = (struct elf32_mn10300_link_hash_entry *)
2218 elf_sym_hashes (input_bfd)[r_index];
2221 sym_name = hash->root.root.root.string;
2222 if ((section->flags & SEC_CODE) != 0)
2224 /* If this is not a "call" instruction, then we
2225 should convert "call" instructions to "calls"
2226 instructions. */
2227 code = bfd_get_8 (input_bfd,
2228 contents + irel->r_offset - 1);
2229 if (code != 0xdd && code != 0xcd)
2230 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2233 /* If this is a jump/call, then bump the
2234 direct_calls counter. Else force "call" to
2235 "calls" conversions. */
2236 if (r_type == R_MN10300_PCREL32
2237 || r_type == R_MN10300_PLT32
2238 || r_type == R_MN10300_PLT16
2239 || r_type == R_MN10300_PCREL16)
2240 hash->direct_calls++;
2241 else
2242 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2246 /* Now look at the actual contents to get the stack size,
2247 and a list of what registers were saved in the prologue
2248 (ie movm_args). */
2249 if ((section->flags & SEC_CODE) != 0)
2251 Elf_Internal_Sym *isym, *isymend;
2252 unsigned int sec_shndx;
2253 struct elf_link_hash_entry **hashes;
2254 struct elf_link_hash_entry **end_hashes;
2255 unsigned int symcount;
2257 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2258 section);
2260 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2261 - symtab_hdr->sh_info);
2262 hashes = elf_sym_hashes (input_bfd);
2263 end_hashes = hashes + symcount;
2265 /* Look at each function defined in this section and
2266 update info for that function. */
2267 isymend = isymbuf + symtab_hdr->sh_info;
2268 for (isym = isymbuf; isym < isymend; isym++)
2270 if (isym->st_shndx == sec_shndx
2271 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
2273 struct elf_link_hash_table *elftab;
2274 bfd_size_type amt;
2275 struct elf_link_hash_entry **lhashes = hashes;
2277 /* Skip a local symbol if it aliases a
2278 global one. */
2279 for (; lhashes < end_hashes; lhashes++)
2281 hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
2282 if ((hash->root.root.type == bfd_link_hash_defined
2283 || hash->root.root.type == bfd_link_hash_defweak)
2284 && hash->root.root.u.def.section == section
2285 && hash->root.type == STT_FUNC
2286 && hash->root.root.u.def.value == isym->st_value)
2287 break;
2289 if (lhashes != end_hashes)
2290 continue;
2292 if (isym->st_shndx == SHN_UNDEF)
2293 sym_sec = bfd_und_section_ptr;
2294 else if (isym->st_shndx == SHN_ABS)
2295 sym_sec = bfd_abs_section_ptr;
2296 else if (isym->st_shndx == SHN_COMMON)
2297 sym_sec = bfd_com_section_ptr;
2298 else
2299 sym_sec
2300 = bfd_section_from_elf_index (input_bfd,
2301 isym->st_shndx);
2303 sym_name = (bfd_elf_string_from_elf_section
2304 (input_bfd, symtab_hdr->sh_link,
2305 isym->st_name));
2307 /* Tack on an ID so we can uniquely identify this
2308 local symbol in the global hash table. */
2309 amt = strlen (sym_name) + 10;
2310 new_name = bfd_malloc (amt);
2311 if (new_name == NULL)
2312 goto error_return;
2314 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2315 sym_name = new_name;
2317 elftab = &hash_table->static_hash_table->root;
2318 hash = ((struct elf32_mn10300_link_hash_entry *)
2319 elf_link_hash_lookup (elftab, sym_name,
2320 TRUE, TRUE, FALSE));
2321 free (new_name);
2322 compute_function_info (input_bfd, hash,
2323 isym->st_value, contents);
2324 hash->value = isym->st_value;
2328 for (; hashes < end_hashes; hashes++)
2330 hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2331 if ((hash->root.root.type == bfd_link_hash_defined
2332 || hash->root.root.type == bfd_link_hash_defweak)
2333 && hash->root.root.u.def.section == section
2334 && hash->root.type == STT_FUNC)
2335 compute_function_info (input_bfd, hash,
2336 (hash)->root.root.u.def.value,
2337 contents);
2341 /* Cache or free any memory we allocated for the relocs. */
2342 if (internal_relocs != NULL
2343 && elf_section_data (section)->relocs != internal_relocs)
2344 free (internal_relocs);
2345 internal_relocs = NULL;
2347 /* Cache or free any memory we allocated for the contents. */
2348 if (contents != NULL
2349 && elf_section_data (section)->this_hdr.contents != contents)
2351 if (! link_info->keep_memory)
2352 free (contents);
2353 else
2355 /* Cache the section contents for elf_link_input_bfd. */
2356 elf_section_data (section)->this_hdr.contents = contents;
2359 contents = NULL;
2362 /* Cache or free any memory we allocated for the symbols. */
2363 if (isymbuf != NULL
2364 && symtab_hdr->contents != (unsigned char *) isymbuf)
2366 if (! link_info->keep_memory)
2367 free (isymbuf);
2368 else
2370 /* Cache the symbols for elf_link_input_bfd. */
2371 symtab_hdr->contents = (unsigned char *) isymbuf;
2374 isymbuf = NULL;
2377 /* Now iterate on each symbol in the hash table and perform
2378 the final initialization steps on each. */
2379 elf32_mn10300_link_hash_traverse (hash_table,
2380 elf32_mn10300_finish_hash_table_entry,
2381 link_info);
2382 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2383 elf32_mn10300_finish_hash_table_entry,
2384 link_info);
2387 /* This section of code collects all our local symbols, sorts
2388 them by value, and looks for multiple symbols referring to
2389 the same address. For those symbols, the flags are merged.
2390 At this point, the only flag that can be set is
2391 MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
2392 together. */
2393 int static_count = 0, i;
2394 struct elf32_mn10300_link_hash_entry **entries;
2395 struct elf32_mn10300_link_hash_entry **ptr;
2397 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2398 elf32_mn10300_count_hash_table_entries,
2399 &static_count);
2401 entries = bfd_malloc (static_count * sizeof (* ptr));
2403 ptr = entries;
2404 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2405 elf32_mn10300_list_hash_table_entries,
2406 & ptr);
2408 qsort (entries, static_count, sizeof (entries[0]), sort_by_value);
2410 for (i = 0; i < static_count - 1; i++)
2411 if (entries[i]->value && entries[i]->value == entries[i+1]->value)
2413 int v = entries[i]->flags;
2414 int j;
2416 for (j = i + 1; j < static_count && entries[j]->value == entries[i]->value; j++)
2417 v |= entries[j]->flags;
2419 for (j = i; j < static_count && entries[j]->value == entries[i]->value; j++)
2420 entries[j]->flags = v;
2422 i = j - 1;
2426 /* All entries in the hash table are fully initialized. */
2427 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
2429 /* Now that everything has been initialized, go through each
2430 code section and delete any prologue insns which will be
2431 redundant because their operations will be performed by
2432 a "call" instruction. */
2433 for (input_bfd = link_info->input_bfds;
2434 input_bfd != NULL;
2435 input_bfd = input_bfd->link_next)
2437 /* We're going to need all the local symbols for each bfd. */
2438 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2439 if (symtab_hdr->sh_info != 0)
2441 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2442 if (isymbuf == NULL)
2443 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2444 symtab_hdr->sh_info, 0,
2445 NULL, NULL, NULL);
2446 if (isymbuf == NULL)
2447 goto error_return;
2450 /* Walk over each section in this bfd. */
2451 for (section = input_bfd->sections;
2452 section != NULL;
2453 section = section->next)
2455 unsigned int sec_shndx;
2456 Elf_Internal_Sym *isym, *isymend;
2457 struct elf_link_hash_entry **hashes;
2458 struct elf_link_hash_entry **end_hashes;
2459 unsigned int symcount;
2461 /* Skip non-code sections and empty sections. */
2462 if ((section->flags & SEC_CODE) == 0 || section->size == 0)
2463 continue;
2465 if (section->reloc_count != 0)
2467 /* Get a copy of the native relocations. */
2468 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2469 NULL, NULL,
2470 link_info->keep_memory);
2471 if (internal_relocs == NULL)
2472 goto error_return;
2475 /* Get cached copy of section contents if it exists. */
2476 if (elf_section_data (section)->this_hdr.contents != NULL)
2477 contents = elf_section_data (section)->this_hdr.contents;
2478 else
2480 /* Go get them off disk. */
2481 if (!bfd_malloc_and_get_section (input_bfd, section,
2482 &contents))
2483 goto error_return;
2486 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2487 section);
2489 /* Now look for any function in this section which needs
2490 insns deleted from its prologue. */
2491 isymend = isymbuf + symtab_hdr->sh_info;
2492 for (isym = isymbuf; isym < isymend; isym++)
2494 struct elf32_mn10300_link_hash_entry *sym_hash;
2495 asection *sym_sec = NULL;
2496 const char *sym_name;
2497 char *new_name;
2498 struct elf_link_hash_table *elftab;
2499 bfd_size_type amt;
2501 if (isym->st_shndx != sec_shndx)
2502 continue;
2504 if (isym->st_shndx == SHN_UNDEF)
2505 sym_sec = bfd_und_section_ptr;
2506 else if (isym->st_shndx == SHN_ABS)
2507 sym_sec = bfd_abs_section_ptr;
2508 else if (isym->st_shndx == SHN_COMMON)
2509 sym_sec = bfd_com_section_ptr;
2510 else
2511 sym_sec
2512 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2514 sym_name
2515 = bfd_elf_string_from_elf_section (input_bfd,
2516 symtab_hdr->sh_link,
2517 isym->st_name);
2519 /* Tack on an ID so we can uniquely identify this
2520 local symbol in the global hash table. */
2521 amt = strlen (sym_name) + 10;
2522 new_name = bfd_malloc (amt);
2523 if (new_name == NULL)
2524 goto error_return;
2525 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2526 sym_name = new_name;
2528 elftab = & hash_table->static_hash_table->root;
2529 sym_hash = (struct elf32_mn10300_link_hash_entry *)
2530 elf_link_hash_lookup (elftab, sym_name,
2531 FALSE, FALSE, FALSE);
2533 free (new_name);
2534 if (sym_hash == NULL)
2535 continue;
2537 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2538 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2540 int bytes = 0;
2542 /* Note that we've changed things. */
2543 elf_section_data (section)->relocs = internal_relocs;
2544 elf_section_data (section)->this_hdr.contents = contents;
2545 symtab_hdr->contents = (unsigned char *) isymbuf;
2547 /* Count how many bytes we're going to delete. */
2548 if (sym_hash->movm_args)
2549 bytes += 2;
2551 if (sym_hash->stack_size > 0)
2553 if (sym_hash->stack_size <= 128)
2554 bytes += 3;
2555 else
2556 bytes += 4;
2559 /* Note that we've deleted prologue bytes for this
2560 function. */
2561 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2563 /* Actually delete the bytes. */
2564 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2565 section,
2566 isym->st_value,
2567 bytes))
2568 goto error_return;
2570 /* Something changed. Not strictly necessary, but
2571 may lead to more relaxing opportunities. */
2572 *again = TRUE;
2576 /* Look for any global functions in this section which
2577 need insns deleted from their prologues. */
2578 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2579 - symtab_hdr->sh_info);
2580 hashes = elf_sym_hashes (input_bfd);
2581 end_hashes = hashes + symcount;
2582 for (; hashes < end_hashes; hashes++)
2584 struct elf32_mn10300_link_hash_entry *sym_hash;
2586 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2587 if ((sym_hash->root.root.type == bfd_link_hash_defined
2588 || sym_hash->root.root.type == bfd_link_hash_defweak)
2589 && sym_hash->root.root.u.def.section == section
2590 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2591 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2593 int bytes = 0;
2594 bfd_vma symval;
2596 /* Note that we've changed things. */
2597 elf_section_data (section)->relocs = internal_relocs;
2598 elf_section_data (section)->this_hdr.contents = contents;
2599 symtab_hdr->contents = (unsigned char *) isymbuf;
2601 /* Count how many bytes we're going to delete. */
2602 if (sym_hash->movm_args)
2603 bytes += 2;
2605 if (sym_hash->stack_size > 0)
2607 if (sym_hash->stack_size <= 128)
2608 bytes += 3;
2609 else
2610 bytes += 4;
2613 /* Note that we've deleted prologue bytes for this
2614 function. */
2615 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2617 /* Actually delete the bytes. */
2618 symval = sym_hash->root.root.u.def.value;
2619 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2620 section,
2621 symval,
2622 bytes))
2623 goto error_return;
2625 /* Something changed. Not strictly necessary, but
2626 may lead to more relaxing opportunities. */
2627 *again = TRUE;
2631 /* Cache or free any memory we allocated for the relocs. */
2632 if (internal_relocs != NULL
2633 && elf_section_data (section)->relocs != internal_relocs)
2634 free (internal_relocs);
2635 internal_relocs = NULL;
2637 /* Cache or free any memory we allocated for the contents. */
2638 if (contents != NULL
2639 && elf_section_data (section)->this_hdr.contents != contents)
2641 if (! link_info->keep_memory)
2642 free (contents);
2643 else
2644 /* Cache the section contents for elf_link_input_bfd. */
2645 elf_section_data (section)->this_hdr.contents = contents;
2647 contents = NULL;
2650 /* Cache or free any memory we allocated for the symbols. */
2651 if (isymbuf != NULL
2652 && symtab_hdr->contents != (unsigned char *) isymbuf)
2654 if (! link_info->keep_memory)
2655 free (isymbuf);
2656 else
2657 /* Cache the symbols for elf_link_input_bfd. */
2658 symtab_hdr->contents = (unsigned char *) isymbuf;
2660 isymbuf = NULL;
2664 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
2665 contents = NULL;
2666 internal_relocs = NULL;
2667 isymbuf = NULL;
2668 /* For error_return. */
2669 section = sec;
2671 /* We don't have to do anything for a relocatable link, if
2672 this section does not have relocs, or if this is not a
2673 code section. */
2674 if (link_info->relocatable
2675 || (sec->flags & SEC_RELOC) == 0
2676 || sec->reloc_count == 0
2677 || (sec->flags & SEC_CODE) == 0)
2678 return TRUE;
2680 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2682 /* Get a copy of the native relocations. */
2683 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2684 link_info->keep_memory);
2685 if (internal_relocs == NULL)
2686 goto error_return;
2688 /* Scan for worst case alignment gap changes. Note that this logic
2689 is not ideal; what we should do is run this scan for every
2690 opcode/address range and adjust accordingly, but that's
2691 expensive. Worst case is that for an alignment of N bytes, we
2692 move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc
2693 all before it. Plus, this still doesn't cover cross-section
2694 jumps with section alignment. */
2695 irelend = internal_relocs + sec->reloc_count;
2696 align_gap_adjustment = 0;
2697 for (irel = internal_relocs; irel < irelend; irel++)
2699 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN)
2701 bfd_vma adj = 1 << irel->r_addend;
2702 bfd_vma aend = irel->r_offset;
2704 aend = BFD_ALIGN (aend, 1 << irel->r_addend);
2705 adj = 2 * adj - adj - 1;
2707 /* Record the biggest adjustmnet. Skip any alignment at the
2708 end of our section. */
2709 if (align_gap_adjustment < adj
2710 && aend < sec->output_section->vma + sec->output_offset + sec->size)
2711 align_gap_adjustment = adj;
2715 /* Walk through them looking for relaxing opportunities. */
2716 irelend = internal_relocs + sec->reloc_count;
2717 for (irel = internal_relocs; irel < irelend; irel++)
2719 bfd_vma symval;
2720 bfd_signed_vma jump_offset;
2721 asection *sym_sec = NULL;
2722 struct elf32_mn10300_link_hash_entry *h = NULL;
2724 /* If this isn't something that can be relaxed, then ignore
2725 this reloc. */
2726 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
2727 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
2728 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
2729 continue;
2731 /* Get the section contents if we haven't done so already. */
2732 if (contents == NULL)
2734 /* Get cached copy if it exists. */
2735 if (elf_section_data (sec)->this_hdr.contents != NULL)
2736 contents = elf_section_data (sec)->this_hdr.contents;
2737 else
2739 /* Go get them off disk. */
2740 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2741 goto error_return;
2745 /* Read this BFD's symbols if we haven't done so already. */
2746 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2748 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2749 if (isymbuf == NULL)
2750 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2751 symtab_hdr->sh_info, 0,
2752 NULL, NULL, NULL);
2753 if (isymbuf == NULL)
2754 goto error_return;
2757 /* Get the value of the symbol referred to by the reloc. */
2758 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2760 Elf_Internal_Sym *isym;
2761 const char *sym_name;
2762 char *new_name;
2764 /* A local symbol. */
2765 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2766 if (isym->st_shndx == SHN_UNDEF)
2767 sym_sec = bfd_und_section_ptr;
2768 else if (isym->st_shndx == SHN_ABS)
2769 sym_sec = bfd_abs_section_ptr;
2770 else if (isym->st_shndx == SHN_COMMON)
2771 sym_sec = bfd_com_section_ptr;
2772 else
2773 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2775 sym_name = bfd_elf_string_from_elf_section (abfd,
2776 symtab_hdr->sh_link,
2777 isym->st_name);
2779 if ((sym_sec->flags & SEC_MERGE)
2780 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
2781 && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2783 symval = isym->st_value + irel->r_addend;
2784 symval = _bfd_merged_section_offset (abfd, & sym_sec,
2785 elf_section_data (sym_sec)->sec_info,
2786 symval);
2787 symval += sym_sec->output_section->vma + sym_sec->output_offset - irel->r_addend;
2789 else
2790 symval = (isym->st_value
2791 + sym_sec->output_section->vma
2792 + sym_sec->output_offset);
2794 /* Tack on an ID so we can uniquely identify this
2795 local symbol in the global hash table. */
2796 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
2797 if (new_name == NULL)
2798 goto error_return;
2799 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2800 sym_name = new_name;
2802 h = (struct elf32_mn10300_link_hash_entry *)
2803 elf_link_hash_lookup (&hash_table->static_hash_table->root,
2804 sym_name, FALSE, FALSE, FALSE);
2805 free (new_name);
2807 else
2809 unsigned long indx;
2811 /* An external symbol. */
2812 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2813 h = (struct elf32_mn10300_link_hash_entry *)
2814 (elf_sym_hashes (abfd)[indx]);
2815 BFD_ASSERT (h != NULL);
2816 if (h->root.root.type != bfd_link_hash_defined
2817 && h->root.root.type != bfd_link_hash_defweak)
2818 /* This appears to be a reference to an undefined
2819 symbol. Just ignore it--it will be caught by the
2820 regular reloc processing. */
2821 continue;
2823 /* Check for a reference to a discarded symbol and ignore it. */
2824 if (h->root.root.u.def.section->output_section == NULL)
2825 continue;
2827 sym_sec = h->root.root.u.def.section->output_section;
2829 symval = (h->root.root.u.def.value
2830 + h->root.root.u.def.section->output_section->vma
2831 + h->root.root.u.def.section->output_offset);
2834 /* For simplicity of coding, we are going to modify the section
2835 contents, the section relocs, and the BFD symbol table. We
2836 must tell the rest of the code not to free up this
2837 information. It would be possible to instead create a table
2838 of changes which have to be made, as is done in coff-mips.c;
2839 that would be more work, but would require less memory when
2840 the linker is run. */
2842 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2843 branch/call, also deal with "call" -> "calls" conversions and
2844 insertion of prologue data into "call" instructions. */
2845 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
2846 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
2848 bfd_vma value = symval;
2850 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
2851 && h != NULL
2852 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2853 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2854 && h->root.plt.offset != (bfd_vma) -1)
2856 asection * splt;
2858 splt = bfd_get_section_by_name (elf_hash_table (link_info)
2859 ->dynobj, ".plt");
2861 value = ((splt->output_section->vma
2862 + splt->output_offset
2863 + h->root.plt.offset)
2864 - (sec->output_section->vma
2865 + sec->output_offset
2866 + irel->r_offset));
2869 /* If we've got a "call" instruction that needs to be turned
2870 into a "calls" instruction, do so now. It saves a byte. */
2871 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2873 unsigned char code;
2875 /* Get the opcode. */
2876 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2878 /* Make sure we're working with a "call" instruction! */
2879 if (code == 0xdd)
2881 /* Note that we've changed the relocs, section contents,
2882 etc. */
2883 elf_section_data (sec)->relocs = internal_relocs;
2884 elf_section_data (sec)->this_hdr.contents = contents;
2885 symtab_hdr->contents = (unsigned char *) isymbuf;
2887 /* Fix the opcode. */
2888 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
2889 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2891 /* Fix irel->r_offset and irel->r_addend. */
2892 irel->r_offset += 1;
2893 irel->r_addend += 1;
2895 /* Delete one byte of data. */
2896 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2897 irel->r_offset + 3, 1))
2898 goto error_return;
2900 /* That will change things, so, we should relax again.
2901 Note that this is not required, and it may be slow. */
2902 *again = TRUE;
2905 else if (h)
2907 /* We've got a "call" instruction which needs some data
2908 from target function filled in. */
2909 unsigned char code;
2911 /* Get the opcode. */
2912 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2914 /* Insert data from the target function into the "call"
2915 instruction if needed. */
2916 if (code == 0xdd)
2918 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
2919 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2920 contents + irel->r_offset + 5);
2924 /* Deal with pc-relative gunk. */
2925 value -= (sec->output_section->vma + sec->output_offset);
2926 value -= irel->r_offset;
2927 value += irel->r_addend;
2929 /* See if the value will fit in 16 bits, note the high value is
2930 0x7fff + 2 as the target will be two bytes closer if we are
2931 able to relax, if it's in the same section. */
2932 if (sec->output_section == sym_sec->output_section)
2933 jump_offset = 0x8001;
2934 else
2935 jump_offset = 0x7fff;
2937 /* Account for jumps across alignment boundaries using
2938 align_gap_adjustment. */
2939 if ((bfd_signed_vma) value < jump_offset - (bfd_signed_vma) align_gap_adjustment
2940 && ((bfd_signed_vma) value > -0x8000 + (bfd_signed_vma) align_gap_adjustment))
2942 unsigned char code;
2944 /* Get the opcode. */
2945 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2947 if (code != 0xdc && code != 0xdd && code != 0xff)
2948 continue;
2950 /* Note that we've changed the relocs, section contents, etc. */
2951 elf_section_data (sec)->relocs = internal_relocs;
2952 elf_section_data (sec)->this_hdr.contents = contents;
2953 symtab_hdr->contents = (unsigned char *) isymbuf;
2955 /* Fix the opcode. */
2956 if (code == 0xdc)
2957 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
2958 else if (code == 0xdd)
2959 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
2960 else if (code == 0xff)
2961 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2963 /* Fix the relocation's type. */
2964 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2965 (ELF32_R_TYPE (irel->r_info)
2966 == (int) R_MN10300_PLT32)
2967 ? R_MN10300_PLT16 :
2968 R_MN10300_PCREL16);
2970 /* Delete two bytes of data. */
2971 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2972 irel->r_offset + 1, 2))
2973 goto error_return;
2975 /* That will change things, so, we should relax again.
2976 Note that this is not required, and it may be slow. */
2977 *again = TRUE;
2981 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2982 branch. */
2983 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
2985 bfd_vma value = symval;
2987 /* If we've got a "call" instruction that needs to be turned
2988 into a "calls" instruction, do so now. It saves a byte. */
2989 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2991 unsigned char code;
2993 /* Get the opcode. */
2994 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2996 /* Make sure we're working with a "call" instruction! */
2997 if (code == 0xcd)
2999 /* Note that we've changed the relocs, section contents,
3000 etc. */
3001 elf_section_data (sec)->relocs = internal_relocs;
3002 elf_section_data (sec)->this_hdr.contents = contents;
3003 symtab_hdr->contents = (unsigned char *) isymbuf;
3005 /* Fix the opcode. */
3006 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
3007 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
3009 /* Fix irel->r_offset and irel->r_addend. */
3010 irel->r_offset += 1;
3011 irel->r_addend += 1;
3013 /* Delete one byte of data. */
3014 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3015 irel->r_offset + 1, 1))
3016 goto error_return;
3018 /* That will change things, so, we should relax again.
3019 Note that this is not required, and it may be slow. */
3020 *again = TRUE;
3023 else if (h)
3025 unsigned char code;
3027 /* Get the opcode. */
3028 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3030 /* Insert data from the target function into the "call"
3031 instruction if needed. */
3032 if (code == 0xcd)
3034 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
3035 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
3036 contents + irel->r_offset + 3);
3040 /* Deal with pc-relative gunk. */
3041 value -= (sec->output_section->vma + sec->output_offset);
3042 value -= irel->r_offset;
3043 value += irel->r_addend;
3045 /* See if the value will fit in 8 bits, note the high value is
3046 0x7f + 1 as the target will be one bytes closer if we are
3047 able to relax. */
3048 if ((long) value < 0x80 && (long) value > -0x80)
3050 unsigned char code;
3052 /* Get the opcode. */
3053 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3055 if (code != 0xcc)
3056 continue;
3058 /* Note that we've changed the relocs, section contents, etc. */
3059 elf_section_data (sec)->relocs = internal_relocs;
3060 elf_section_data (sec)->this_hdr.contents = contents;
3061 symtab_hdr->contents = (unsigned char *) isymbuf;
3063 /* Fix the opcode. */
3064 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
3066 /* Fix the relocation's type. */
3067 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3068 R_MN10300_PCREL8);
3070 /* Delete one byte of data. */
3071 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3072 irel->r_offset + 1, 1))
3073 goto error_return;
3075 /* That will change things, so, we should relax again.
3076 Note that this is not required, and it may be slow. */
3077 *again = TRUE;
3081 /* Try to eliminate an unconditional 8 bit pc-relative branch
3082 which immediately follows a conditional 8 bit pc-relative
3083 branch around the unconditional branch.
3085 original: new:
3086 bCC lab1 bCC' lab2
3087 bra lab2
3088 lab1: lab1:
3090 This happens when the bCC can't reach lab2 at assembly time,
3091 but due to other relaxations it can reach at link time. */
3092 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
3094 Elf_Internal_Rela *nrel;
3095 bfd_vma value = symval;
3096 unsigned char code;
3098 /* Deal with pc-relative gunk. */
3099 value -= (sec->output_section->vma + sec->output_offset);
3100 value -= irel->r_offset;
3101 value += irel->r_addend;
3103 /* Do nothing if this reloc is the last byte in the section. */
3104 if (irel->r_offset == sec->size)
3105 continue;
3107 /* See if the next instruction is an unconditional pc-relative
3108 branch, more often than not this test will fail, so we
3109 test it first to speed things up. */
3110 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
3111 if (code != 0xca)
3112 continue;
3114 /* Also make sure the next relocation applies to the next
3115 instruction and that it's a pc-relative 8 bit branch. */
3116 nrel = irel + 1;
3117 if (nrel == irelend
3118 || irel->r_offset + 2 != nrel->r_offset
3119 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
3120 continue;
3122 /* Make sure our destination immediately follows the
3123 unconditional branch. */
3124 if (symval != (sec->output_section->vma + sec->output_offset
3125 + irel->r_offset + 3))
3126 continue;
3128 /* Now make sure we are a conditional branch. This may not
3129 be necessary, but why take the chance.
3131 Note these checks assume that R_MN10300_PCREL8 relocs
3132 only occur on bCC and bCCx insns. If they occured
3133 elsewhere, we'd need to know the start of this insn
3134 for this check to be accurate. */
3135 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3136 if (code != 0xc0 && code != 0xc1 && code != 0xc2
3137 && code != 0xc3 && code != 0xc4 && code != 0xc5
3138 && code != 0xc6 && code != 0xc7 && code != 0xc8
3139 && code != 0xc9 && code != 0xe8 && code != 0xe9
3140 && code != 0xea && code != 0xeb)
3141 continue;
3143 /* We also have to be sure there is no symbol/label
3144 at the unconditional branch. */
3145 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
3146 irel->r_offset + 1))
3147 continue;
3149 /* Note that we've changed the relocs, section contents, etc. */
3150 elf_section_data (sec)->relocs = internal_relocs;
3151 elf_section_data (sec)->this_hdr.contents = contents;
3152 symtab_hdr->contents = (unsigned char *) isymbuf;
3154 /* Reverse the condition of the first branch. */
3155 switch (code)
3157 case 0xc8:
3158 code = 0xc9;
3159 break;
3160 case 0xc9:
3161 code = 0xc8;
3162 break;
3163 case 0xc0:
3164 code = 0xc2;
3165 break;
3166 case 0xc2:
3167 code = 0xc0;
3168 break;
3169 case 0xc3:
3170 code = 0xc1;
3171 break;
3172 case 0xc1:
3173 code = 0xc3;
3174 break;
3175 case 0xc4:
3176 code = 0xc6;
3177 break;
3178 case 0xc6:
3179 code = 0xc4;
3180 break;
3181 case 0xc7:
3182 code = 0xc5;
3183 break;
3184 case 0xc5:
3185 code = 0xc7;
3186 break;
3187 case 0xe8:
3188 code = 0xe9;
3189 break;
3190 case 0x9d:
3191 code = 0xe8;
3192 break;
3193 case 0xea:
3194 code = 0xeb;
3195 break;
3196 case 0xeb:
3197 code = 0xea;
3198 break;
3200 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3202 /* Set the reloc type and symbol for the first branch
3203 from the second branch. */
3204 irel->r_info = nrel->r_info;
3206 /* Make the reloc for the second branch a null reloc. */
3207 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3208 R_MN10300_NONE);
3210 /* Delete two bytes of data. */
3211 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3212 irel->r_offset + 1, 2))
3213 goto error_return;
3215 /* That will change things, so, we should relax again.
3216 Note that this is not required, and it may be slow. */
3217 *again = TRUE;
3220 /* Try to turn a 24 immediate, displacement or absolute address
3221 into a 8 immediate, displacement or absolute address. */
3222 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
3224 bfd_vma value = symval;
3225 value += irel->r_addend;
3227 /* See if the value will fit in 8 bits. */
3228 if ((long) value < 0x7f && (long) value > -0x80)
3230 unsigned char code;
3232 /* AM33 insns which have 24 operands are 6 bytes long and
3233 will have 0xfd as the first byte. */
3235 /* Get the first opcode. */
3236 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3238 if (code == 0xfd)
3240 /* Get the second opcode. */
3241 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3243 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3244 equivalent instructions exists. */
3245 if (code != 0x6b && code != 0x7b
3246 && code != 0x8b && code != 0x9b
3247 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3248 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3249 || (code & 0x0f) == 0x0e))
3251 /* Not safe if the high bit is on as relaxing may
3252 move the value out of high mem and thus not fit
3253 in a signed 8bit value. This is currently over
3254 conservative. */
3255 if ((value & 0x80) == 0)
3257 /* Note that we've changed the relocation contents,
3258 etc. */
3259 elf_section_data (sec)->relocs = internal_relocs;
3260 elf_section_data (sec)->this_hdr.contents = contents;
3261 symtab_hdr->contents = (unsigned char *) isymbuf;
3263 /* Fix the opcode. */
3264 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
3265 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3267 /* Fix the relocation's type. */
3268 irel->r_info =
3269 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3270 R_MN10300_8);
3272 /* Delete two bytes of data. */
3273 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3274 irel->r_offset + 1, 2))
3275 goto error_return;
3277 /* That will change things, so, we should relax
3278 again. Note that this is not required, and it
3279 may be slow. */
3280 *again = TRUE;
3281 break;
3288 /* Try to turn a 32bit immediate, displacement or absolute address
3289 into a 16bit immediate, displacement or absolute address. */
3290 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
3291 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
3292 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3294 bfd_vma value = symval;
3296 if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
3298 asection * sgot;
3300 sgot = bfd_get_section_by_name (elf_hash_table (link_info)
3301 ->dynobj, ".got");
3303 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
3305 value = sgot->output_offset;
3307 if (h)
3308 value += h->root.got.offset;
3309 else
3310 value += (elf_local_got_offsets
3311 (abfd)[ELF32_R_SYM (irel->r_info)]);
3313 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3314 value -= sgot->output_section->vma;
3315 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
3316 value = (sgot->output_section->vma
3317 - (sec->output_section->vma
3318 + sec->output_offset
3319 + irel->r_offset));
3320 else
3321 abort ();
3324 value += irel->r_addend;
3326 /* See if the value will fit in 24 bits.
3327 We allow any 16bit match here. We prune those we can't
3328 handle below. */
3329 if ((long) value < 0x7fffff && (long) value > -0x800000)
3331 unsigned char code;
3333 /* AM33 insns which have 32bit operands are 7 bytes long and
3334 will have 0xfe as the first byte. */
3336 /* Get the first opcode. */
3337 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3339 if (code == 0xfe)
3341 /* Get the second opcode. */
3342 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3344 /* All the am33 32 -> 24 relaxing possibilities. */
3345 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3346 equivalent instructions exists. */
3347 if (code != 0x6b && code != 0x7b
3348 && code != 0x8b && code != 0x9b
3349 && (ELF32_R_TYPE (irel->r_info)
3350 != (int) R_MN10300_GOTPC32)
3351 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3352 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3353 || (code & 0x0f) == 0x0e))
3355 /* Not safe if the high bit is on as relaxing may
3356 move the value out of high mem and thus not fit
3357 in a signed 16bit value. This is currently over
3358 conservative. */
3359 if ((value & 0x8000) == 0)
3361 /* Note that we've changed the relocation contents,
3362 etc. */
3363 elf_section_data (sec)->relocs = internal_relocs;
3364 elf_section_data (sec)->this_hdr.contents = contents;
3365 symtab_hdr->contents = (unsigned char *) isymbuf;
3367 /* Fix the opcode. */
3368 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
3369 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3371 /* Fix the relocation's type. */
3372 irel->r_info =
3373 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3374 (ELF32_R_TYPE (irel->r_info)
3375 == (int) R_MN10300_GOTOFF32)
3376 ? R_MN10300_GOTOFF24
3377 : (ELF32_R_TYPE (irel->r_info)
3378 == (int) R_MN10300_GOT32)
3379 ? R_MN10300_GOT24 :
3380 R_MN10300_24);
3382 /* Delete one byte of data. */
3383 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3384 irel->r_offset + 3, 1))
3385 goto error_return;
3387 /* That will change things, so, we should relax
3388 again. Note that this is not required, and it
3389 may be slow. */
3390 *again = TRUE;
3391 break;
3397 /* See if the value will fit in 16 bits.
3398 We allow any 16bit match here. We prune those we can't
3399 handle below. */
3400 if ((long) value < 0x7fff && (long) value > -0x8000)
3402 unsigned char code;
3404 /* Most insns which have 32bit operands are 6 bytes long;
3405 exceptions are pcrel insns and bit insns.
3407 We handle pcrel insns above. We don't bother trying
3408 to handle the bit insns here.
3410 The first byte of the remaining insns will be 0xfc. */
3412 /* Get the first opcode. */
3413 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3415 if (code != 0xfc)
3416 continue;
3418 /* Get the second opcode. */
3419 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3421 if ((code & 0xf0) < 0x80)
3422 switch (code & 0xf0)
3424 /* mov (d32,am),dn -> mov (d32,am),dn
3425 mov dm,(d32,am) -> mov dn,(d32,am)
3426 mov (d32,am),an -> mov (d32,am),an
3427 mov dm,(d32,am) -> mov dn,(d32,am)
3428 movbu (d32,am),dn -> movbu (d32,am),dn
3429 movbu dm,(d32,am) -> movbu dn,(d32,am)
3430 movhu (d32,am),dn -> movhu (d32,am),dn
3431 movhu dm,(d32,am) -> movhu dn,(d32,am) */
3432 case 0x00:
3433 case 0x10:
3434 case 0x20:
3435 case 0x30:
3436 case 0x40:
3437 case 0x50:
3438 case 0x60:
3439 case 0x70:
3440 /* Not safe if the high bit is on as relaxing may
3441 move the value out of high mem and thus not fit
3442 in a signed 16bit value. */
3443 if (code == 0xcc
3444 && (value & 0x8000))
3445 continue;
3447 /* Note that we've changed the relocation contents, etc. */
3448 elf_section_data (sec)->relocs = internal_relocs;
3449 elf_section_data (sec)->this_hdr.contents = contents;
3450 symtab_hdr->contents = (unsigned char *) isymbuf;
3452 /* Fix the opcode. */
3453 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3454 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3456 /* Fix the relocation's type. */
3457 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3458 (ELF32_R_TYPE (irel->r_info)
3459 == (int) R_MN10300_GOTOFF32)
3460 ? R_MN10300_GOTOFF16
3461 : (ELF32_R_TYPE (irel->r_info)
3462 == (int) R_MN10300_GOT32)
3463 ? R_MN10300_GOT16
3464 : (ELF32_R_TYPE (irel->r_info)
3465 == (int) R_MN10300_GOTPC32)
3466 ? R_MN10300_GOTPC16 :
3467 R_MN10300_16);
3469 /* Delete two bytes of data. */
3470 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3471 irel->r_offset + 2, 2))
3472 goto error_return;
3474 /* That will change things, so, we should relax again.
3475 Note that this is not required, and it may be slow. */
3476 *again = TRUE;
3477 break;
3479 else if ((code & 0xf0) == 0x80
3480 || (code & 0xf0) == 0x90)
3481 switch (code & 0xf3)
3483 /* mov dn,(abs32) -> mov dn,(abs16)
3484 movbu dn,(abs32) -> movbu dn,(abs16)
3485 movhu dn,(abs32) -> movhu dn,(abs16) */
3486 case 0x81:
3487 case 0x82:
3488 case 0x83:
3489 /* Note that we've changed the relocation contents, etc. */
3490 elf_section_data (sec)->relocs = internal_relocs;
3491 elf_section_data (sec)->this_hdr.contents = contents;
3492 symtab_hdr->contents = (unsigned char *) isymbuf;
3494 if ((code & 0xf3) == 0x81)
3495 code = 0x01 + (code & 0x0c);
3496 else if ((code & 0xf3) == 0x82)
3497 code = 0x02 + (code & 0x0c);
3498 else if ((code & 0xf3) == 0x83)
3499 code = 0x03 + (code & 0x0c);
3500 else
3501 abort ();
3503 /* Fix the opcode. */
3504 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3506 /* Fix the relocation's type. */
3507 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3508 (ELF32_R_TYPE (irel->r_info)
3509 == (int) R_MN10300_GOTOFF32)
3510 ? R_MN10300_GOTOFF16
3511 : (ELF32_R_TYPE (irel->r_info)
3512 == (int) R_MN10300_GOT32)
3513 ? R_MN10300_GOT16
3514 : (ELF32_R_TYPE (irel->r_info)
3515 == (int) R_MN10300_GOTPC32)
3516 ? R_MN10300_GOTPC16 :
3517 R_MN10300_16);
3519 /* The opcode got shorter too, so we have to fix the
3520 addend and offset too! */
3521 irel->r_offset -= 1;
3523 /* Delete three bytes of data. */
3524 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3525 irel->r_offset + 1, 3))
3526 goto error_return;
3528 /* That will change things, so, we should relax again.
3529 Note that this is not required, and it may be slow. */
3530 *again = TRUE;
3531 break;
3533 /* mov am,(abs32) -> mov am,(abs16)
3534 mov am,(d32,sp) -> mov am,(d16,sp)
3535 mov dm,(d32,sp) -> mov dm,(d32,sp)
3536 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3537 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3538 case 0x80:
3539 case 0x90:
3540 case 0x91:
3541 case 0x92:
3542 case 0x93:
3543 /* sp-based offsets are zero-extended. */
3544 if (code >= 0x90 && code <= 0x93
3545 && (long) value < 0)
3546 continue;
3548 /* Note that we've changed the relocation contents, etc. */
3549 elf_section_data (sec)->relocs = internal_relocs;
3550 elf_section_data (sec)->this_hdr.contents = contents;
3551 symtab_hdr->contents = (unsigned char *) isymbuf;
3553 /* Fix the opcode. */
3554 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3555 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3557 /* Fix the relocation's type. */
3558 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3559 (ELF32_R_TYPE (irel->r_info)
3560 == (int) R_MN10300_GOTOFF32)
3561 ? R_MN10300_GOTOFF16
3562 : (ELF32_R_TYPE (irel->r_info)
3563 == (int) R_MN10300_GOT32)
3564 ? R_MN10300_GOT16
3565 : (ELF32_R_TYPE (irel->r_info)
3566 == (int) R_MN10300_GOTPC32)
3567 ? R_MN10300_GOTPC16 :
3568 R_MN10300_16);
3570 /* Delete two bytes of data. */
3571 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3572 irel->r_offset + 2, 2))
3573 goto error_return;
3575 /* That will change things, so, we should relax again.
3576 Note that this is not required, and it may be slow. */
3577 *again = TRUE;
3578 break;
3580 else if ((code & 0xf0) < 0xf0)
3581 switch (code & 0xfc)
3583 /* mov imm32,dn -> mov imm16,dn
3584 mov imm32,an -> mov imm16,an
3585 mov (abs32),dn -> mov (abs16),dn
3586 movbu (abs32),dn -> movbu (abs16),dn
3587 movhu (abs32),dn -> movhu (abs16),dn */
3588 case 0xcc:
3589 case 0xdc:
3590 case 0xa4:
3591 case 0xa8:
3592 case 0xac:
3593 /* Not safe if the high bit is on as relaxing may
3594 move the value out of high mem and thus not fit
3595 in a signed 16bit value. */
3596 if (code == 0xcc
3597 && (value & 0x8000))
3598 continue;
3600 /* mov imm16, an zero-extends the immediate. */
3601 if (code == 0xdc
3602 && (long) value < 0)
3603 continue;
3605 /* Note that we've changed the relocation contents, etc. */
3606 elf_section_data (sec)->relocs = internal_relocs;
3607 elf_section_data (sec)->this_hdr.contents = contents;
3608 symtab_hdr->contents = (unsigned char *) isymbuf;
3610 if ((code & 0xfc) == 0xcc)
3611 code = 0x2c + (code & 0x03);
3612 else if ((code & 0xfc) == 0xdc)
3613 code = 0x24 + (code & 0x03);
3614 else if ((code & 0xfc) == 0xa4)
3615 code = 0x30 + (code & 0x03);
3616 else if ((code & 0xfc) == 0xa8)
3617 code = 0x34 + (code & 0x03);
3618 else if ((code & 0xfc) == 0xac)
3619 code = 0x38 + (code & 0x03);
3620 else
3621 abort ();
3623 /* Fix the opcode. */
3624 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3626 /* Fix the relocation's type. */
3627 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3628 (ELF32_R_TYPE (irel->r_info)
3629 == (int) R_MN10300_GOTOFF32)
3630 ? R_MN10300_GOTOFF16
3631 : (ELF32_R_TYPE (irel->r_info)
3632 == (int) R_MN10300_GOT32)
3633 ? R_MN10300_GOT16
3634 : (ELF32_R_TYPE (irel->r_info)
3635 == (int) R_MN10300_GOTPC32)
3636 ? R_MN10300_GOTPC16 :
3637 R_MN10300_16);
3639 /* The opcode got shorter too, so we have to fix the
3640 addend and offset too! */
3641 irel->r_offset -= 1;
3643 /* Delete three bytes of data. */
3644 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3645 irel->r_offset + 1, 3))
3646 goto error_return;
3648 /* That will change things, so, we should relax again.
3649 Note that this is not required, and it may be slow. */
3650 *again = TRUE;
3651 break;
3653 /* mov (abs32),an -> mov (abs16),an
3654 mov (d32,sp),an -> mov (d16,sp),an
3655 mov (d32,sp),dn -> mov (d16,sp),dn
3656 movbu (d32,sp),dn -> movbu (d16,sp),dn
3657 movhu (d32,sp),dn -> movhu (d16,sp),dn
3658 add imm32,dn -> add imm16,dn
3659 cmp imm32,dn -> cmp imm16,dn
3660 add imm32,an -> add imm16,an
3661 cmp imm32,an -> cmp imm16,an
3662 and imm32,dn -> and imm16,dn
3663 or imm32,dn -> or imm16,dn
3664 xor imm32,dn -> xor imm16,dn
3665 btst imm32,dn -> btst imm16,dn */
3667 case 0xa0:
3668 case 0xb0:
3669 case 0xb1:
3670 case 0xb2:
3671 case 0xb3:
3672 case 0xc0:
3673 case 0xc8:
3675 case 0xd0:
3676 case 0xd8:
3677 case 0xe0:
3678 case 0xe1:
3679 case 0xe2:
3680 case 0xe3:
3681 /* cmp imm16, an zero-extends the immediate. */
3682 if (code == 0xdc
3683 && (long) value < 0)
3684 continue;
3686 /* So do sp-based offsets. */
3687 if (code >= 0xb0 && code <= 0xb3
3688 && (long) value < 0)
3689 continue;
3691 /* Note that we've changed the relocation contents, etc. */
3692 elf_section_data (sec)->relocs = internal_relocs;
3693 elf_section_data (sec)->this_hdr.contents = contents;
3694 symtab_hdr->contents = (unsigned char *) isymbuf;
3696 /* Fix the opcode. */
3697 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3698 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3700 /* Fix the relocation's type. */
3701 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3702 (ELF32_R_TYPE (irel->r_info)
3703 == (int) R_MN10300_GOTOFF32)
3704 ? R_MN10300_GOTOFF16
3705 : (ELF32_R_TYPE (irel->r_info)
3706 == (int) R_MN10300_GOT32)
3707 ? R_MN10300_GOT16
3708 : (ELF32_R_TYPE (irel->r_info)
3709 == (int) R_MN10300_GOTPC32)
3710 ? R_MN10300_GOTPC16 :
3711 R_MN10300_16);
3713 /* Delete two bytes of data. */
3714 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3715 irel->r_offset + 2, 2))
3716 goto error_return;
3718 /* That will change things, so, we should relax again.
3719 Note that this is not required, and it may be slow. */
3720 *again = TRUE;
3721 break;
3723 else if (code == 0xfe)
3725 /* add imm32,sp -> add imm16,sp */
3727 /* Note that we've changed the relocation contents, etc. */
3728 elf_section_data (sec)->relocs = internal_relocs;
3729 elf_section_data (sec)->this_hdr.contents = contents;
3730 symtab_hdr->contents = (unsigned char *) isymbuf;
3732 /* Fix the opcode. */
3733 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3734 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
3736 /* Fix the relocation's type. */
3737 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3738 (ELF32_R_TYPE (irel->r_info)
3739 == (int) R_MN10300_GOT32)
3740 ? R_MN10300_GOT16
3741 : (ELF32_R_TYPE (irel->r_info)
3742 == (int) R_MN10300_GOTOFF32)
3743 ? R_MN10300_GOTOFF16
3744 : (ELF32_R_TYPE (irel->r_info)
3745 == (int) R_MN10300_GOTPC32)
3746 ? R_MN10300_GOTPC16 :
3747 R_MN10300_16);
3749 /* Delete two bytes of data. */
3750 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3751 irel->r_offset + 2, 2))
3752 goto error_return;
3754 /* That will change things, so, we should relax again.
3755 Note that this is not required, and it may be slow. */
3756 *again = TRUE;
3757 break;
3763 if (isymbuf != NULL
3764 && symtab_hdr->contents != (unsigned char *) isymbuf)
3766 if (! link_info->keep_memory)
3767 free (isymbuf);
3768 else
3770 /* Cache the symbols for elf_link_input_bfd. */
3771 symtab_hdr->contents = (unsigned char *) isymbuf;
3775 if (contents != NULL
3776 && elf_section_data (sec)->this_hdr.contents != contents)
3778 if (! link_info->keep_memory)
3779 free (contents);
3780 else
3782 /* Cache the section contents for elf_link_input_bfd. */
3783 elf_section_data (sec)->this_hdr.contents = contents;
3787 if (internal_relocs != NULL
3788 && elf_section_data (sec)->relocs != internal_relocs)
3789 free (internal_relocs);
3791 return TRUE;
3793 error_return:
3794 if (isymbuf != NULL
3795 && symtab_hdr->contents != (unsigned char *) isymbuf)
3796 free (isymbuf);
3797 if (contents != NULL
3798 && elf_section_data (section)->this_hdr.contents != contents)
3799 free (contents);
3800 if (internal_relocs != NULL
3801 && elf_section_data (section)->relocs != internal_relocs)
3802 free (internal_relocs);
3804 return FALSE;
3807 /* This is a version of bfd_generic_get_relocated_section_contents
3808 which uses mn10300_elf_relocate_section. */
3810 static bfd_byte *
3811 mn10300_elf_get_relocated_section_contents (bfd *output_bfd,
3812 struct bfd_link_info *link_info,
3813 struct bfd_link_order *link_order,
3814 bfd_byte *data,
3815 bfd_boolean relocatable,
3816 asymbol **symbols)
3818 Elf_Internal_Shdr *symtab_hdr;
3819 asection *input_section = link_order->u.indirect.section;
3820 bfd *input_bfd = input_section->owner;
3821 asection **sections = NULL;
3822 Elf_Internal_Rela *internal_relocs = NULL;
3823 Elf_Internal_Sym *isymbuf = NULL;
3825 /* We only need to handle the case of relaxing, or of having a
3826 particular set of section contents, specially. */
3827 if (relocatable
3828 || elf_section_data (input_section)->this_hdr.contents == NULL)
3829 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3830 link_order, data,
3831 relocatable,
3832 symbols);
3834 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3836 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3837 (size_t) input_section->size);
3839 if ((input_section->flags & SEC_RELOC) != 0
3840 && input_section->reloc_count > 0)
3842 asection **secpp;
3843 Elf_Internal_Sym *isym, *isymend;
3844 bfd_size_type amt;
3846 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
3847 NULL, NULL, FALSE);
3848 if (internal_relocs == NULL)
3849 goto error_return;
3851 if (symtab_hdr->sh_info != 0)
3853 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3854 if (isymbuf == NULL)
3855 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3856 symtab_hdr->sh_info, 0,
3857 NULL, NULL, NULL);
3858 if (isymbuf == NULL)
3859 goto error_return;
3862 amt = symtab_hdr->sh_info;
3863 amt *= sizeof (asection *);
3864 sections = bfd_malloc (amt);
3865 if (sections == NULL && amt != 0)
3866 goto error_return;
3868 isymend = isymbuf + symtab_hdr->sh_info;
3869 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3871 asection *isec;
3873 if (isym->st_shndx == SHN_UNDEF)
3874 isec = bfd_und_section_ptr;
3875 else if (isym->st_shndx == SHN_ABS)
3876 isec = bfd_abs_section_ptr;
3877 else if (isym->st_shndx == SHN_COMMON)
3878 isec = bfd_com_section_ptr;
3879 else
3880 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3882 *secpp = isec;
3885 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
3886 input_section, data, internal_relocs,
3887 isymbuf, sections))
3888 goto error_return;
3890 if (sections != NULL)
3891 free (sections);
3892 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3893 free (isymbuf);
3894 if (internal_relocs != elf_section_data (input_section)->relocs)
3895 free (internal_relocs);
3898 return data;
3900 error_return:
3901 if (sections != NULL)
3902 free (sections);
3903 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3904 free (isymbuf);
3905 if (internal_relocs != NULL
3906 && internal_relocs != elf_section_data (input_section)->relocs)
3907 free (internal_relocs);
3908 return NULL;
3911 /* Assorted hash table functions. */
3913 /* Initialize an entry in the link hash table. */
3915 /* Create an entry in an MN10300 ELF linker hash table. */
3917 static struct bfd_hash_entry *
3918 elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry *entry,
3919 struct bfd_hash_table *table,
3920 const char *string)
3922 struct elf32_mn10300_link_hash_entry *ret =
3923 (struct elf32_mn10300_link_hash_entry *) entry;
3925 /* Allocate the structure if it has not already been allocated by a
3926 subclass. */
3927 if (ret == NULL)
3928 ret = (struct elf32_mn10300_link_hash_entry *)
3929 bfd_hash_allocate (table, sizeof (* ret));
3930 if (ret == NULL)
3931 return (struct bfd_hash_entry *) ret;
3933 /* Call the allocation method of the superclass. */
3934 ret = (struct elf32_mn10300_link_hash_entry *)
3935 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3936 table, string);
3937 if (ret != NULL)
3939 ret->direct_calls = 0;
3940 ret->stack_size = 0;
3941 ret->movm_args = 0;
3942 ret->movm_stack_size = 0;
3943 ret->flags = 0;
3944 ret->value = 0;
3947 return (struct bfd_hash_entry *) ret;
3950 /* Create an mn10300 ELF linker hash table. */
3952 static struct bfd_link_hash_table *
3953 elf32_mn10300_link_hash_table_create (bfd *abfd)
3955 struct elf32_mn10300_link_hash_table *ret;
3956 bfd_size_type amt = sizeof (* ret);
3958 ret = bfd_malloc (amt);
3959 if (ret == NULL)
3960 return NULL;
3962 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3963 elf32_mn10300_link_hash_newfunc,
3964 sizeof (struct elf32_mn10300_link_hash_entry)))
3966 free (ret);
3967 return NULL;
3970 ret->flags = 0;
3971 amt = sizeof (struct elf_link_hash_table);
3972 ret->static_hash_table = bfd_malloc (amt);
3973 if (ret->static_hash_table == NULL)
3975 free (ret);
3976 return NULL;
3979 if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
3980 elf32_mn10300_link_hash_newfunc,
3981 sizeof (struct elf32_mn10300_link_hash_entry)))
3983 free (ret->static_hash_table);
3984 free (ret);
3985 return NULL;
3987 return & ret->root.root;
3990 /* Free an mn10300 ELF linker hash table. */
3992 static void
3993 elf32_mn10300_link_hash_table_free (struct bfd_link_hash_table *hash)
3995 struct elf32_mn10300_link_hash_table *ret
3996 = (struct elf32_mn10300_link_hash_table *) hash;
3998 _bfd_generic_link_hash_table_free
3999 ((struct bfd_link_hash_table *) ret->static_hash_table);
4000 _bfd_generic_link_hash_table_free
4001 ((struct bfd_link_hash_table *) ret);
4004 static unsigned long
4005 elf_mn10300_mach (flagword flags)
4007 switch (flags & EF_MN10300_MACH)
4009 case E_MN10300_MACH_MN10300:
4010 default:
4011 return bfd_mach_mn10300;
4013 case E_MN10300_MACH_AM33:
4014 return bfd_mach_am33;
4016 case E_MN10300_MACH_AM33_2:
4017 return bfd_mach_am33_2;
4021 /* The final processing done just before writing out a MN10300 ELF object
4022 file. This gets the MN10300 architecture right based on the machine
4023 number. */
4025 static void
4026 _bfd_mn10300_elf_final_write_processing (bfd *abfd,
4027 bfd_boolean linker ATTRIBUTE_UNUSED)
4029 unsigned long val;
4031 switch (bfd_get_mach (abfd))
4033 default:
4034 case bfd_mach_mn10300:
4035 val = E_MN10300_MACH_MN10300;
4036 break;
4038 case bfd_mach_am33:
4039 val = E_MN10300_MACH_AM33;
4040 break;
4042 case bfd_mach_am33_2:
4043 val = E_MN10300_MACH_AM33_2;
4044 break;
4047 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
4048 elf_elfheader (abfd)->e_flags |= val;
4051 static bfd_boolean
4052 _bfd_mn10300_elf_object_p (bfd *abfd)
4054 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
4055 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
4056 return TRUE;
4059 /* Merge backend specific data from an object file to the output
4060 object file when linking. */
4062 static bfd_boolean
4063 _bfd_mn10300_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4065 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4066 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4067 return TRUE;
4069 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4070 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
4072 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4073 bfd_get_mach (ibfd)))
4074 return FALSE;
4077 return TRUE;
4080 #define PLT0_ENTRY_SIZE 15
4081 #define PLT_ENTRY_SIZE 20
4082 #define PIC_PLT_ENTRY_SIZE 24
4084 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
4086 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
4087 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
4088 0xf0, 0xf4, /* jmp (a0) */
4091 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
4093 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
4094 0xf0, 0xf4, /* jmp (a0) */
4095 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4096 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
4099 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
4101 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
4102 0xf0, 0xf4, /* jmp (a0) */
4103 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4104 0xf8, 0x22, 8, /* mov (8,a2),a0 */
4105 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
4106 0xf0, 0xf4, /* jmp (a0) */
4109 /* Return size of the first PLT entry. */
4110 #define elf_mn10300_sizeof_plt0(info) \
4111 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4113 /* Return size of a PLT entry. */
4114 #define elf_mn10300_sizeof_plt(info) \
4115 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4117 /* Return offset of the PLT0 address in an absolute PLT entry. */
4118 #define elf_mn10300_plt_plt0_offset(info) 16
4120 /* Return offset of the linker in PLT0 entry. */
4121 #define elf_mn10300_plt0_linker_offset(info) 2
4123 /* Return offset of the GOT id in PLT0 entry. */
4124 #define elf_mn10300_plt0_gotid_offset(info) 9
4126 /* Return offset of the temporary in PLT entry. */
4127 #define elf_mn10300_plt_temp_offset(info) 8
4129 /* Return offset of the symbol in PLT entry. */
4130 #define elf_mn10300_plt_symbol_offset(info) 2
4132 /* Return offset of the relocation in PLT entry. */
4133 #define elf_mn10300_plt_reloc_offset(info) 11
4135 /* The name of the dynamic interpreter. This is put in the .interp
4136 section. */
4138 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4140 /* Create dynamic sections when linking against a dynamic object. */
4142 static bfd_boolean
4143 _bfd_mn10300_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4145 flagword flags;
4146 asection * s;
4147 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
4148 int ptralign = 0;
4150 switch (bed->s->arch_size)
4152 case 32:
4153 ptralign = 2;
4154 break;
4156 case 64:
4157 ptralign = 3;
4158 break;
4160 default:
4161 bfd_set_error (bfd_error_bad_value);
4162 return FALSE;
4165 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4166 .rel[a].bss sections. */
4167 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4168 | SEC_LINKER_CREATED);
4170 s = bfd_make_section_with_flags (abfd,
4171 (bed->default_use_rela_p
4172 ? ".rela.plt" : ".rel.plt"),
4173 flags | SEC_READONLY);
4174 if (s == NULL
4175 || ! bfd_set_section_alignment (abfd, s, ptralign))
4176 return FALSE;
4178 if (! _bfd_mn10300_elf_create_got_section (abfd, info))
4179 return FALSE;
4182 const char * secname;
4183 char * relname;
4184 flagword secflags;
4185 asection * sec;
4187 for (sec = abfd->sections; sec; sec = sec->next)
4189 secflags = bfd_get_section_flags (abfd, sec);
4190 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
4191 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
4192 continue;
4194 secname = bfd_get_section_name (abfd, sec);
4195 relname = bfd_malloc (strlen (secname) + 6);
4196 strcpy (relname, ".rela");
4197 strcat (relname, secname);
4199 s = bfd_make_section_with_flags (abfd, relname,
4200 flags | SEC_READONLY);
4201 if (s == NULL
4202 || ! bfd_set_section_alignment (abfd, s, ptralign))
4203 return FALSE;
4207 if (bed->want_dynbss)
4209 /* The .dynbss section is a place to put symbols which are defined
4210 by dynamic objects, are referenced by regular objects, and are
4211 not functions. We must allocate space for them in the process
4212 image and use a R_*_COPY reloc to tell the dynamic linker to
4213 initialize them at run time. The linker script puts the .dynbss
4214 section into the .bss section of the final image. */
4215 s = bfd_make_section_with_flags (abfd, ".dynbss",
4216 SEC_ALLOC | SEC_LINKER_CREATED);
4217 if (s == NULL)
4218 return FALSE;
4220 /* The .rel[a].bss section holds copy relocs. This section is not
4221 normally needed. We need to create it here, though, so that the
4222 linker will map it to an output section. We can't just create it
4223 only if we need it, because we will not know whether we need it
4224 until we have seen all the input files, and the first time the
4225 main linker code calls BFD after examining all the input files
4226 (size_dynamic_sections) the input sections have already been
4227 mapped to the output sections. If the section turns out not to
4228 be needed, we can discard it later. We will never need this
4229 section when generating a shared object, since they do not use
4230 copy relocs. */
4231 if (! info->shared)
4233 s = bfd_make_section_with_flags (abfd,
4234 (bed->default_use_rela_p
4235 ? ".rela.bss" : ".rel.bss"),
4236 flags | SEC_READONLY);
4237 if (s == NULL
4238 || ! bfd_set_section_alignment (abfd, s, ptralign))
4239 return FALSE;
4243 return TRUE;
4246 /* Adjust a symbol defined by a dynamic object and referenced by a
4247 regular object. The current definition is in some section of the
4248 dynamic object, but we're not including those sections. We have to
4249 change the definition to something the rest of the link can
4250 understand. */
4252 static bfd_boolean
4253 _bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
4254 struct elf_link_hash_entry * h)
4256 bfd * dynobj;
4257 asection * s;
4259 dynobj = elf_hash_table (info)->dynobj;
4261 /* Make sure we know what is going on here. */
4262 BFD_ASSERT (dynobj != NULL
4263 && (h->needs_plt
4264 || h->u.weakdef != NULL
4265 || (h->def_dynamic
4266 && h->ref_regular
4267 && !h->def_regular)));
4269 /* If this is a function, put it in the procedure linkage table. We
4270 will fill in the contents of the procedure linkage table later,
4271 when we know the address of the .got section. */
4272 if (h->type == STT_FUNC
4273 || h->needs_plt)
4275 if (! info->shared
4276 && !h->def_dynamic
4277 && !h->ref_dynamic)
4279 /* This case can occur if we saw a PLT reloc in an input
4280 file, but the symbol was never referred to by a dynamic
4281 object. In such a case, we don't actually need to build
4282 a procedure linkage table, and we can just do a REL32
4283 reloc instead. */
4284 BFD_ASSERT (h->needs_plt);
4285 return TRUE;
4288 /* Make sure this symbol is output as a dynamic symbol. */
4289 if (h->dynindx == -1)
4291 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4292 return FALSE;
4295 s = bfd_get_section_by_name (dynobj, ".plt");
4296 BFD_ASSERT (s != NULL);
4298 /* If this is the first .plt entry, make room for the special
4299 first entry. */
4300 if (s->size == 0)
4301 s->size += elf_mn10300_sizeof_plt0 (info);
4303 /* If this symbol is not defined in a regular file, and we are
4304 not generating a shared library, then set the symbol to this
4305 location in the .plt. This is required to make function
4306 pointers compare as equal between the normal executable and
4307 the shared library. */
4308 if (! info->shared
4309 && !h->def_regular)
4311 h->root.u.def.section = s;
4312 h->root.u.def.value = s->size;
4315 h->plt.offset = s->size;
4317 /* Make room for this entry. */
4318 s->size += elf_mn10300_sizeof_plt (info);
4320 /* We also need to make an entry in the .got.plt section, which
4321 will be placed in the .got section by the linker script. */
4322 s = bfd_get_section_by_name (dynobj, ".got.plt");
4323 BFD_ASSERT (s != NULL);
4324 s->size += 4;
4326 /* We also need to make an entry in the .rela.plt section. */
4327 s = bfd_get_section_by_name (dynobj, ".rela.plt");
4328 BFD_ASSERT (s != NULL);
4329 s->size += sizeof (Elf32_External_Rela);
4331 return TRUE;
4334 /* If this is a weak symbol, and there is a real definition, the
4335 processor independent code will have arranged for us to see the
4336 real definition first, and we can just use the same value. */
4337 if (h->u.weakdef != NULL)
4339 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4340 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4341 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4342 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4343 return TRUE;
4346 /* This is a reference to a symbol defined by a dynamic object which
4347 is not a function. */
4349 /* If we are creating a shared library, we must presume that the
4350 only references to the symbol are via the global offset table.
4351 For such cases we need not do anything here; the relocations will
4352 be handled correctly by relocate_section. */
4353 if (info->shared)
4354 return TRUE;
4356 /* If there are no references to this symbol that do not use the
4357 GOT, we don't need to generate a copy reloc. */
4358 if (!h->non_got_ref)
4359 return TRUE;
4361 if (h->size == 0)
4363 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
4364 h->root.root.string);
4365 return TRUE;
4368 /* We must allocate the symbol in our .dynbss section, which will
4369 become part of the .bss section of the executable. There will be
4370 an entry for this symbol in the .dynsym section. The dynamic
4371 object will contain position independent code, so all references
4372 from the dynamic object to this symbol will go through the global
4373 offset table. The dynamic linker will use the .dynsym entry to
4374 determine the address it must put in the global offset table, so
4375 both the dynamic object and the regular object will refer to the
4376 same memory location for the variable. */
4378 s = bfd_get_section_by_name (dynobj, ".dynbss");
4379 BFD_ASSERT (s != NULL);
4381 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4382 copy the initial value out of the dynamic object and into the
4383 runtime process image. We need to remember the offset into the
4384 .rela.bss section we are going to use. */
4385 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4387 asection * srel;
4389 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
4390 BFD_ASSERT (srel != NULL);
4391 srel->size += sizeof (Elf32_External_Rela);
4392 h->needs_copy = 1;
4395 return _bfd_elf_adjust_dynamic_copy (h, s);
4398 /* Set the sizes of the dynamic sections. */
4400 static bfd_boolean
4401 _bfd_mn10300_elf_size_dynamic_sections (bfd * output_bfd,
4402 struct bfd_link_info * info)
4404 bfd * dynobj;
4405 asection * s;
4406 bfd_boolean plt;
4407 bfd_boolean relocs;
4408 bfd_boolean reltext;
4410 dynobj = elf_hash_table (info)->dynobj;
4411 BFD_ASSERT (dynobj != NULL);
4413 if (elf_hash_table (info)->dynamic_sections_created)
4415 /* Set the contents of the .interp section to the interpreter. */
4416 if (info->executable)
4418 s = bfd_get_section_by_name (dynobj, ".interp");
4419 BFD_ASSERT (s != NULL);
4420 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4421 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4424 else
4426 /* We may have created entries in the .rela.got section.
4427 However, if we are not creating the dynamic sections, we will
4428 not actually use these entries. Reset the size of .rela.got,
4429 which will cause it to get stripped from the output file
4430 below. */
4431 s = bfd_get_section_by_name (dynobj, ".rela.got");
4432 if (s != NULL)
4433 s->size = 0;
4436 /* The check_relocs and adjust_dynamic_symbol entry points have
4437 determined the sizes of the various dynamic sections. Allocate
4438 memory for them. */
4439 plt = FALSE;
4440 relocs = FALSE;
4441 reltext = FALSE;
4442 for (s = dynobj->sections; s != NULL; s = s->next)
4444 const char * name;
4446 if ((s->flags & SEC_LINKER_CREATED) == 0)
4447 continue;
4449 /* It's OK to base decisions on the section name, because none
4450 of the dynobj section names depend upon the input files. */
4451 name = bfd_get_section_name (dynobj, s);
4453 if (streq (name, ".plt"))
4455 /* Remember whether there is a PLT. */
4456 plt = s->size != 0;
4458 else if (CONST_STRNEQ (name, ".rela"))
4460 if (s->size != 0)
4462 asection * target;
4464 /* Remember whether there are any reloc sections other
4465 than .rela.plt. */
4466 if (! streq (name, ".rela.plt"))
4468 const char * outname;
4470 relocs = TRUE;
4472 /* If this relocation section applies to a read only
4473 section, then we probably need a DT_TEXTREL
4474 entry. The entries in the .rela.plt section
4475 really apply to the .got section, which we
4476 created ourselves and so know is not readonly. */
4477 outname = bfd_get_section_name (output_bfd,
4478 s->output_section);
4479 target = bfd_get_section_by_name (output_bfd, outname + 5);
4480 if (target != NULL
4481 && (target->flags & SEC_READONLY) != 0
4482 && (target->flags & SEC_ALLOC) != 0)
4483 reltext = TRUE;
4486 /* We use the reloc_count field as a counter if we need
4487 to copy relocs into the output file. */
4488 s->reloc_count = 0;
4491 else if (! CONST_STRNEQ (name, ".got")
4492 && ! streq (name, ".dynbss"))
4493 /* It's not one of our sections, so don't allocate space. */
4494 continue;
4496 if (s->size == 0)
4498 /* If we don't need this section, strip it from the
4499 output file. This is mostly to handle .rela.bss and
4500 .rela.plt. We must create both sections in
4501 create_dynamic_sections, because they must be created
4502 before the linker maps input sections to output
4503 sections. The linker does that before
4504 adjust_dynamic_symbol is called, and it is that
4505 function which decides whether anything needs to go
4506 into these sections. */
4507 s->flags |= SEC_EXCLUDE;
4508 continue;
4511 if ((s->flags & SEC_HAS_CONTENTS) == 0)
4512 continue;
4514 /* Allocate memory for the section contents. We use bfd_zalloc
4515 here in case unused entries are not reclaimed before the
4516 section's contents are written out. This should not happen,
4517 but this way if it does, we get a R_MN10300_NONE reloc
4518 instead of garbage. */
4519 s->contents = bfd_zalloc (dynobj, s->size);
4520 if (s->contents == NULL)
4521 return FALSE;
4524 if (elf_hash_table (info)->dynamic_sections_created)
4526 /* Add some entries to the .dynamic section. We fill in the
4527 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4528 but we must add the entries now so that we get the correct
4529 size for the .dynamic section. The DT_DEBUG entry is filled
4530 in by the dynamic linker and used by the debugger. */
4531 if (! info->shared)
4533 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
4534 return FALSE;
4537 if (plt)
4539 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
4540 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
4541 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
4542 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
4543 return FALSE;
4546 if (relocs)
4548 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
4549 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
4550 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
4551 sizeof (Elf32_External_Rela)))
4552 return FALSE;
4555 if (reltext)
4557 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
4558 return FALSE;
4562 return TRUE;
4565 /* Finish up dynamic symbol handling. We set the contents of various
4566 dynamic sections here. */
4568 static bfd_boolean
4569 _bfd_mn10300_elf_finish_dynamic_symbol (bfd * output_bfd,
4570 struct bfd_link_info * info,
4571 struct elf_link_hash_entry * h,
4572 Elf_Internal_Sym * sym)
4574 bfd * dynobj;
4576 dynobj = elf_hash_table (info)->dynobj;
4578 if (h->plt.offset != (bfd_vma) -1)
4580 asection * splt;
4581 asection * sgot;
4582 asection * srel;
4583 bfd_vma plt_index;
4584 bfd_vma got_offset;
4585 Elf_Internal_Rela rel;
4587 /* This symbol has an entry in the procedure linkage table. Set
4588 it up. */
4590 BFD_ASSERT (h->dynindx != -1);
4592 splt = bfd_get_section_by_name (dynobj, ".plt");
4593 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4594 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4595 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4597 /* Get the index in the procedure linkage table which
4598 corresponds to this symbol. This is the index of this symbol
4599 in all the symbols for which we are making plt entries. The
4600 first entry in the procedure linkage table is reserved. */
4601 plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
4602 / elf_mn10300_sizeof_plt (info));
4604 /* Get the offset into the .got table of the entry that
4605 corresponds to this function. Each .got entry is 4 bytes.
4606 The first three are reserved. */
4607 got_offset = (plt_index + 3) * 4;
4609 /* Fill in the entry in the procedure linkage table. */
4610 if (! info->shared)
4612 memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
4613 elf_mn10300_sizeof_plt (info));
4614 bfd_put_32 (output_bfd,
4615 (sgot->output_section->vma
4616 + sgot->output_offset
4617 + got_offset),
4618 (splt->contents + h->plt.offset
4619 + elf_mn10300_plt_symbol_offset (info)));
4621 bfd_put_32 (output_bfd,
4622 (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
4623 (splt->contents + h->plt.offset
4624 + elf_mn10300_plt_plt0_offset (info)));
4626 else
4628 memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
4629 elf_mn10300_sizeof_plt (info));
4631 bfd_put_32 (output_bfd, got_offset,
4632 (splt->contents + h->plt.offset
4633 + elf_mn10300_plt_symbol_offset (info)));
4636 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4637 (splt->contents + h->plt.offset
4638 + elf_mn10300_plt_reloc_offset (info)));
4640 /* Fill in the entry in the global offset table. */
4641 bfd_put_32 (output_bfd,
4642 (splt->output_section->vma
4643 + splt->output_offset
4644 + h->plt.offset
4645 + elf_mn10300_plt_temp_offset (info)),
4646 sgot->contents + got_offset);
4648 /* Fill in the entry in the .rela.plt section. */
4649 rel.r_offset = (sgot->output_section->vma
4650 + sgot->output_offset
4651 + got_offset);
4652 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
4653 rel.r_addend = 0;
4654 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4655 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4656 + plt_index));
4658 if (!h->def_regular)
4659 /* Mark the symbol as undefined, rather than as defined in
4660 the .plt section. Leave the value alone. */
4661 sym->st_shndx = SHN_UNDEF;
4664 if (h->got.offset != (bfd_vma) -1)
4666 asection * sgot;
4667 asection * srel;
4668 Elf_Internal_Rela rel;
4670 /* This symbol has an entry in the global offset table. Set it up. */
4671 sgot = bfd_get_section_by_name (dynobj, ".got");
4672 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4673 BFD_ASSERT (sgot != NULL && srel != NULL);
4675 rel.r_offset = (sgot->output_section->vma
4676 + sgot->output_offset
4677 + (h->got.offset & ~1));
4679 /* If this is a -Bsymbolic link, and the symbol is defined
4680 locally, we just want to emit a RELATIVE reloc. Likewise if
4681 the symbol was forced to be local because of a version file.
4682 The entry in the global offset table will already have been
4683 initialized in the relocate_section function. */
4684 if (info->shared
4685 && (info->symbolic || h->dynindx == -1)
4686 && h->def_regular)
4688 rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
4689 rel.r_addend = (h->root.u.def.value
4690 + h->root.u.def.section->output_section->vma
4691 + h->root.u.def.section->output_offset);
4693 else
4695 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4696 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
4697 rel.r_addend = 0;
4700 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4701 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4702 + srel->reloc_count));
4703 ++ srel->reloc_count;
4706 if (h->needs_copy)
4708 asection * s;
4709 Elf_Internal_Rela rel;
4711 /* This symbol needs a copy reloc. Set it up. */
4712 BFD_ASSERT (h->dynindx != -1
4713 && (h->root.type == bfd_link_hash_defined
4714 || h->root.type == bfd_link_hash_defweak));
4716 s = bfd_get_section_by_name (h->root.u.def.section->owner,
4717 ".rela.bss");
4718 BFD_ASSERT (s != NULL);
4720 rel.r_offset = (h->root.u.def.value
4721 + h->root.u.def.section->output_section->vma
4722 + h->root.u.def.section->output_offset);
4723 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
4724 rel.r_addend = 0;
4725 bfd_elf32_swap_reloca_out (output_bfd, & rel,
4726 (bfd_byte *) ((Elf32_External_Rela *) s->contents
4727 + s->reloc_count));
4728 ++ s->reloc_count;
4731 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4732 if (streq (h->root.root.string, "_DYNAMIC")
4733 || h == elf_hash_table (info)->hgot)
4734 sym->st_shndx = SHN_ABS;
4736 return TRUE;
4739 /* Finish up the dynamic sections. */
4741 static bfd_boolean
4742 _bfd_mn10300_elf_finish_dynamic_sections (bfd * output_bfd,
4743 struct bfd_link_info * info)
4745 bfd * dynobj;
4746 asection * sgot;
4747 asection * sdyn;
4749 dynobj = elf_hash_table (info)->dynobj;
4751 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4752 BFD_ASSERT (sgot != NULL);
4753 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4755 if (elf_hash_table (info)->dynamic_sections_created)
4757 asection * splt;
4758 Elf32_External_Dyn * dyncon;
4759 Elf32_External_Dyn * dynconend;
4761 BFD_ASSERT (sdyn != NULL);
4763 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4764 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4766 for (; dyncon < dynconend; dyncon++)
4768 Elf_Internal_Dyn dyn;
4769 const char * name;
4770 asection * s;
4772 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4774 switch (dyn.d_tag)
4776 default:
4777 break;
4779 case DT_PLTGOT:
4780 name = ".got";
4781 goto get_vma;
4783 case DT_JMPREL:
4784 name = ".rela.plt";
4785 get_vma:
4786 s = bfd_get_section_by_name (output_bfd, name);
4787 BFD_ASSERT (s != NULL);
4788 dyn.d_un.d_ptr = s->vma;
4789 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4790 break;
4792 case DT_PLTRELSZ:
4793 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4794 BFD_ASSERT (s != NULL);
4795 dyn.d_un.d_val = s->size;
4796 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4797 break;
4799 case DT_RELASZ:
4800 /* My reading of the SVR4 ABI indicates that the
4801 procedure linkage table relocs (DT_JMPREL) should be
4802 included in the overall relocs (DT_RELA). This is
4803 what Solaris does. However, UnixWare can not handle
4804 that case. Therefore, we override the DT_RELASZ entry
4805 here to make it not include the JMPREL relocs. Since
4806 the linker script arranges for .rela.plt to follow all
4807 other relocation sections, we don't have to worry
4808 about changing the DT_RELA entry. */
4809 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4810 if (s != NULL)
4811 dyn.d_un.d_val -= s->size;
4812 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4813 break;
4817 /* Fill in the first entry in the procedure linkage table. */
4818 splt = bfd_get_section_by_name (dynobj, ".plt");
4819 if (splt && splt->size > 0)
4821 if (info->shared)
4823 memcpy (splt->contents, elf_mn10300_pic_plt_entry,
4824 elf_mn10300_sizeof_plt (info));
4826 else
4828 memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
4829 bfd_put_32 (output_bfd,
4830 sgot->output_section->vma + sgot->output_offset + 4,
4831 splt->contents + elf_mn10300_plt0_gotid_offset (info));
4832 bfd_put_32 (output_bfd,
4833 sgot->output_section->vma + sgot->output_offset + 8,
4834 splt->contents + elf_mn10300_plt0_linker_offset (info));
4837 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4838 really seem like the right value. */
4839 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4843 /* Fill in the first three entries in the global offset table. */
4844 if (sgot->size > 0)
4846 if (sdyn == NULL)
4847 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4848 else
4849 bfd_put_32 (output_bfd,
4850 sdyn->output_section->vma + sdyn->output_offset,
4851 sgot->contents);
4852 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4853 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4856 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4858 return TRUE;
4861 /* Classify relocation types, such that combreloc can sort them
4862 properly. */
4864 static enum elf_reloc_type_class
4865 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
4867 switch ((int) ELF32_R_TYPE (rela->r_info))
4869 case R_MN10300_RELATIVE: return reloc_class_relative;
4870 case R_MN10300_JMP_SLOT: return reloc_class_plt;
4871 case R_MN10300_COPY: return reloc_class_copy;
4872 default: return reloc_class_normal;
4876 #ifndef ELF_ARCH
4877 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
4878 #define TARGET_LITTLE_NAME "elf32-mn10300"
4879 #define ELF_ARCH bfd_arch_mn10300
4880 #define ELF_MACHINE_CODE EM_MN10300
4881 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
4882 #define ELF_MAXPAGESIZE 0x1000
4883 #endif
4885 #define elf_info_to_howto mn10300_info_to_howto
4886 #define elf_info_to_howto_rel 0
4887 #define elf_backend_can_gc_sections 1
4888 #define elf_backend_rela_normal 1
4889 #define elf_backend_check_relocs mn10300_elf_check_relocs
4890 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
4891 #define elf_backend_relocate_section mn10300_elf_relocate_section
4892 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
4893 #define bfd_elf32_bfd_get_relocated_section_contents \
4894 mn10300_elf_get_relocated_section_contents
4895 #define bfd_elf32_bfd_link_hash_table_create \
4896 elf32_mn10300_link_hash_table_create
4897 #define bfd_elf32_bfd_link_hash_table_free \
4898 elf32_mn10300_link_hash_table_free
4900 #ifndef elf_symbol_leading_char
4901 #define elf_symbol_leading_char '_'
4902 #endif
4904 /* So we can set bits in e_flags. */
4905 #define elf_backend_final_write_processing \
4906 _bfd_mn10300_elf_final_write_processing
4907 #define elf_backend_object_p _bfd_mn10300_elf_object_p
4909 #define bfd_elf32_bfd_merge_private_bfd_data \
4910 _bfd_mn10300_elf_merge_private_bfd_data
4912 #define elf_backend_can_gc_sections 1
4913 #define elf_backend_create_dynamic_sections \
4914 _bfd_mn10300_elf_create_dynamic_sections
4915 #define elf_backend_adjust_dynamic_symbol \
4916 _bfd_mn10300_elf_adjust_dynamic_symbol
4917 #define elf_backend_size_dynamic_sections \
4918 _bfd_mn10300_elf_size_dynamic_sections
4919 #define elf_backend_omit_section_dynsym \
4920 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4921 #define elf_backend_finish_dynamic_symbol \
4922 _bfd_mn10300_elf_finish_dynamic_symbol
4923 #define elf_backend_finish_dynamic_sections \
4924 _bfd_mn10300_elf_finish_dynamic_sections
4926 #define elf_backend_reloc_type_class \
4927 _bfd_mn10300_elf_reloc_type_class
4929 #define elf_backend_want_got_plt 1
4930 #define elf_backend_plt_readonly 1
4931 #define elf_backend_want_plt_sym 0
4932 #define elf_backend_got_header_size 12
4934 #include "elf32-target.h"