1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright (C) 1996-2019 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
25 #include "elf/mn10300.h"
26 #include "libiberty.h"
28 /* The mn10300 linker needs to keep track of the number of relocs that
29 it decides to copy in check_relocs for each symbol. This is so
30 that it can discard PC relative relocs if it doesn't need them when
31 linking with -Bsymbolic. We store the information in a field
32 extending the regular ELF linker hash table. */
34 struct elf32_mn10300_link_hash_entry
36 /* The basic elf link hash table entry. */
37 struct elf_link_hash_entry root
;
39 /* For function symbols, the number of times this function is
40 called directly (ie by name). */
41 unsigned int direct_calls
;
43 /* For function symbols, the size of this function's stack
44 (if <= 255 bytes). We stuff this into "call" instructions
45 to this target when it's valid and profitable to do so.
47 This does not include stack allocated by movm! */
48 unsigned char stack_size
;
50 /* For function symbols, arguments (if any) for movm instruction
51 in the prologue. We stuff this value into "call" instructions
52 to the target when it's valid and profitable to do so. */
53 unsigned char movm_args
;
55 /* For function symbols, the amount of stack space that would be allocated
56 by the movm instruction. This is redundant with movm_args, but we
57 add it to the hash table to avoid computing it over and over. */
58 unsigned char movm_stack_size
;
60 /* When set, convert all "call" instructions to this target into "calls"
62 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
64 /* Used to mark functions which have had redundant parts of their
66 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
69 /* Calculated value. */
77 /* Used to distinguish GOT entries for TLS types from normal GOT entries. */
78 unsigned char tls_type
;
81 /* We derive a hash table from the main elf linker hash table so
82 we can store state variables and a secondary hash table without
83 resorting to global variables. */
84 struct elf32_mn10300_link_hash_table
86 /* The main hash table. */
87 struct elf_link_hash_table root
;
89 /* A hash table for static functions. We could derive a new hash table
90 instead of using the full elf32_mn10300_link_hash_table if we wanted
91 to save some memory. */
92 struct elf32_mn10300_link_hash_table
*static_hash_table
;
94 /* Random linker state flags. */
95 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
99 bfd_signed_vma refcount
;
106 #define elf_mn10300_hash_entry(ent) ((struct elf32_mn10300_link_hash_entry *)(ent))
108 struct elf_mn10300_obj_tdata
110 struct elf_obj_tdata root
;
112 /* tls_type for each local got entry. */
113 char * local_got_tls_type
;
116 #define elf_mn10300_tdata(abfd) \
117 ((struct elf_mn10300_obj_tdata *) (abfd)->tdata.any)
119 #define elf_mn10300_local_got_tls_type(abfd) \
120 (elf_mn10300_tdata (abfd)->local_got_tls_type)
123 #define streq(a, b) (strcmp ((a),(b)) == 0)
126 /* For MN10300 linker hash table. */
128 /* Get the MN10300 ELF linker hash table from a link_info structure. */
130 #define elf32_mn10300_hash_table(p) \
131 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
132 == MN10300_ELF_DATA ? ((struct elf32_mn10300_link_hash_table *) ((p)->hash)) : NULL)
134 #define elf32_mn10300_link_hash_traverse(table, func, info) \
135 (elf_link_hash_traverse \
137 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
140 static reloc_howto_type elf_mn10300_howto_table
[] =
142 /* Dummy relocation. Does nothing. */
143 HOWTO (R_MN10300_NONE
,
149 complain_overflow_dont
,
150 bfd_elf_generic_reloc
,
156 /* Standard 32 bit reloc. */
163 complain_overflow_bitfield
,
164 bfd_elf_generic_reloc
,
170 /* Standard 16 bit reloc. */
177 complain_overflow_bitfield
,
178 bfd_elf_generic_reloc
,
184 /* Standard 8 bit reloc. */
191 complain_overflow_bitfield
,
192 bfd_elf_generic_reloc
,
198 /* Standard 32bit pc-relative reloc. */
199 HOWTO (R_MN10300_PCREL32
,
205 complain_overflow_bitfield
,
206 bfd_elf_generic_reloc
,
212 /* Standard 16bit pc-relative reloc. */
213 HOWTO (R_MN10300_PCREL16
,
219 complain_overflow_bitfield
,
220 bfd_elf_generic_reloc
,
226 /* Standard 8 pc-relative reloc. */
227 HOWTO (R_MN10300_PCREL8
,
233 complain_overflow_bitfield
,
234 bfd_elf_generic_reloc
,
241 /* GNU extension to record C++ vtable hierarchy. */
242 HOWTO (R_MN10300_GNU_VTINHERIT
, /* type */
244 0, /* size (0 = byte, 1 = short, 2 = long) */
246 FALSE
, /* pc_relative */
248 complain_overflow_dont
, /* complain_on_overflow */
249 NULL
, /* special_function */
250 "R_MN10300_GNU_VTINHERIT", /* name */
251 FALSE
, /* partial_inplace */
254 FALSE
), /* pcrel_offset */
256 /* GNU extension to record C++ vtable member usage */
257 HOWTO (R_MN10300_GNU_VTENTRY
, /* type */
259 0, /* size (0 = byte, 1 = short, 2 = long) */
261 FALSE
, /* pc_relative */
263 complain_overflow_dont
, /* complain_on_overflow */
264 NULL
, /* special_function */
265 "R_MN10300_GNU_VTENTRY", /* name */
266 FALSE
, /* partial_inplace */
269 FALSE
), /* pcrel_offset */
271 /* Standard 24 bit reloc. */
278 complain_overflow_bitfield
,
279 bfd_elf_generic_reloc
,
285 HOWTO (R_MN10300_GOTPC32
, /* type */
287 2, /* size (0 = byte, 1 = short, 2 = long) */
289 TRUE
, /* pc_relative */
291 complain_overflow_bitfield
, /* complain_on_overflow */
292 bfd_elf_generic_reloc
, /* */
293 "R_MN10300_GOTPC32", /* name */
294 FALSE
, /* partial_inplace */
295 0xffffffff, /* src_mask */
296 0xffffffff, /* dst_mask */
297 TRUE
), /* pcrel_offset */
299 HOWTO (R_MN10300_GOTPC16
, /* type */
301 1, /* size (0 = byte, 1 = short, 2 = long) */
303 TRUE
, /* pc_relative */
305 complain_overflow_bitfield
, /* complain_on_overflow */
306 bfd_elf_generic_reloc
, /* */
307 "R_MN10300_GOTPC16", /* name */
308 FALSE
, /* partial_inplace */
309 0xffff, /* src_mask */
310 0xffff, /* dst_mask */
311 TRUE
), /* pcrel_offset */
313 HOWTO (R_MN10300_GOTOFF32
, /* type */
315 2, /* size (0 = byte, 1 = short, 2 = long) */
317 FALSE
, /* pc_relative */
319 complain_overflow_bitfield
, /* complain_on_overflow */
320 bfd_elf_generic_reloc
, /* */
321 "R_MN10300_GOTOFF32", /* name */
322 FALSE
, /* partial_inplace */
323 0xffffffff, /* src_mask */
324 0xffffffff, /* dst_mask */
325 FALSE
), /* pcrel_offset */
327 HOWTO (R_MN10300_GOTOFF24
, /* type */
329 2, /* size (0 = byte, 1 = short, 2 = long) */
331 FALSE
, /* pc_relative */
333 complain_overflow_bitfield
, /* complain_on_overflow */
334 bfd_elf_generic_reloc
, /* */
335 "R_MN10300_GOTOFF24", /* name */
336 FALSE
, /* partial_inplace */
337 0xffffff, /* src_mask */
338 0xffffff, /* dst_mask */
339 FALSE
), /* pcrel_offset */
341 HOWTO (R_MN10300_GOTOFF16
, /* type */
343 1, /* size (0 = byte, 1 = short, 2 = long) */
345 FALSE
, /* pc_relative */
347 complain_overflow_bitfield
, /* complain_on_overflow */
348 bfd_elf_generic_reloc
, /* */
349 "R_MN10300_GOTOFF16", /* name */
350 FALSE
, /* partial_inplace */
351 0xffff, /* src_mask */
352 0xffff, /* dst_mask */
353 FALSE
), /* pcrel_offset */
355 HOWTO (R_MN10300_PLT32
, /* type */
357 2, /* size (0 = byte, 1 = short, 2 = long) */
359 TRUE
, /* pc_relative */
361 complain_overflow_bitfield
, /* complain_on_overflow */
362 bfd_elf_generic_reloc
, /* */
363 "R_MN10300_PLT32", /* name */
364 FALSE
, /* partial_inplace */
365 0xffffffff, /* src_mask */
366 0xffffffff, /* dst_mask */
367 TRUE
), /* pcrel_offset */
369 HOWTO (R_MN10300_PLT16
, /* type */
371 1, /* size (0 = byte, 1 = short, 2 = long) */
373 TRUE
, /* pc_relative */
375 complain_overflow_bitfield
, /* complain_on_overflow */
376 bfd_elf_generic_reloc
, /* */
377 "R_MN10300_PLT16", /* name */
378 FALSE
, /* partial_inplace */
379 0xffff, /* src_mask */
380 0xffff, /* dst_mask */
381 TRUE
), /* pcrel_offset */
383 HOWTO (R_MN10300_GOT32
, /* type */
385 2, /* size (0 = byte, 1 = short, 2 = long) */
387 FALSE
, /* pc_relative */
389 complain_overflow_bitfield
, /* complain_on_overflow */
390 bfd_elf_generic_reloc
, /* */
391 "R_MN10300_GOT32", /* name */
392 FALSE
, /* partial_inplace */
393 0xffffffff, /* src_mask */
394 0xffffffff, /* dst_mask */
395 FALSE
), /* pcrel_offset */
397 HOWTO (R_MN10300_GOT24
, /* type */
399 2, /* size (0 = byte, 1 = short, 2 = long) */
401 FALSE
, /* pc_relative */
403 complain_overflow_bitfield
, /* complain_on_overflow */
404 bfd_elf_generic_reloc
, /* */
405 "R_MN10300_GOT24", /* name */
406 FALSE
, /* partial_inplace */
407 0xffffffff, /* src_mask */
408 0xffffffff, /* dst_mask */
409 FALSE
), /* pcrel_offset */
411 HOWTO (R_MN10300_GOT16
, /* type */
413 1, /* size (0 = byte, 1 = short, 2 = long) */
415 FALSE
, /* pc_relative */
417 complain_overflow_bitfield
, /* complain_on_overflow */
418 bfd_elf_generic_reloc
, /* */
419 "R_MN10300_GOT16", /* name */
420 FALSE
, /* partial_inplace */
421 0xffffffff, /* src_mask */
422 0xffffffff, /* dst_mask */
423 FALSE
), /* pcrel_offset */
425 HOWTO (R_MN10300_COPY
, /* type */
427 2, /* size (0 = byte, 1 = short, 2 = long) */
429 FALSE
, /* pc_relative */
431 complain_overflow_bitfield
, /* complain_on_overflow */
432 bfd_elf_generic_reloc
, /* */
433 "R_MN10300_COPY", /* name */
434 FALSE
, /* partial_inplace */
435 0xffffffff, /* src_mask */
436 0xffffffff, /* dst_mask */
437 FALSE
), /* pcrel_offset */
439 HOWTO (R_MN10300_GLOB_DAT
, /* type */
441 2, /* size (0 = byte, 1 = short, 2 = long) */
443 FALSE
, /* pc_relative */
445 complain_overflow_bitfield
, /* complain_on_overflow */
446 bfd_elf_generic_reloc
, /* */
447 "R_MN10300_GLOB_DAT", /* name */
448 FALSE
, /* partial_inplace */
449 0xffffffff, /* src_mask */
450 0xffffffff, /* dst_mask */
451 FALSE
), /* pcrel_offset */
453 HOWTO (R_MN10300_JMP_SLOT
, /* type */
455 2, /* size (0 = byte, 1 = short, 2 = long) */
457 FALSE
, /* pc_relative */
459 complain_overflow_bitfield
, /* complain_on_overflow */
460 bfd_elf_generic_reloc
, /* */
461 "R_MN10300_JMP_SLOT", /* name */
462 FALSE
, /* partial_inplace */
463 0xffffffff, /* src_mask */
464 0xffffffff, /* dst_mask */
465 FALSE
), /* pcrel_offset */
467 HOWTO (R_MN10300_RELATIVE
, /* type */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
471 FALSE
, /* pc_relative */
473 complain_overflow_bitfield
, /* complain_on_overflow */
474 bfd_elf_generic_reloc
, /* */
475 "R_MN10300_RELATIVE", /* name */
476 FALSE
, /* partial_inplace */
477 0xffffffff, /* src_mask */
478 0xffffffff, /* dst_mask */
479 FALSE
), /* pcrel_offset */
481 HOWTO (R_MN10300_TLS_GD
, /* type */
483 2, /* size (0 = byte, 1 = short, 2 = long) */
485 FALSE
, /* pc_relative */
487 complain_overflow_bitfield
, /* complain_on_overflow */
488 bfd_elf_generic_reloc
, /* */
489 "R_MN10300_TLS_GD", /* name */
490 FALSE
, /* partial_inplace */
491 0xffffffff, /* src_mask */
492 0xffffffff, /* dst_mask */
493 FALSE
), /* pcrel_offset */
495 HOWTO (R_MN10300_TLS_LD
, /* type */
497 2, /* size (0 = byte, 1 = short, 2 = long) */
499 FALSE
, /* pc_relative */
501 complain_overflow_bitfield
, /* complain_on_overflow */
502 bfd_elf_generic_reloc
, /* */
503 "R_MN10300_TLS_LD", /* name */
504 FALSE
, /* partial_inplace */
505 0xffffffff, /* src_mask */
506 0xffffffff, /* dst_mask */
507 FALSE
), /* pcrel_offset */
509 HOWTO (R_MN10300_TLS_LDO
, /* type */
511 2, /* size (0 = byte, 1 = short, 2 = long) */
513 FALSE
, /* pc_relative */
515 complain_overflow_bitfield
, /* complain_on_overflow */
516 bfd_elf_generic_reloc
, /* */
517 "R_MN10300_TLS_LDO", /* name */
518 FALSE
, /* partial_inplace */
519 0xffffffff, /* src_mask */
520 0xffffffff, /* dst_mask */
521 FALSE
), /* pcrel_offset */
523 HOWTO (R_MN10300_TLS_GOTIE
, /* type */
525 2, /* size (0 = byte, 1 = short, 2 = long) */
527 FALSE
, /* pc_relative */
529 complain_overflow_bitfield
, /* complain_on_overflow */
530 bfd_elf_generic_reloc
, /* */
531 "R_MN10300_TLS_GOTIE", /* name */
532 FALSE
, /* partial_inplace */
533 0xffffffff, /* src_mask */
534 0xffffffff, /* dst_mask */
535 FALSE
), /* pcrel_offset */
537 HOWTO (R_MN10300_TLS_IE
, /* type */
539 2, /* size (0 = byte, 1 = short, 2 = long) */
541 FALSE
, /* pc_relative */
543 complain_overflow_bitfield
, /* complain_on_overflow */
544 bfd_elf_generic_reloc
, /* */
545 "R_MN10300_TLS_IE", /* name */
546 FALSE
, /* partial_inplace */
547 0xffffffff, /* src_mask */
548 0xffffffff, /* dst_mask */
549 FALSE
), /* pcrel_offset */
551 HOWTO (R_MN10300_TLS_LE
, /* type */
553 2, /* size (0 = byte, 1 = short, 2 = long) */
555 FALSE
, /* pc_relative */
557 complain_overflow_bitfield
, /* complain_on_overflow */
558 bfd_elf_generic_reloc
, /* */
559 "R_MN10300_TLS_LE", /* name */
560 FALSE
, /* partial_inplace */
561 0xffffffff, /* src_mask */
562 0xffffffff, /* dst_mask */
563 FALSE
), /* pcrel_offset */
565 HOWTO (R_MN10300_TLS_DTPMOD
, /* type */
567 2, /* size (0 = byte, 1 = short, 2 = long) */
569 FALSE
, /* pc_relative */
571 complain_overflow_bitfield
, /* complain_on_overflow */
572 bfd_elf_generic_reloc
, /* */
573 "R_MN10300_TLS_DTPMOD", /* name */
574 FALSE
, /* partial_inplace */
575 0xffffffff, /* src_mask */
576 0xffffffff, /* dst_mask */
577 FALSE
), /* pcrel_offset */
579 HOWTO (R_MN10300_TLS_DTPOFF
, /* type */
581 2, /* size (0 = byte, 1 = short, 2 = long) */
583 FALSE
, /* pc_relative */
585 complain_overflow_bitfield
, /* complain_on_overflow */
586 bfd_elf_generic_reloc
, /* */
587 "R_MN10300_TLS_DTPOFF", /* name */
588 FALSE
, /* partial_inplace */
589 0xffffffff, /* src_mask */
590 0xffffffff, /* dst_mask */
591 FALSE
), /* pcrel_offset */
593 HOWTO (R_MN10300_TLS_TPOFF
, /* type */
595 2, /* size (0 = byte, 1 = short, 2 = long) */
597 FALSE
, /* pc_relative */
599 complain_overflow_bitfield
, /* complain_on_overflow */
600 bfd_elf_generic_reloc
, /* */
601 "R_MN10300_TLS_TPOFF", /* name */
602 FALSE
, /* partial_inplace */
603 0xffffffff, /* src_mask */
604 0xffffffff, /* dst_mask */
605 FALSE
), /* pcrel_offset */
607 HOWTO (R_MN10300_SYM_DIFF
, /* type */
609 2, /* size (0 = byte, 1 = short, 2 = long) */
611 FALSE
, /* pc_relative */
613 complain_overflow_dont
,/* complain_on_overflow */
614 NULL
, /* special handler. */
615 "R_MN10300_SYM_DIFF", /* name */
616 FALSE
, /* partial_inplace */
617 0xffffffff, /* src_mask */
618 0xffffffff, /* dst_mask */
619 FALSE
), /* pcrel_offset */
621 HOWTO (R_MN10300_ALIGN
, /* type */
623 0, /* size (0 = byte, 1 = short, 2 = long) */
625 FALSE
, /* pc_relative */
627 complain_overflow_dont
,/* complain_on_overflow */
628 NULL
, /* special handler. */
629 "R_MN10300_ALIGN", /* name */
630 FALSE
, /* partial_inplace */
633 FALSE
) /* pcrel_offset */
636 struct mn10300_reloc_map
638 bfd_reloc_code_real_type bfd_reloc_val
;
639 unsigned char elf_reloc_val
;
642 static const struct mn10300_reloc_map mn10300_reloc_map
[] =
644 { BFD_RELOC_NONE
, R_MN10300_NONE
, },
645 { BFD_RELOC_32
, R_MN10300_32
, },
646 { BFD_RELOC_16
, R_MN10300_16
, },
647 { BFD_RELOC_8
, R_MN10300_8
, },
648 { BFD_RELOC_32_PCREL
, R_MN10300_PCREL32
, },
649 { BFD_RELOC_16_PCREL
, R_MN10300_PCREL16
, },
650 { BFD_RELOC_8_PCREL
, R_MN10300_PCREL8
, },
651 { BFD_RELOC_24
, R_MN10300_24
, },
652 { BFD_RELOC_VTABLE_INHERIT
, R_MN10300_GNU_VTINHERIT
},
653 { BFD_RELOC_VTABLE_ENTRY
, R_MN10300_GNU_VTENTRY
},
654 { BFD_RELOC_32_GOT_PCREL
, R_MN10300_GOTPC32
},
655 { BFD_RELOC_16_GOT_PCREL
, R_MN10300_GOTPC16
},
656 { BFD_RELOC_32_GOTOFF
, R_MN10300_GOTOFF32
},
657 { BFD_RELOC_MN10300_GOTOFF24
, R_MN10300_GOTOFF24
},
658 { BFD_RELOC_16_GOTOFF
, R_MN10300_GOTOFF16
},
659 { BFD_RELOC_32_PLT_PCREL
, R_MN10300_PLT32
},
660 { BFD_RELOC_16_PLT_PCREL
, R_MN10300_PLT16
},
661 { BFD_RELOC_MN10300_GOT32
, R_MN10300_GOT32
},
662 { BFD_RELOC_MN10300_GOT24
, R_MN10300_GOT24
},
663 { BFD_RELOC_MN10300_GOT16
, R_MN10300_GOT16
},
664 { BFD_RELOC_MN10300_COPY
, R_MN10300_COPY
},
665 { BFD_RELOC_MN10300_GLOB_DAT
, R_MN10300_GLOB_DAT
},
666 { BFD_RELOC_MN10300_JMP_SLOT
, R_MN10300_JMP_SLOT
},
667 { BFD_RELOC_MN10300_RELATIVE
, R_MN10300_RELATIVE
},
668 { BFD_RELOC_MN10300_TLS_GD
, R_MN10300_TLS_GD
},
669 { BFD_RELOC_MN10300_TLS_LD
, R_MN10300_TLS_LD
},
670 { BFD_RELOC_MN10300_TLS_LDO
, R_MN10300_TLS_LDO
},
671 { BFD_RELOC_MN10300_TLS_GOTIE
, R_MN10300_TLS_GOTIE
},
672 { BFD_RELOC_MN10300_TLS_IE
, R_MN10300_TLS_IE
},
673 { BFD_RELOC_MN10300_TLS_LE
, R_MN10300_TLS_LE
},
674 { BFD_RELOC_MN10300_TLS_DTPMOD
, R_MN10300_TLS_DTPMOD
},
675 { BFD_RELOC_MN10300_TLS_DTPOFF
, R_MN10300_TLS_DTPOFF
},
676 { BFD_RELOC_MN10300_TLS_TPOFF
, R_MN10300_TLS_TPOFF
},
677 { BFD_RELOC_MN10300_SYM_DIFF
, R_MN10300_SYM_DIFF
},
678 { BFD_RELOC_MN10300_ALIGN
, R_MN10300_ALIGN
}
681 /* Create the GOT section. */
684 _bfd_mn10300_elf_create_got_section (bfd
* abfd
,
685 struct bfd_link_info
* info
)
690 struct elf_link_hash_entry
* h
;
691 const struct elf_backend_data
* bed
= get_elf_backend_data (abfd
);
692 struct elf_link_hash_table
*htab
;
695 /* This function may be called more than once. */
696 htab
= elf_hash_table (info
);
697 if (htab
->sgot
!= NULL
)
700 switch (bed
->s
->arch_size
)
711 bfd_set_error (bfd_error_bad_value
);
715 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
716 | SEC_LINKER_CREATED
);
719 pltflags
|= SEC_CODE
;
720 if (bed
->plt_not_loaded
)
721 pltflags
&= ~ (SEC_LOAD
| SEC_HAS_CONTENTS
);
722 if (bed
->plt_readonly
)
723 pltflags
|= SEC_READONLY
;
725 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
728 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
731 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
733 if (bed
->want_plt_sym
)
735 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
736 "_PROCEDURE_LINKAGE_TABLE_");
742 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
745 || !bfd_set_section_alignment (s
, ptralign
))
748 if (bed
->want_got_plt
)
750 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
753 || !bfd_set_section_alignment (s
, ptralign
))
757 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
758 (or .got.plt) section. We don't do this in the linker script
759 because we don't want to define the symbol if we are not creating
760 a global offset table. */
761 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_GLOBAL_OFFSET_TABLE_");
766 /* The first bit of the global offset table is the header. */
767 s
->size
+= bed
->got_header_size
;
772 static reloc_howto_type
*
773 bfd_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
774 bfd_reloc_code_real_type code
)
778 for (i
= ARRAY_SIZE (mn10300_reloc_map
); i
--;)
779 if (mn10300_reloc_map
[i
].bfd_reloc_val
== code
)
780 return &elf_mn10300_howto_table
[mn10300_reloc_map
[i
].elf_reloc_val
];
785 static reloc_howto_type
*
786 bfd_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
791 for (i
= ARRAY_SIZE (elf_mn10300_howto_table
); i
--;)
792 if (elf_mn10300_howto_table
[i
].name
!= NULL
793 && strcasecmp (elf_mn10300_howto_table
[i
].name
, r_name
) == 0)
794 return elf_mn10300_howto_table
+ i
;
799 /* Set the howto pointer for an MN10300 ELF reloc. */
802 mn10300_info_to_howto (bfd
*abfd
,
804 Elf_Internal_Rela
*dst
)
808 r_type
= ELF32_R_TYPE (dst
->r_info
);
809 if (r_type
>= R_MN10300_MAX
)
811 /* xgettext:c-format */
812 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
814 bfd_set_error (bfd_error_bad_value
);
817 cache_ptr
->howto
= elf_mn10300_howto_table
+ r_type
;
822 elf_mn10300_tls_transition (struct bfd_link_info
* info
,
824 struct elf_link_hash_entry
* h
,
826 bfd_boolean counting
)
828 bfd_boolean is_local
;
830 if (r_type
== R_MN10300_TLS_GD
832 && elf_mn10300_hash_entry (h
)->tls_type
== GOT_TLS_IE
)
833 return R_MN10300_TLS_GOTIE
;
835 if (bfd_link_pic (info
))
838 if (! (sec
->flags
& SEC_CODE
))
841 if (! counting
&& h
!= NULL
&& ! elf_hash_table (info
)->dynamic_sections_created
)
844 is_local
= SYMBOL_CALLS_LOCAL (info
, h
);
846 /* For the main program, these are the transitions we do. */
849 case R_MN10300_TLS_GD
: return is_local
? R_MN10300_TLS_LE
: R_MN10300_TLS_GOTIE
;
850 case R_MN10300_TLS_LD
: return R_MN10300_NONE
;
851 case R_MN10300_TLS_LDO
: return R_MN10300_TLS_LE
;
852 case R_MN10300_TLS_IE
:
853 case R_MN10300_TLS_GOTIE
: return is_local
? R_MN10300_TLS_LE
: r_type
;
859 /* Return the relocation value for @tpoff relocation
860 if STT_TLS virtual address is ADDRESS. */
863 dtpoff (struct bfd_link_info
* info
, bfd_vma address
)
865 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
867 /* If tls_sec is NULL, we should have signalled an error already. */
868 if (htab
->tls_sec
== NULL
)
870 return address
- htab
->tls_sec
->vma
;
873 /* Return the relocation value for @tpoff relocation
874 if STT_TLS virtual address is ADDRESS. */
877 tpoff (struct bfd_link_info
* info
, bfd_vma address
)
879 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
881 /* If tls_sec is NULL, we should have signalled an error already. */
882 if (htab
->tls_sec
== NULL
)
884 return address
- (htab
->tls_size
+ htab
->tls_sec
->vma
);
887 /* Returns nonzero if there's a R_MN10300_PLT32 reloc that we now need
888 to skip, after this one. The actual value is the offset between
889 this reloc and the PLT reloc. */
892 mn10300_do_tls_transition (bfd
* input_bfd
,
894 unsigned int tls_r_type
,
898 bfd_byte
*op
= contents
+ offset
;
901 #define TLS_PAIR(r1,r2) ((r1) * R_MN10300_MAX + (r2))
903 /* This is common to all GD/LD transitions, so break it out. */
904 if (r_type
== R_MN10300_TLS_GD
905 || r_type
== R_MN10300_TLS_LD
)
909 BFD_ASSERT (bfd_get_8 (input_bfd
, op
) == 0xFC);
910 BFD_ASSERT (bfd_get_8 (input_bfd
, op
+ 1) == 0xCC);
912 BFD_ASSERT (bfd_get_8 (input_bfd
, op
+ 6) == 0xF1);
913 gotreg
= (bfd_get_8 (input_bfd
, op
+ 7) & 0x0c) >> 2;
915 BFD_ASSERT (bfd_get_8 (input_bfd
, op
+ 8) == 0xDD);
918 switch (TLS_PAIR (r_type
, tls_r_type
))
920 case TLS_PAIR (R_MN10300_TLS_GD
, R_MN10300_TLS_GOTIE
):
922 /* Keep track of which register we put GOTptr in. */
923 /* mov (_x@indntpoff,a2),a0. */
924 memcpy (op
, "\xFC\x20\x00\x00\x00\x00", 6);
927 memcpy (op
+6, "\xF9\x78\x28", 3);
928 /* or 0x00000000, d0 - six byte nop. */
929 memcpy (op
+9, "\xFC\xE4\x00\x00\x00\x00", 6);
933 case TLS_PAIR (R_MN10300_TLS_GD
, R_MN10300_TLS_LE
):
935 /* Register is *always* a0. */
936 /* mov _x@tpoff,a0. */
937 memcpy (op
, "\xFC\xDC\x00\x00\x00\x00", 6);
939 memcpy (op
+6, "\xF9\x78\x28", 3);
940 /* or 0x00000000, d0 - six byte nop. */
941 memcpy (op
+9, "\xFC\xE4\x00\x00\x00\x00", 6);
944 case TLS_PAIR (R_MN10300_TLS_LD
, R_MN10300_NONE
):
946 /* Register is *always* a0. */
948 memcpy (op
, "\xF5\x88", 2);
949 /* or 0x00000000, d0 - six byte nop. */
950 memcpy (op
+2, "\xFC\xE4\x00\x00\x00\x00", 6);
951 /* or 0x00000000, e2 - seven byte nop. */
952 memcpy (op
+8, "\xFE\x19\x22\x00\x00\x00\x00", 7);
956 case TLS_PAIR (R_MN10300_TLS_LDO
, R_MN10300_TLS_LE
):
957 /* No changes needed, just the reloc change. */
960 /* These are a little tricky, because we have to detect which
961 opcode is being used (they're different sizes, with the reloc
962 at different offsets within the opcode) and convert each
963 accordingly, copying the operands as needed. The conversions
964 we do are as follows (IE,GOTIE,LE):
966 1111 1100 1010 01Dn [-- abs32 --] MOV (x@indntpoff),Dn
967 1111 1100 0000 DnAm [-- abs32 --] MOV (x@gotntpoff,Am),Dn
968 1111 1100 1100 11Dn [-- abs32 --] MOV x@tpoff,Dn
970 1111 1100 1010 00An [-- abs32 --] MOV (x@indntpoff),An
971 1111 1100 0010 AnAm [-- abs32 --] MOV (x@gotntpoff,Am),An
972 1111 1100 1101 11An [-- abs32 --] MOV x@tpoff,An
974 1111 1110 0000 1110 Rnnn Xxxx [-- abs32 --] MOV (x@indntpoff),Rn
975 1111 1110 0000 1010 Rnnn Rmmm [-- abs32 --] MOV (x@indntpoff,Rm),Rn
976 1111 1110 0000 1000 Rnnn Xxxx [-- abs32 --] MOV x@tpoff,Rn
978 Since the GOT pointer is always $a2, we assume the last
979 normally won't happen, but let's be paranoid and plan for the
980 day that GCC optimizes it somewhow. */
982 case TLS_PAIR (R_MN10300_TLS_IE
, R_MN10300_TLS_LE
):
986 if ((op
[1] & 0xFC) == 0xA4) /* Dn */
988 op
[1] &= 0x03; /* Leaves Dn. */
993 op
[1] &= 0x03; /* Leaves An. */
997 else if (op
[-3] == 0xFE)
1003 case TLS_PAIR (R_MN10300_TLS_GOTIE
, R_MN10300_TLS_LE
):
1007 if ((op
[1] & 0xF0) == 0x00) /* Dn */
1009 op
[1] &= 0x0C; /* Leaves Dn. */
1015 op
[1] &= 0x0C; /* Leaves An. */
1020 else if (op
[-3] == 0xFE)
1028 /* xgettext:c-format */
1029 (_("%pB: unsupported transition from %s to %s"),
1031 elf_mn10300_howto_table
[r_type
].name
,
1032 elf_mn10300_howto_table
[tls_r_type
].name
);
1039 /* Look through the relocs for a section during the first phase.
1040 Since we don't do .gots or .plts, we just need to consider the
1041 virtual table relocs for gc. */
1044 mn10300_elf_check_relocs (bfd
*abfd
,
1045 struct bfd_link_info
*info
,
1047 const Elf_Internal_Rela
*relocs
)
1049 struct elf32_mn10300_link_hash_table
* htab
= elf32_mn10300_hash_table (info
);
1050 bfd_boolean sym_diff_reloc_seen
;
1051 Elf_Internal_Shdr
*symtab_hdr
;
1052 Elf_Internal_Sym
* isymbuf
= NULL
;
1053 struct elf_link_hash_entry
**sym_hashes
;
1054 const Elf_Internal_Rela
*rel
;
1055 const Elf_Internal_Rela
*rel_end
;
1057 bfd_vma
* local_got_offsets
;
1061 bfd_boolean result
= FALSE
;
1067 if (bfd_link_relocatable (info
))
1070 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1071 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1072 sym_hashes
= elf_sym_hashes (abfd
);
1074 dynobj
= elf_hash_table (info
)->dynobj
;
1075 local_got_offsets
= elf_local_got_offsets (abfd
);
1076 rel_end
= relocs
+ sec
->reloc_count
;
1077 sym_diff_reloc_seen
= FALSE
;
1079 for (rel
= relocs
; rel
< rel_end
; rel
++)
1081 struct elf_link_hash_entry
*h
;
1082 unsigned long r_symndx
;
1083 unsigned int r_type
;
1084 int tls_type
= GOT_NORMAL
;
1086 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1087 if (r_symndx
< symtab_hdr
->sh_info
)
1091 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1092 while (h
->root
.type
== bfd_link_hash_indirect
1093 || h
->root
.type
== bfd_link_hash_warning
)
1094 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1097 r_type
= ELF32_R_TYPE (rel
->r_info
);
1098 r_type
= elf_mn10300_tls_transition (info
, r_type
, h
, sec
, TRUE
);
1100 /* Some relocs require a global offset table. */
1105 case R_MN10300_GOT32
:
1106 case R_MN10300_GOT24
:
1107 case R_MN10300_GOT16
:
1108 case R_MN10300_GOTOFF32
:
1109 case R_MN10300_GOTOFF24
:
1110 case R_MN10300_GOTOFF16
:
1111 case R_MN10300_GOTPC32
:
1112 case R_MN10300_GOTPC16
:
1113 case R_MN10300_TLS_GD
:
1114 case R_MN10300_TLS_LD
:
1115 case R_MN10300_TLS_GOTIE
:
1116 case R_MN10300_TLS_IE
:
1117 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
1118 if (! _bfd_mn10300_elf_create_got_section (dynobj
, info
))
1129 /* This relocation describes the C++ object vtable hierarchy.
1130 Reconstruct it for later use during GC. */
1131 case R_MN10300_GNU_VTINHERIT
:
1132 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
1136 /* This relocation describes which C++ vtable entries are actually
1137 used. Record for later use during GC. */
1138 case R_MN10300_GNU_VTENTRY
:
1139 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
1143 case R_MN10300_TLS_LD
:
1144 htab
->tls_ldm_got
.refcount
++;
1145 tls_type
= GOT_TLS_LD
;
1147 if (htab
->tls_ldm_got
.got_allocated
)
1151 case R_MN10300_TLS_IE
:
1152 case R_MN10300_TLS_GOTIE
:
1153 if (bfd_link_pic (info
))
1154 info
->flags
|= DF_STATIC_TLS
;
1157 case R_MN10300_TLS_GD
:
1158 case R_MN10300_GOT32
:
1159 case R_MN10300_GOT24
:
1160 case R_MN10300_GOT16
:
1162 /* This symbol requires a global offset table entry. */
1166 case R_MN10300_TLS_IE
:
1167 case R_MN10300_TLS_GOTIE
: tls_type
= GOT_TLS_IE
; break;
1168 case R_MN10300_TLS_GD
: tls_type
= GOT_TLS_GD
; break;
1169 default: tls_type
= GOT_NORMAL
; break;
1172 sgot
= htab
->root
.sgot
;
1173 srelgot
= htab
->root
.srelgot
;
1174 BFD_ASSERT (sgot
!= NULL
&& srelgot
!= NULL
);
1176 if (r_type
== R_MN10300_TLS_LD
)
1178 htab
->tls_ldm_got
.offset
= sgot
->size
;
1179 htab
->tls_ldm_got
.got_allocated
++;
1183 if (elf_mn10300_hash_entry (h
)->tls_type
!= tls_type
1184 && elf_mn10300_hash_entry (h
)->tls_type
!= GOT_UNKNOWN
)
1186 if (tls_type
== GOT_TLS_IE
1187 && elf_mn10300_hash_entry (h
)->tls_type
== GOT_TLS_GD
)
1188 /* No change - this is ok. */;
1189 else if (tls_type
== GOT_TLS_GD
1190 && elf_mn10300_hash_entry (h
)->tls_type
== GOT_TLS_IE
)
1191 /* Transition GD->IE. */
1192 tls_type
= GOT_TLS_IE
;
1195 /* xgettext:c-format */
1196 (_("%pB: %s' accessed both as normal and thread local symbol"),
1197 abfd
, h
? h
->root
.root
.string
: "<local>");
1200 elf_mn10300_hash_entry (h
)->tls_type
= tls_type
;
1202 if (h
->got
.offset
!= (bfd_vma
) -1)
1203 /* We have already allocated space in the .got. */
1206 h
->got
.offset
= sgot
->size
;
1208 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
1209 /* Make sure this symbol is output as a dynamic symbol. */
1210 && h
->dynindx
== -1)
1212 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1216 srelgot
->size
+= sizeof (Elf32_External_Rela
);
1217 if (r_type
== R_MN10300_TLS_GD
)
1218 srelgot
->size
+= sizeof (Elf32_External_Rela
);
1222 /* This is a global offset table entry for a local
1224 if (local_got_offsets
== NULL
)
1229 size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + sizeof (char));
1230 local_got_offsets
= bfd_alloc (abfd
, size
);
1232 if (local_got_offsets
== NULL
)
1235 elf_local_got_offsets (abfd
) = local_got_offsets
;
1236 elf_mn10300_local_got_tls_type (abfd
)
1237 = (char *) (local_got_offsets
+ symtab_hdr
->sh_info
);
1239 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
1240 local_got_offsets
[i
] = (bfd_vma
) -1;
1243 if (local_got_offsets
[r_symndx
] != (bfd_vma
) -1)
1244 /* We have already allocated space in the .got. */
1247 local_got_offsets
[r_symndx
] = sgot
->size
;
1249 if (bfd_link_pic (info
))
1251 /* If we are generating a shared object, we need to
1252 output a R_MN10300_RELATIVE reloc so that the dynamic
1253 linker can adjust this GOT entry. */
1254 srelgot
->size
+= sizeof (Elf32_External_Rela
);
1256 if (r_type
== R_MN10300_TLS_GD
)
1257 /* And a R_MN10300_TLS_DTPOFF reloc as well. */
1258 srelgot
->size
+= sizeof (Elf32_External_Rela
);
1261 elf_mn10300_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
1265 if (r_type
== R_MN10300_TLS_GD
1266 || r_type
== R_MN10300_TLS_LD
)
1269 goto need_shared_relocs
;
1271 case R_MN10300_PLT32
:
1272 case R_MN10300_PLT16
:
1273 /* This symbol requires a procedure linkage table entry. We
1274 actually build the entry in adjust_dynamic_symbol,
1275 because this might be a case of linking PIC code which is
1276 never referenced by a dynamic object, in which case we
1277 don't need to generate a procedure linkage table entry
1280 /* If this is a local symbol, we resolve it directly without
1281 creating a procedure linkage table entry. */
1285 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
1286 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
1295 case R_MN10300_PCREL32
:
1296 case R_MN10300_PCREL16
:
1297 case R_MN10300_PCREL8
:
1302 case R_MN10300_SYM_DIFF
:
1303 sym_diff_reloc_seen
= TRUE
;
1311 /* If we are creating a shared library, then we
1312 need to copy the reloc into the shared library. */
1313 if (bfd_link_pic (info
)
1314 && (sec
->flags
& SEC_ALLOC
) != 0
1315 /* Do not generate a dynamic reloc for a
1316 reloc associated with a SYM_DIFF operation. */
1317 && ! sym_diff_reloc_seen
)
1319 asection
* sym_section
= NULL
;
1321 /* Find the section containing the
1322 symbol involved in the relocation. */
1325 Elf_Internal_Sym
* isym
;
1327 if (isymbuf
== NULL
)
1328 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
1329 symtab_hdr
->sh_info
, 0,
1333 isym
= isymbuf
+ r_symndx
;
1334 /* All we care about is whether this local symbol is absolute. */
1335 if (isym
->st_shndx
== SHN_ABS
)
1336 sym_section
= bfd_abs_section_ptr
;
1341 if (h
->root
.type
== bfd_link_hash_defined
1342 || h
->root
.type
== bfd_link_hash_defweak
)
1343 sym_section
= h
->root
.u
.def
.section
;
1346 /* If the symbol is absolute then the relocation can
1347 be resolved during linking and there is no need for
1349 if (sym_section
!= bfd_abs_section_ptr
)
1351 /* When creating a shared object, we must copy these
1352 reloc types into the output file. We create a reloc
1353 section in dynobj and make room for this reloc. */
1356 sreloc
= _bfd_elf_make_dynamic_reloc_section
1357 (sec
, dynobj
, 2, abfd
, /*rela?*/ TRUE
);
1362 sreloc
->size
+= sizeof (Elf32_External_Rela
);
1369 if (ELF32_R_TYPE (rel
->r_info
) != R_MN10300_SYM_DIFF
)
1370 sym_diff_reloc_seen
= FALSE
;
1375 if (isymbuf
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1381 /* Return the section that should be marked against GC for a given
1385 mn10300_elf_gc_mark_hook (asection
*sec
,
1386 struct bfd_link_info
*info
,
1387 Elf_Internal_Rela
*rel
,
1388 struct elf_link_hash_entry
*h
,
1389 Elf_Internal_Sym
*sym
)
1392 switch (ELF32_R_TYPE (rel
->r_info
))
1394 case R_MN10300_GNU_VTINHERIT
:
1395 case R_MN10300_GNU_VTENTRY
:
1399 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
1402 /* Perform a relocation as part of a final link. */
1404 static bfd_reloc_status_type
1405 mn10300_elf_final_link_relocate (reloc_howto_type
*howto
,
1407 bfd
*output_bfd ATTRIBUTE_UNUSED
,
1408 asection
*input_section
,
1413 struct elf_link_hash_entry
* h
,
1414 unsigned long symndx
,
1415 struct bfd_link_info
*info
,
1416 asection
*sym_sec ATTRIBUTE_UNUSED
,
1417 int is_local ATTRIBUTE_UNUSED
)
1419 struct elf32_mn10300_link_hash_table
* htab
= elf32_mn10300_hash_table (info
);
1420 static asection
* sym_diff_section
;
1421 static bfd_vma sym_diff_value
;
1422 bfd_boolean is_sym_diff_reloc
;
1423 unsigned long r_type
= howto
->type
;
1424 bfd_byte
* hit_data
= contents
+ offset
;
1430 dynobj
= elf_hash_table (info
)->dynobj
;
1440 case R_MN10300_PCREL8
:
1441 case R_MN10300_PCREL16
:
1442 case R_MN10300_PCREL32
:
1443 case R_MN10300_GOTOFF32
:
1444 case R_MN10300_GOTOFF24
:
1445 case R_MN10300_GOTOFF16
:
1446 if (bfd_link_pic (info
)
1447 && (input_section
->flags
& SEC_ALLOC
) != 0
1449 && ! SYMBOL_REFERENCES_LOCAL (info
, h
))
1450 return bfd_reloc_dangerous
;
1452 case R_MN10300_GOT32
:
1454 Taking the address of a protected function in a shared library
1455 is illegal. Issue an error message here. */
1456 if (bfd_link_pic (info
)
1457 && (input_section
->flags
& SEC_ALLOC
) != 0
1459 && ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
1460 && (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
)
1461 && ! SYMBOL_REFERENCES_LOCAL (info
, h
))
1462 return bfd_reloc_dangerous
;
1465 is_sym_diff_reloc
= FALSE
;
1466 if (sym_diff_section
!= NULL
)
1468 BFD_ASSERT (sym_diff_section
== input_section
);
1476 value
-= sym_diff_value
;
1477 /* If we are computing a 32-bit value for the location lists
1478 and the result is 0 then we add one to the value. A zero
1479 value can result because of linker relaxation deleteing
1480 prologue instructions and using a value of 1 (for the begin
1481 and end offsets in the location list entry) results in a
1482 nul entry which does not prevent the following entries from
1484 if (r_type
== R_MN10300_32
1486 && strcmp (input_section
->name
, ".debug_loc") == 0)
1488 sym_diff_section
= NULL
;
1489 is_sym_diff_reloc
= TRUE
;
1493 sym_diff_section
= NULL
;
1500 case R_MN10300_SYM_DIFF
:
1501 BFD_ASSERT (addend
== 0);
1502 /* Cache the input section and value.
1503 The offset is unreliable, since relaxation may
1504 have reduced the following reloc's offset. */
1505 sym_diff_section
= input_section
;
1506 sym_diff_value
= value
;
1507 return bfd_reloc_ok
;
1509 case R_MN10300_ALIGN
:
1510 case R_MN10300_NONE
:
1511 return bfd_reloc_ok
;
1514 if (bfd_link_pic (info
)
1515 /* Do not generate relocs when an R_MN10300_32 has been used
1516 with an R_MN10300_SYM_DIFF to compute a difference of two
1518 && !is_sym_diff_reloc
1519 /* Also, do not generate a reloc when the symbol associated
1520 with the R_MN10300_32 reloc is absolute - there is no
1521 need for a run time computation in this case. */
1522 && sym_sec
!= bfd_abs_section_ptr
1523 /* If the section is not going to be allocated at load time
1524 then there is no need to generate relocs for it. */
1525 && (input_section
->flags
& SEC_ALLOC
) != 0)
1527 Elf_Internal_Rela outrel
;
1528 bfd_boolean skip
, relocate
;
1530 /* When generating a shared object, these relocations are
1531 copied into the output file to be resolved at run
1535 sreloc
= _bfd_elf_get_dynamic_reloc_section
1536 (input_bfd
, input_section
, /*rela?*/ TRUE
);
1543 outrel
.r_offset
= _bfd_elf_section_offset (input_bfd
, info
,
1544 input_section
, offset
);
1545 if (outrel
.r_offset
== (bfd_vma
) -1)
1548 outrel
.r_offset
+= (input_section
->output_section
->vma
1549 + input_section
->output_offset
);
1553 memset (&outrel
, 0, sizeof outrel
);
1558 /* h->dynindx may be -1 if this symbol was marked to
1561 || SYMBOL_REFERENCES_LOCAL (info
, h
))
1564 outrel
.r_info
= ELF32_R_INFO (0, R_MN10300_RELATIVE
);
1565 outrel
.r_addend
= value
+ addend
;
1569 BFD_ASSERT (h
->dynindx
!= -1);
1571 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_32
);
1572 outrel
.r_addend
= value
+ addend
;
1576 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
1577 (bfd_byte
*) (((Elf32_External_Rela
*) sreloc
->contents
)
1578 + sreloc
->reloc_count
));
1579 ++sreloc
->reloc_count
;
1581 /* If this reloc is against an external symbol, we do
1582 not want to fiddle with the addend. Otherwise, we
1583 need to include the symbol value so that it becomes
1584 an addend for the dynamic reloc. */
1586 return bfd_reloc_ok
;
1589 bfd_put_32 (input_bfd
, value
, hit_data
);
1590 return bfd_reloc_ok
;
1595 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
1596 return bfd_reloc_overflow
;
1598 bfd_put_8 (input_bfd
, value
& 0xff, hit_data
);
1599 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
1600 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
1601 return bfd_reloc_ok
;
1606 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1607 return bfd_reloc_overflow
;
1609 bfd_put_16 (input_bfd
, value
, hit_data
);
1610 return bfd_reloc_ok
;
1615 if ((long) value
> 0x7f || (long) value
< -0x80)
1616 return bfd_reloc_overflow
;
1618 bfd_put_8 (input_bfd
, value
, hit_data
);
1619 return bfd_reloc_ok
;
1621 case R_MN10300_PCREL8
:
1622 value
-= (input_section
->output_section
->vma
1623 + input_section
->output_offset
);
1627 if ((long) value
> 0x7f || (long) value
< -0x80)
1628 return bfd_reloc_overflow
;
1630 bfd_put_8 (input_bfd
, value
, hit_data
);
1631 return bfd_reloc_ok
;
1633 case R_MN10300_PCREL16
:
1634 value
-= (input_section
->output_section
->vma
1635 + input_section
->output_offset
);
1639 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1640 return bfd_reloc_overflow
;
1642 bfd_put_16 (input_bfd
, value
, hit_data
);
1643 return bfd_reloc_ok
;
1645 case R_MN10300_PCREL32
:
1646 value
-= (input_section
->output_section
->vma
1647 + input_section
->output_offset
);
1651 bfd_put_32 (input_bfd
, value
, hit_data
);
1652 return bfd_reloc_ok
;
1654 case R_MN10300_GNU_VTINHERIT
:
1655 case R_MN10300_GNU_VTENTRY
:
1656 return bfd_reloc_ok
;
1658 case R_MN10300_GOTPC32
:
1660 return bfd_reloc_dangerous
;
1662 /* Use global offset table as symbol value. */
1663 value
= htab
->root
.sgot
->output_section
->vma
;
1664 value
-= (input_section
->output_section
->vma
1665 + input_section
->output_offset
);
1669 bfd_put_32 (input_bfd
, value
, hit_data
);
1670 return bfd_reloc_ok
;
1672 case R_MN10300_GOTPC16
:
1674 return bfd_reloc_dangerous
;
1676 /* Use global offset table as symbol value. */
1677 value
= htab
->root
.sgot
->output_section
->vma
;
1678 value
-= (input_section
->output_section
->vma
1679 + input_section
->output_offset
);
1683 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1684 return bfd_reloc_overflow
;
1686 bfd_put_16 (input_bfd
, value
, hit_data
);
1687 return bfd_reloc_ok
;
1689 case R_MN10300_GOTOFF32
:
1691 return bfd_reloc_dangerous
;
1693 value
-= htab
->root
.sgot
->output_section
->vma
;
1696 bfd_put_32 (input_bfd
, value
, hit_data
);
1697 return bfd_reloc_ok
;
1699 case R_MN10300_GOTOFF24
:
1701 return bfd_reloc_dangerous
;
1703 value
-= htab
->root
.sgot
->output_section
->vma
;
1706 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
1707 return bfd_reloc_overflow
;
1709 bfd_put_8 (input_bfd
, value
, hit_data
);
1710 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
1711 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
1712 return bfd_reloc_ok
;
1714 case R_MN10300_GOTOFF16
:
1716 return bfd_reloc_dangerous
;
1718 value
-= htab
->root
.sgot
->output_section
->vma
;
1721 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1722 return bfd_reloc_overflow
;
1724 bfd_put_16 (input_bfd
, value
, hit_data
);
1725 return bfd_reloc_ok
;
1727 case R_MN10300_PLT32
:
1729 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
1730 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
1731 && h
->plt
.offset
!= (bfd_vma
) -1)
1734 return bfd_reloc_dangerous
;
1736 splt
= htab
->root
.splt
;
1737 value
= (splt
->output_section
->vma
1738 + splt
->output_offset
1739 + h
->plt
.offset
) - value
;
1742 value
-= (input_section
->output_section
->vma
1743 + input_section
->output_offset
);
1747 bfd_put_32 (input_bfd
, value
, hit_data
);
1748 return bfd_reloc_ok
;
1750 case R_MN10300_PLT16
:
1752 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
1753 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
1754 && h
->plt
.offset
!= (bfd_vma
) -1)
1757 return bfd_reloc_dangerous
;
1759 splt
= htab
->root
.splt
;
1760 value
= (splt
->output_section
->vma
1761 + splt
->output_offset
1762 + h
->plt
.offset
) - value
;
1765 value
-= (input_section
->output_section
->vma
1766 + input_section
->output_offset
);
1770 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1771 return bfd_reloc_overflow
;
1773 bfd_put_16 (input_bfd
, value
, hit_data
);
1774 return bfd_reloc_ok
;
1776 case R_MN10300_TLS_LDO
:
1777 value
= dtpoff (info
, value
);
1778 bfd_put_32 (input_bfd
, value
+ addend
, hit_data
);
1779 return bfd_reloc_ok
;
1781 case R_MN10300_TLS_LE
:
1782 value
= tpoff (info
, value
);
1783 bfd_put_32 (input_bfd
, value
+ addend
, hit_data
);
1784 return bfd_reloc_ok
;
1786 case R_MN10300_TLS_LD
:
1788 return bfd_reloc_dangerous
;
1790 sgot
= htab
->root
.sgot
;
1791 BFD_ASSERT (sgot
!= NULL
);
1792 value
= htab
->tls_ldm_got
.offset
+ sgot
->output_offset
;
1793 bfd_put_32 (input_bfd
, value
, hit_data
);
1795 if (!htab
->tls_ldm_got
.rel_emitted
)
1797 asection
*srelgot
= htab
->root
.srelgot
;
1798 Elf_Internal_Rela rel
;
1800 BFD_ASSERT (srelgot
!= NULL
);
1801 htab
->tls_ldm_got
.rel_emitted
++;
1802 rel
.r_offset
= (sgot
->output_section
->vma
1803 + sgot
->output_offset
1804 + htab
->tls_ldm_got
.offset
);
1805 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ htab
->tls_ldm_got
.offset
);
1806 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ htab
->tls_ldm_got
.offset
+4);
1807 rel
.r_info
= ELF32_R_INFO (0, R_MN10300_TLS_DTPMOD
);
1809 bfd_elf32_swap_reloca_out (output_bfd
, & rel
,
1810 (bfd_byte
*) ((Elf32_External_Rela
*) srelgot
->contents
1811 + srelgot
->reloc_count
));
1812 ++ srelgot
->reloc_count
;
1815 return bfd_reloc_ok
;
1817 case R_MN10300_TLS_GOTIE
:
1818 value
= tpoff (info
, value
);
1821 case R_MN10300_TLS_GD
:
1822 case R_MN10300_TLS_IE
:
1823 case R_MN10300_GOT32
:
1824 case R_MN10300_GOT24
:
1825 case R_MN10300_GOT16
:
1827 return bfd_reloc_dangerous
;
1829 sgot
= htab
->root
.sgot
;
1830 if (r_type
== R_MN10300_TLS_GD
)
1831 value
= dtpoff (info
, value
);
1837 off
= h
->got
.offset
;
1838 /* Offsets in the GOT are allocated in check_relocs
1839 which is not called for shared libraries... */
1840 if (off
== (bfd_vma
) -1)
1843 if (sgot
->contents
!= NULL
1844 && (! elf_hash_table (info
)->dynamic_sections_created
1845 || SYMBOL_REFERENCES_LOCAL (info
, h
)))
1846 /* This is actually a static link, or it is a
1847 -Bsymbolic link and the symbol is defined
1848 locally, or the symbol was forced to be local
1849 because of a version file. We must initialize
1850 this entry in the global offset table.
1852 When doing a dynamic link, we create a .rela.got
1853 relocation entry to initialize the value. This
1854 is done in the finish_dynamic_symbol routine. */
1855 bfd_put_32 (output_bfd
, value
,
1856 sgot
->contents
+ off
);
1858 value
= sgot
->output_offset
+ off
;
1864 off
= elf_local_got_offsets (input_bfd
)[symndx
];
1867 bfd_put_32 (output_bfd
, value
, sgot
->contents
+ (off
& ~ 1));
1870 bfd_put_32 (output_bfd
, value
, sgot
->contents
+ off
);
1872 if (bfd_link_pic (info
))
1874 asection
*srelgot
= htab
->root
.srelgot
;;
1875 Elf_Internal_Rela outrel
;
1877 BFD_ASSERT (srelgot
!= NULL
);
1879 outrel
.r_offset
= (sgot
->output_section
->vma
1880 + sgot
->output_offset
1884 case R_MN10300_TLS_GD
:
1885 outrel
.r_info
= ELF32_R_INFO (0, R_MN10300_TLS_DTPOFF
);
1886 outrel
.r_offset
= (sgot
->output_section
->vma
1887 + sgot
->output_offset
1889 bfd_elf32_swap_reloca_out (output_bfd
, & outrel
,
1890 (bfd_byte
*) (((Elf32_External_Rela
*)
1892 + srelgot
->reloc_count
));
1893 ++ srelgot
->reloc_count
;
1894 outrel
.r_info
= ELF32_R_INFO (0, R_MN10300_TLS_DTPMOD
);
1896 case R_MN10300_TLS_GOTIE
:
1897 case R_MN10300_TLS_IE
:
1898 outrel
.r_info
= ELF32_R_INFO (0, R_MN10300_TLS_TPOFF
);
1901 outrel
.r_info
= ELF32_R_INFO (0, R_MN10300_RELATIVE
);
1905 outrel
.r_addend
= value
;
1906 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
1907 (bfd_byte
*) (((Elf32_External_Rela
*)
1909 + srelgot
->reloc_count
));
1910 ++ srelgot
->reloc_count
;
1911 elf_local_got_offsets (input_bfd
)[symndx
] |= 1;
1914 value
= sgot
->output_offset
+ (off
& ~(bfd_vma
) 1);
1920 if (r_type
== R_MN10300_TLS_IE
)
1922 value
+= sgot
->output_section
->vma
;
1923 bfd_put_32 (input_bfd
, value
, hit_data
);
1924 return bfd_reloc_ok
;
1926 else if (r_type
== R_MN10300_TLS_GOTIE
1927 || r_type
== R_MN10300_TLS_GD
1928 || r_type
== R_MN10300_TLS_LD
)
1930 bfd_put_32 (input_bfd
, value
, hit_data
);
1931 return bfd_reloc_ok
;
1933 else if (r_type
== R_MN10300_GOT32
)
1935 bfd_put_32 (input_bfd
, value
, hit_data
);
1936 return bfd_reloc_ok
;
1938 else if (r_type
== R_MN10300_GOT24
)
1940 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
1941 return bfd_reloc_overflow
;
1943 bfd_put_8 (input_bfd
, value
& 0xff, hit_data
);
1944 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
1945 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
1946 return bfd_reloc_ok
;
1948 else if (r_type
== R_MN10300_GOT16
)
1950 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1951 return bfd_reloc_overflow
;
1953 bfd_put_16 (input_bfd
, value
, hit_data
);
1954 return bfd_reloc_ok
;
1959 return bfd_reloc_notsupported
;
1963 /* Relocate an MN10300 ELF section. */
1966 mn10300_elf_relocate_section (bfd
*output_bfd
,
1967 struct bfd_link_info
*info
,
1969 asection
*input_section
,
1971 Elf_Internal_Rela
*relocs
,
1972 Elf_Internal_Sym
*local_syms
,
1973 asection
**local_sections
)
1975 Elf_Internal_Shdr
*symtab_hdr
;
1976 struct elf_link_hash_entry
**sym_hashes
;
1977 Elf_Internal_Rela
*rel
, *relend
;
1978 Elf_Internal_Rela
* trel
;
1980 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1981 sym_hashes
= elf_sym_hashes (input_bfd
);
1984 relend
= relocs
+ input_section
->reloc_count
;
1985 for (; rel
< relend
; rel
++)
1988 reloc_howto_type
*howto
;
1989 unsigned long r_symndx
;
1990 Elf_Internal_Sym
*sym
;
1992 struct elf32_mn10300_link_hash_entry
*h
;
1994 bfd_reloc_status_type r
;
1996 bfd_boolean unresolved_reloc
= FALSE
;
1997 bfd_boolean warned
, ignored
;
1998 struct elf_link_hash_entry
* hh
;
2001 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2002 r_type
= ELF32_R_TYPE (rel
->r_info
);
2003 howto
= elf_mn10300_howto_table
+ r_type
;
2005 /* Just skip the vtable gc relocs. */
2006 if (r_type
== R_MN10300_GNU_VTINHERIT
2007 || r_type
== R_MN10300_GNU_VTENTRY
)
2013 if (r_symndx
< symtab_hdr
->sh_info
)
2017 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2018 r_symndx
, symtab_hdr
, sym_hashes
,
2019 hh
, sec
, relocation
,
2020 unresolved_reloc
, warned
, ignored
);
2022 h
= elf_mn10300_hash_entry (hh
);
2024 tls_r_type
= elf_mn10300_tls_transition (info
, r_type
, hh
, input_section
, 0);
2025 if (tls_r_type
!= r_type
)
2027 bfd_boolean had_plt
;
2029 had_plt
= mn10300_do_tls_transition (input_bfd
, r_type
, tls_r_type
,
2030 contents
, rel
->r_offset
);
2031 r_type
= tls_r_type
;
2032 howto
= elf_mn10300_howto_table
+ r_type
;
2035 for (trel
= rel
+1; trel
< relend
; trel
++)
2036 if ((ELF32_R_TYPE (trel
->r_info
) == R_MN10300_PLT32
2037 || ELF32_R_TYPE (trel
->r_info
) == R_MN10300_PCREL32
)
2038 && rel
->r_offset
+ had_plt
== trel
->r_offset
)
2039 trel
->r_info
= ELF32_R_INFO (0, R_MN10300_NONE
);
2042 if (r_symndx
< symtab_hdr
->sh_info
)
2044 sym
= local_syms
+ r_symndx
;
2045 sec
= local_sections
[r_symndx
];
2046 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2050 if ((h
->root
.root
.type
== bfd_link_hash_defined
2051 || h
->root
.root
.type
== bfd_link_hash_defweak
)
2052 && ( r_type
== R_MN10300_GOTPC32
2053 || r_type
== R_MN10300_GOTPC16
2054 || (( r_type
== R_MN10300_PLT32
2055 || r_type
== R_MN10300_PLT16
)
2056 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_INTERNAL
2057 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_HIDDEN
2058 && h
->root
.plt
.offset
!= (bfd_vma
) -1)
2059 || (( r_type
== R_MN10300_GOT32
2060 || r_type
== R_MN10300_GOT24
2061 || r_type
== R_MN10300_TLS_GD
2062 || r_type
== R_MN10300_TLS_LD
2063 || r_type
== R_MN10300_TLS_GOTIE
2064 || r_type
== R_MN10300_TLS_IE
2065 || r_type
== R_MN10300_GOT16
)
2066 && elf_hash_table (info
)->dynamic_sections_created
2067 && !SYMBOL_REFERENCES_LOCAL (info
, hh
))
2068 || (r_type
== R_MN10300_32
2069 /* _32 relocs in executables force _COPY relocs,
2070 such that the address of the symbol ends up
2072 && !bfd_link_executable (info
)
2073 && !SYMBOL_REFERENCES_LOCAL (info
, hh
)
2074 && ((input_section
->flags
& SEC_ALLOC
) != 0
2075 /* DWARF will emit R_MN10300_32 relocations
2076 in its sections against symbols defined
2077 externally in shared libraries. We can't
2078 do anything with them here. */
2079 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
2080 && h
->root
.def_dynamic
)))))
2081 /* In these cases, we don't need the relocation
2082 value. We check specially because in some
2083 obscure cases sec->output_section will be NULL. */
2086 else if (!bfd_link_relocatable (info
) && unresolved_reloc
2087 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2088 rel
->r_offset
) != (bfd_vma
) -1)
2091 /* xgettext:c-format */
2092 (_("%pB(%pA+%#" PRIx64
"): "
2093 "unresolvable %s relocation against symbol `%s'"),
2096 (uint64_t) rel
->r_offset
,
2098 h
->root
.root
.root
.string
);
2101 if (sec
!= NULL
&& discarded_section (sec
))
2102 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2103 rel
, 1, relend
, howto
, 0, contents
);
2105 if (bfd_link_relocatable (info
))
2108 r
= mn10300_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
2110 contents
, rel
->r_offset
,
2111 relocation
, rel
->r_addend
,
2112 (struct elf_link_hash_entry
*) h
,
2114 info
, sec
, h
== NULL
);
2116 if (r
!= bfd_reloc_ok
)
2119 const char *msg
= NULL
;
2122 name
= h
->root
.root
.root
.string
;
2125 name
= (bfd_elf_string_from_elf_section
2126 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
2127 if (name
== NULL
|| *name
== '\0')
2128 name
= bfd_section_name (sec
);
2133 case bfd_reloc_overflow
:
2134 (*info
->callbacks
->reloc_overflow
)
2135 (info
, (h
? &h
->root
.root
: NULL
), name
, howto
->name
,
2136 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2139 case bfd_reloc_undefined
:
2140 (*info
->callbacks
->undefined_symbol
)
2141 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
2144 case bfd_reloc_outofrange
:
2145 msg
= _("internal error: out of range error");
2148 case bfd_reloc_notsupported
:
2149 msg
= _("internal error: unsupported relocation error");
2152 case bfd_reloc_dangerous
:
2153 if (r_type
== R_MN10300_PCREL32
)
2154 msg
= _("error: inappropriate relocation type for shared"
2155 " library (did you forget -fpic?)");
2156 else if (r_type
== R_MN10300_GOT32
)
2157 /* xgettext:c-format */
2158 msg
= _("%pB: taking the address of protected function"
2159 " '%s' cannot be done when making a shared library");
2161 msg
= _("internal error: suspicious relocation type used"
2162 " in shared library");
2166 msg
= _("internal error: unknown error");
2170 _bfd_error_handler (msg
, input_bfd
, name
);
2171 bfd_set_error (bfd_error_bad_value
);
2180 /* Finish initializing one hash table entry. */
2183 elf32_mn10300_finish_hash_table_entry (struct bfd_hash_entry
*gen_entry
,
2186 struct elf32_mn10300_link_hash_entry
*entry
;
2187 struct bfd_link_info
*link_info
= (struct bfd_link_info
*) in_args
;
2188 unsigned int byte_count
= 0;
2190 entry
= (struct elf32_mn10300_link_hash_entry
*) gen_entry
;
2192 /* If we already know we want to convert "call" to "calls" for calls
2193 to this symbol, then return now. */
2194 if (entry
->flags
== MN10300_CONVERT_CALL_TO_CALLS
)
2197 /* If there are no named calls to this symbol, or there's nothing we
2198 can move from the function itself into the "call" instruction,
2199 then note that all "call" instructions should be converted into
2200 "calls" instructions and return. If a symbol is available for
2201 dynamic symbol resolution (overridable or overriding), avoid
2202 custom calling conventions. */
2203 if (entry
->direct_calls
== 0
2204 || (entry
->stack_size
== 0 && entry
->movm_args
== 0)
2205 || (elf_hash_table (link_info
)->dynamic_sections_created
2206 && ELF_ST_VISIBILITY (entry
->root
.other
) != STV_INTERNAL
2207 && ELF_ST_VISIBILITY (entry
->root
.other
) != STV_HIDDEN
))
2209 /* Make a note that we should convert "call" instructions to "calls"
2210 instructions for calls to this symbol. */
2211 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
2215 /* We may be able to move some instructions from the function itself into
2216 the "call" instruction. Count how many bytes we might be able to
2217 eliminate in the function itself. */
2219 /* A movm instruction is two bytes. */
2220 if (entry
->movm_args
)
2223 /* Count the insn to allocate stack space too. */
2224 if (entry
->stack_size
> 0)
2226 if (entry
->stack_size
<= 128)
2232 /* If using "call" will result in larger code, then turn all
2233 the associated "call" instructions into "calls" instructions. */
2234 if (byte_count
< entry
->direct_calls
)
2235 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
2237 /* This routine never fails. */
2241 /* Used to count hash table entries. */
2244 elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry
*gen_entry ATTRIBUTE_UNUSED
,
2247 int *count
= (int *) in_args
;
2253 /* Used to enumerate hash table entries into a linear array. */
2256 elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry
*gen_entry
,
2259 struct bfd_hash_entry
***ptr
= (struct bfd_hash_entry
***) in_args
;
2266 /* Used to sort the array created by the above. */
2269 sort_by_value (const void *va
, const void *vb
)
2271 struct elf32_mn10300_link_hash_entry
*a
2272 = *(struct elf32_mn10300_link_hash_entry
**) va
;
2273 struct elf32_mn10300_link_hash_entry
*b
2274 = *(struct elf32_mn10300_link_hash_entry
**) vb
;
2276 return a
->value
- b
->value
;
2279 /* Compute the stack size and movm arguments for the function
2280 referred to by HASH at address ADDR in section with
2281 contents CONTENTS, store the information in the hash table. */
2284 compute_function_info (bfd
*abfd
,
2285 struct elf32_mn10300_link_hash_entry
*hash
,
2287 unsigned char *contents
)
2289 unsigned char byte1
, byte2
;
2290 /* We only care about a very small subset of the possible prologue
2291 sequences here. Basically we look for:
2293 movm [d2,d3,a2,a3],sp (optional)
2294 add <size>,sp (optional, and only for sizes which fit in an unsigned
2297 If we find anything else, we quit. */
2299 /* Look for movm [regs],sp. */
2300 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
2301 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
2305 hash
->movm_args
= byte2
;
2307 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
2308 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
2311 /* Now figure out how much stack space will be allocated by the movm
2312 instruction. We need this kept separate from the function's normal
2314 if (hash
->movm_args
)
2317 if (hash
->movm_args
& 0x80)
2318 hash
->movm_stack_size
+= 4;
2321 if (hash
->movm_args
& 0x40)
2322 hash
->movm_stack_size
+= 4;
2325 if (hash
->movm_args
& 0x20)
2326 hash
->movm_stack_size
+= 4;
2329 if (hash
->movm_args
& 0x10)
2330 hash
->movm_stack_size
+= 4;
2332 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2333 if (hash
->movm_args
& 0x08)
2334 hash
->movm_stack_size
+= 8 * 4;
2336 if (bfd_get_mach (abfd
) == bfd_mach_am33
2337 || bfd_get_mach (abfd
) == bfd_mach_am33_2
)
2339 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
2340 if (hash
->movm_args
& 0x1)
2341 hash
->movm_stack_size
+= 6 * 4;
2343 /* exreg1 space. e4, e5, e6, e7 */
2344 if (hash
->movm_args
& 0x2)
2345 hash
->movm_stack_size
+= 4 * 4;
2347 /* exreg0 space. e2, e3 */
2348 if (hash
->movm_args
& 0x4)
2349 hash
->movm_stack_size
+= 2 * 4;
2353 /* Now look for the two stack adjustment variants. */
2354 if (byte1
== 0xf8 && byte2
== 0xfe)
2356 int temp
= bfd_get_8 (abfd
, contents
+ addr
+ 2);
2357 temp
= ((temp
& 0xff) ^ (~0x7f)) + 0x80;
2359 hash
->stack_size
= -temp
;
2361 else if (byte1
== 0xfa && byte2
== 0xfe)
2363 int temp
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
2364 temp
= ((temp
& 0xffff) ^ (~0x7fff)) + 0x8000;
2368 hash
->stack_size
= temp
;
2371 /* If the total stack to be allocated by the call instruction is more
2372 than 255 bytes, then we can't remove the stack adjustment by using
2373 "call" (we might still be able to remove the "movm" instruction. */
2374 if (hash
->stack_size
+ hash
->movm_stack_size
> 255)
2375 hash
->stack_size
= 0;
2378 /* Delete some bytes from a section while relaxing. */
2381 mn10300_elf_relax_delete_bytes (bfd
*abfd
,
2386 Elf_Internal_Shdr
*symtab_hdr
;
2387 unsigned int sec_shndx
;
2389 Elf_Internal_Rela
*irel
, *irelend
;
2390 Elf_Internal_Rela
*irelalign
;
2392 Elf_Internal_Sym
*isym
, *isymend
;
2393 struct elf_link_hash_entry
**sym_hashes
;
2394 struct elf_link_hash_entry
**end_hashes
;
2395 unsigned int symcount
;
2397 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2399 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2404 irel
= elf_section_data (sec
)->relocs
;
2405 irelend
= irel
+ sec
->reloc_count
;
2407 if (sec
->reloc_count
> 0)
2409 /* If there is an align reloc at the end of the section ignore it.
2410 GAS creates these relocs for reasons of its own, and they just
2411 serve to keep the section artifically inflated. */
2412 if (ELF32_R_TYPE ((irelend
- 1)->r_info
) == (int) R_MN10300_ALIGN
)
2415 /* The deletion must stop at the next ALIGN reloc for an alignment
2416 power larger than, or not a multiple of, the number of bytes we
2418 for (; irel
< irelend
; irel
++)
2420 int alignment
= 1 << irel
->r_addend
;
2422 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_ALIGN
2423 && irel
->r_offset
> addr
2424 && irel
->r_offset
< toaddr
2425 && (count
< alignment
2426 || alignment
% count
!= 0))
2429 toaddr
= irel
->r_offset
;
2435 /* Actually delete the bytes. */
2436 memmove (contents
+ addr
, contents
+ addr
+ count
,
2437 (size_t) (toaddr
- addr
- count
));
2439 /* Adjust the section's size if we are shrinking it, or else
2440 pad the bytes between the end of the shrunken region and
2441 the start of the next region with NOP codes. */
2442 if (irelalign
== NULL
)
2445 /* Include symbols at the end of the section, but
2446 not at the end of a sub-region of the section. */
2453 #define NOP_OPCODE 0xcb
2455 for (i
= 0; i
< count
; i
++)
2456 bfd_put_8 (abfd
, (bfd_vma
) NOP_OPCODE
, contents
+ toaddr
- count
+ i
);
2459 /* Adjust all the relocs. */
2460 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
2462 /* Get the new reloc address. */
2463 if ((irel
->r_offset
> addr
2464 && irel
->r_offset
< toaddr
)
2465 || (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_ALIGN
2466 && irel
->r_offset
== toaddr
))
2467 irel
->r_offset
-= count
;
2470 /* Adjust the local symbols in the section, reducing their value
2471 by the number of bytes deleted. Note - symbols within the deleted
2472 region are moved to the address of the start of the region, which
2473 actually means that they will address the byte beyond the end of
2474 the region once the deletion has been completed. */
2475 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2476 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2477 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
2479 if (isym
->st_shndx
== sec_shndx
2480 && isym
->st_value
> addr
2481 && isym
->st_value
< toaddr
)
2483 if (isym
->st_value
< addr
+ count
)
2484 isym
->st_value
= addr
;
2486 isym
->st_value
-= count
;
2488 /* Adjust the function symbol's size as well. */
2489 else if (isym
->st_shndx
== sec_shndx
2490 && ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
2491 && isym
->st_value
+ isym
->st_size
> addr
2492 && isym
->st_value
+ isym
->st_size
< toaddr
)
2493 isym
->st_size
-= count
;
2496 /* Now adjust the global symbols defined in this section. */
2497 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2498 - symtab_hdr
->sh_info
);
2499 sym_hashes
= elf_sym_hashes (abfd
);
2500 end_hashes
= sym_hashes
+ symcount
;
2501 for (; sym_hashes
< end_hashes
; sym_hashes
++)
2503 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
2505 if ((sym_hash
->root
.type
== bfd_link_hash_defined
2506 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
2507 && sym_hash
->root
.u
.def
.section
== sec
2508 && sym_hash
->root
.u
.def
.value
> addr
2509 && sym_hash
->root
.u
.def
.value
< toaddr
)
2511 if (sym_hash
->root
.u
.def
.value
< addr
+ count
)
2512 sym_hash
->root
.u
.def
.value
= addr
;
2514 sym_hash
->root
.u
.def
.value
-= count
;
2516 /* Adjust the function symbol's size as well. */
2517 else if (sym_hash
->root
.type
== bfd_link_hash_defined
2518 && sym_hash
->root
.u
.def
.section
== sec
2519 && sym_hash
->type
== STT_FUNC
2520 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
2521 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
< toaddr
)
2522 sym_hash
->size
-= count
;
2525 /* See if we can move the ALIGN reloc forward.
2526 We have adjusted r_offset for it already. */
2527 if (irelalign
!= NULL
)
2529 bfd_vma alignto
, alignaddr
;
2531 if ((int) irelalign
->r_addend
> 0)
2533 /* This is the old address. */
2534 alignto
= BFD_ALIGN (toaddr
, 1 << irelalign
->r_addend
);
2535 /* This is where the align points to now. */
2536 alignaddr
= BFD_ALIGN (irelalign
->r_offset
,
2537 1 << irelalign
->r_addend
);
2538 if (alignaddr
< alignto
)
2539 /* Tail recursion. */
2540 return mn10300_elf_relax_delete_bytes (abfd
, sec
, alignaddr
,
2541 (int) (alignto
- alignaddr
));
2548 /* Return TRUE if a symbol exists at the given address, else return
2552 mn10300_elf_symbol_address_p (bfd
*abfd
,
2554 Elf_Internal_Sym
*isym
,
2557 Elf_Internal_Shdr
*symtab_hdr
;
2558 unsigned int sec_shndx
;
2559 Elf_Internal_Sym
*isymend
;
2560 struct elf_link_hash_entry
**sym_hashes
;
2561 struct elf_link_hash_entry
**end_hashes
;
2562 unsigned int symcount
;
2564 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2566 /* Examine all the symbols. */
2567 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2568 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
2569 if (isym
->st_shndx
== sec_shndx
2570 && isym
->st_value
== addr
)
2573 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2574 - symtab_hdr
->sh_info
);
2575 sym_hashes
= elf_sym_hashes (abfd
);
2576 end_hashes
= sym_hashes
+ symcount
;
2577 for (; sym_hashes
< end_hashes
; sym_hashes
++)
2579 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
2581 if ((sym_hash
->root
.type
== bfd_link_hash_defined
2582 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
2583 && sym_hash
->root
.u
.def
.section
== sec
2584 && sym_hash
->root
.u
.def
.value
== addr
)
2591 /* This function handles relaxing for the mn10300.
2593 There are quite a few relaxing opportunities available on the mn10300:
2595 * calls:32 -> calls:16 2 bytes
2596 * call:32 -> call:16 2 bytes
2598 * call:32 -> calls:32 1 byte
2599 * call:16 -> calls:16 1 byte
2600 * These are done anytime using "calls" would result
2601 in smaller code, or when necessary to preserve the
2602 meaning of the program.
2606 * In some circumstances we can move instructions
2607 from a function prologue into a "call" instruction.
2608 This is only done if the resulting code is no larger
2609 than the original code.
2611 * jmp:32 -> jmp:16 2 bytes
2612 * jmp:16 -> bra:8 1 byte
2614 * If the previous instruction is a conditional branch
2615 around the jump/bra, we may be able to reverse its condition
2616 and change its target to the jump's target. The jump/bra
2617 can then be deleted. 2 bytes
2619 * mov abs32 -> mov abs16 1 or 2 bytes
2621 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
2622 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
2624 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
2625 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
2627 We don't handle imm16->imm8 or d16->d8 as they're very rare
2628 and somewhat more difficult to support. */
2631 mn10300_elf_relax_section (bfd
*abfd
,
2633 struct bfd_link_info
*link_info
,
2636 Elf_Internal_Shdr
*symtab_hdr
;
2637 Elf_Internal_Rela
*internal_relocs
= NULL
;
2638 Elf_Internal_Rela
*irel
, *irelend
;
2639 bfd_byte
*contents
= NULL
;
2640 Elf_Internal_Sym
*isymbuf
= NULL
;
2641 struct elf32_mn10300_link_hash_table
*hash_table
;
2642 asection
*section
= sec
;
2643 bfd_vma align_gap_adjustment
;
2645 if (bfd_link_relocatable (link_info
))
2646 (*link_info
->callbacks
->einfo
)
2647 (_("%P%F: --relax and -r may not be used together\n"));
2649 /* Assume nothing changes. */
2652 /* We need a pointer to the mn10300 specific hash table. */
2653 hash_table
= elf32_mn10300_hash_table (link_info
);
2654 if (hash_table
== NULL
)
2657 /* Initialize fields in each hash table entry the first time through. */
2658 if ((hash_table
->flags
& MN10300_HASH_ENTRIES_INITIALIZED
) == 0)
2662 /* Iterate over all the input bfds. */
2663 for (input_bfd
= link_info
->input_bfds
;
2665 input_bfd
= input_bfd
->link
.next
)
2667 /* We're going to need all the symbols for each bfd. */
2668 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2669 if (symtab_hdr
->sh_info
!= 0)
2671 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2672 if (isymbuf
== NULL
)
2673 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
2674 symtab_hdr
->sh_info
, 0,
2676 if (isymbuf
== NULL
)
2680 /* Iterate over each section in this bfd. */
2681 for (section
= input_bfd
->sections
;
2683 section
= section
->next
)
2685 struct elf32_mn10300_link_hash_entry
*hash
;
2686 asection
*sym_sec
= NULL
;
2687 const char *sym_name
;
2690 /* If there's nothing to do in this section, skip it. */
2691 if (! ((section
->flags
& SEC_RELOC
) != 0
2692 && section
->reloc_count
!= 0))
2694 if ((section
->flags
& SEC_ALLOC
) == 0)
2697 /* Get cached copy of section contents if it exists. */
2698 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
2699 contents
= elf_section_data (section
)->this_hdr
.contents
;
2700 else if (section
->size
!= 0)
2702 /* Go get them off disk. */
2703 if (!bfd_malloc_and_get_section (input_bfd
, section
,
2710 /* If there aren't any relocs, then there's nothing to do. */
2711 if ((section
->flags
& SEC_RELOC
) != 0
2712 && section
->reloc_count
!= 0)
2714 /* Get a copy of the native relocations. */
2715 internal_relocs
= _bfd_elf_link_read_relocs (input_bfd
, section
,
2717 link_info
->keep_memory
);
2718 if (internal_relocs
== NULL
)
2721 /* Now examine each relocation. */
2722 irel
= internal_relocs
;
2723 irelend
= irel
+ section
->reloc_count
;
2724 for (; irel
< irelend
; irel
++)
2727 unsigned long r_index
;
2730 r_type
= ELF32_R_TYPE (irel
->r_info
);
2731 r_index
= ELF32_R_SYM (irel
->r_info
);
2733 if (r_type
< 0 || r_type
>= (int) R_MN10300_MAX
)
2736 /* We need the name and hash table entry of the target
2741 if (r_index
< symtab_hdr
->sh_info
)
2743 /* A local symbol. */
2744 Elf_Internal_Sym
*isym
;
2745 struct elf_link_hash_table
*elftab
;
2748 isym
= isymbuf
+ r_index
;
2749 if (isym
->st_shndx
== SHN_UNDEF
)
2750 sym_sec
= bfd_und_section_ptr
;
2751 else if (isym
->st_shndx
== SHN_ABS
)
2752 sym_sec
= bfd_abs_section_ptr
;
2753 else if (isym
->st_shndx
== SHN_COMMON
)
2754 sym_sec
= bfd_com_section_ptr
;
2757 = bfd_section_from_elf_index (input_bfd
,
2761 = bfd_elf_string_from_elf_section (input_bfd
,
2766 /* If it isn't a function, then we don't care
2768 if (ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
)
2771 /* Tack on an ID so we can uniquely identify this
2772 local symbol in the global hash table. */
2773 amt
= strlen (sym_name
) + 10;
2774 new_name
= bfd_malloc (amt
);
2775 if (new_name
== NULL
)
2778 sprintf (new_name
, "%s_%08x", sym_name
, sym_sec
->id
);
2779 sym_name
= new_name
;
2781 elftab
= &hash_table
->static_hash_table
->root
;
2782 hash
= ((struct elf32_mn10300_link_hash_entry
*)
2783 elf_link_hash_lookup (elftab
, sym_name
,
2784 TRUE
, TRUE
, FALSE
));
2789 r_index
-= symtab_hdr
->sh_info
;
2790 hash
= (struct elf32_mn10300_link_hash_entry
*)
2791 elf_sym_hashes (input_bfd
)[r_index
];
2794 sym_name
= hash
->root
.root
.root
.string
;
2795 if ((section
->flags
& SEC_CODE
) != 0)
2797 /* If this is not a "call" instruction, then we
2798 should convert "call" instructions to "calls"
2800 code
= bfd_get_8 (input_bfd
,
2801 contents
+ irel
->r_offset
- 1);
2802 if (code
!= 0xdd && code
!= 0xcd)
2803 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
2806 /* If this is a jump/call, then bump the
2807 direct_calls counter. Else force "call" to
2808 "calls" conversions. */
2809 if (r_type
== R_MN10300_PCREL32
2810 || r_type
== R_MN10300_PLT32
2811 || r_type
== R_MN10300_PLT16
2812 || r_type
== R_MN10300_PCREL16
)
2813 hash
->direct_calls
++;
2815 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
2819 /* Now look at the actual contents to get the stack size,
2820 and a list of what registers were saved in the prologue
2822 if ((section
->flags
& SEC_CODE
) != 0)
2824 Elf_Internal_Sym
*isym
, *isymend
;
2825 unsigned int sec_shndx
;
2826 struct elf_link_hash_entry
**hashes
;
2827 struct elf_link_hash_entry
**end_hashes
;
2828 unsigned int symcount
;
2830 sec_shndx
= _bfd_elf_section_from_bfd_section (input_bfd
,
2833 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2834 - symtab_hdr
->sh_info
);
2835 hashes
= elf_sym_hashes (input_bfd
);
2836 end_hashes
= hashes
+ symcount
;
2838 /* Look at each function defined in this section and
2839 update info for that function. */
2840 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
2841 for (isym
= isymbuf
; isym
< isymend
; isym
++)
2843 if (isym
->st_shndx
== sec_shndx
2844 && ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)
2846 struct elf_link_hash_table
*elftab
;
2848 struct elf_link_hash_entry
**lhashes
= hashes
;
2850 /* Skip a local symbol if it aliases a
2852 for (; lhashes
< end_hashes
; lhashes
++)
2854 hash
= (struct elf32_mn10300_link_hash_entry
*) *lhashes
;
2855 if ((hash
->root
.root
.type
== bfd_link_hash_defined
2856 || hash
->root
.root
.type
== bfd_link_hash_defweak
)
2857 && hash
->root
.root
.u
.def
.section
== section
2858 && hash
->root
.type
== STT_FUNC
2859 && hash
->root
.root
.u
.def
.value
== isym
->st_value
)
2862 if (lhashes
!= end_hashes
)
2865 if (isym
->st_shndx
== SHN_UNDEF
)
2866 sym_sec
= bfd_und_section_ptr
;
2867 else if (isym
->st_shndx
== SHN_ABS
)
2868 sym_sec
= bfd_abs_section_ptr
;
2869 else if (isym
->st_shndx
== SHN_COMMON
)
2870 sym_sec
= bfd_com_section_ptr
;
2873 = bfd_section_from_elf_index (input_bfd
,
2876 sym_name
= (bfd_elf_string_from_elf_section
2877 (input_bfd
, symtab_hdr
->sh_link
,
2880 /* Tack on an ID so we can uniquely identify this
2881 local symbol in the global hash table. */
2882 amt
= strlen (sym_name
) + 10;
2883 new_name
= bfd_malloc (amt
);
2884 if (new_name
== NULL
)
2887 sprintf (new_name
, "%s_%08x", sym_name
, sym_sec
->id
);
2888 sym_name
= new_name
;
2890 elftab
= &hash_table
->static_hash_table
->root
;
2891 hash
= ((struct elf32_mn10300_link_hash_entry
*)
2892 elf_link_hash_lookup (elftab
, sym_name
,
2893 TRUE
, TRUE
, FALSE
));
2895 compute_function_info (input_bfd
, hash
,
2896 isym
->st_value
, contents
);
2897 hash
->value
= isym
->st_value
;
2901 for (; hashes
< end_hashes
; hashes
++)
2903 hash
= (struct elf32_mn10300_link_hash_entry
*) *hashes
;
2904 if ((hash
->root
.root
.type
== bfd_link_hash_defined
2905 || hash
->root
.root
.type
== bfd_link_hash_defweak
)
2906 && hash
->root
.root
.u
.def
.section
== section
2907 && hash
->root
.type
== STT_FUNC
)
2908 compute_function_info (input_bfd
, hash
,
2909 (hash
)->root
.root
.u
.def
.value
,
2914 /* Cache or free any memory we allocated for the relocs. */
2915 if (internal_relocs
!= NULL
2916 && elf_section_data (section
)->relocs
!= internal_relocs
)
2917 free (internal_relocs
);
2918 internal_relocs
= NULL
;
2920 /* Cache or free any memory we allocated for the contents. */
2921 if (contents
!= NULL
2922 && elf_section_data (section
)->this_hdr
.contents
!= contents
)
2924 if (! link_info
->keep_memory
)
2928 /* Cache the section contents for elf_link_input_bfd. */
2929 elf_section_data (section
)->this_hdr
.contents
= contents
;
2935 /* Cache or free any memory we allocated for the symbols. */
2937 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2939 if (! link_info
->keep_memory
)
2943 /* Cache the symbols for elf_link_input_bfd. */
2944 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2950 /* Now iterate on each symbol in the hash table and perform
2951 the final initialization steps on each. */
2952 elf32_mn10300_link_hash_traverse (hash_table
,
2953 elf32_mn10300_finish_hash_table_entry
,
2955 elf32_mn10300_link_hash_traverse (hash_table
->static_hash_table
,
2956 elf32_mn10300_finish_hash_table_entry
,
2960 /* This section of code collects all our local symbols, sorts
2961 them by value, and looks for multiple symbols referring to
2962 the same address. For those symbols, the flags are merged.
2963 At this point, the only flag that can be set is
2964 MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
2966 int static_count
= 0, i
;
2967 struct elf32_mn10300_link_hash_entry
**entries
;
2968 struct elf32_mn10300_link_hash_entry
**ptr
;
2970 elf32_mn10300_link_hash_traverse (hash_table
->static_hash_table
,
2971 elf32_mn10300_count_hash_table_entries
,
2974 entries
= bfd_malloc (static_count
* sizeof (* ptr
));
2977 elf32_mn10300_link_hash_traverse (hash_table
->static_hash_table
,
2978 elf32_mn10300_list_hash_table_entries
,
2981 qsort (entries
, static_count
, sizeof (entries
[0]), sort_by_value
);
2983 for (i
= 0; i
< static_count
- 1; i
++)
2984 if (entries
[i
]->value
&& entries
[i
]->value
== entries
[i
+1]->value
)
2986 int v
= entries
[i
]->flags
;
2989 for (j
= i
+ 1; j
< static_count
&& entries
[j
]->value
== entries
[i
]->value
; j
++)
2990 v
|= entries
[j
]->flags
;
2992 for (j
= i
; j
< static_count
&& entries
[j
]->value
== entries
[i
]->value
; j
++)
2993 entries
[j
]->flags
= v
;
2999 /* All entries in the hash table are fully initialized. */
3000 hash_table
->flags
|= MN10300_HASH_ENTRIES_INITIALIZED
;
3002 /* Now that everything has been initialized, go through each
3003 code section and delete any prologue insns which will be
3004 redundant because their operations will be performed by
3005 a "call" instruction. */
3006 for (input_bfd
= link_info
->input_bfds
;
3008 input_bfd
= input_bfd
->link
.next
)
3010 /* We're going to need all the local symbols for each bfd. */
3011 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3012 if (symtab_hdr
->sh_info
!= 0)
3014 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3015 if (isymbuf
== NULL
)
3016 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
3017 symtab_hdr
->sh_info
, 0,
3019 if (isymbuf
== NULL
)
3023 /* Walk over each section in this bfd. */
3024 for (section
= input_bfd
->sections
;
3026 section
= section
->next
)
3028 unsigned int sec_shndx
;
3029 Elf_Internal_Sym
*isym
, *isymend
;
3030 struct elf_link_hash_entry
**hashes
;
3031 struct elf_link_hash_entry
**end_hashes
;
3032 unsigned int symcount
;
3034 /* Skip non-code sections and empty sections. */
3035 if ((section
->flags
& SEC_CODE
) == 0 || section
->size
== 0)
3038 if (section
->reloc_count
!= 0)
3040 /* Get a copy of the native relocations. */
3041 internal_relocs
= _bfd_elf_link_read_relocs (input_bfd
, section
,
3043 link_info
->keep_memory
);
3044 if (internal_relocs
== NULL
)
3048 /* Get cached copy of section contents if it exists. */
3049 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
3050 contents
= elf_section_data (section
)->this_hdr
.contents
;
3053 /* Go get them off disk. */
3054 if (!bfd_malloc_and_get_section (input_bfd
, section
,
3059 sec_shndx
= _bfd_elf_section_from_bfd_section (input_bfd
,
3062 /* Now look for any function in this section which needs
3063 insns deleted from its prologue. */
3064 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
3065 for (isym
= isymbuf
; isym
< isymend
; isym
++)
3067 struct elf32_mn10300_link_hash_entry
*sym_hash
;
3068 asection
*sym_sec
= NULL
;
3069 const char *sym_name
;
3071 struct elf_link_hash_table
*elftab
;
3074 if (isym
->st_shndx
!= sec_shndx
)
3077 if (isym
->st_shndx
== SHN_UNDEF
)
3078 sym_sec
= bfd_und_section_ptr
;
3079 else if (isym
->st_shndx
== SHN_ABS
)
3080 sym_sec
= bfd_abs_section_ptr
;
3081 else if (isym
->st_shndx
== SHN_COMMON
)
3082 sym_sec
= bfd_com_section_ptr
;
3085 = bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
3088 = bfd_elf_string_from_elf_section (input_bfd
,
3089 symtab_hdr
->sh_link
,
3092 /* Tack on an ID so we can uniquely identify this
3093 local symbol in the global hash table. */
3094 amt
= strlen (sym_name
) + 10;
3095 new_name
= bfd_malloc (amt
);
3096 if (new_name
== NULL
)
3098 sprintf (new_name
, "%s_%08x", sym_name
, sym_sec
->id
);
3099 sym_name
= new_name
;
3101 elftab
= & hash_table
->static_hash_table
->root
;
3102 sym_hash
= (struct elf32_mn10300_link_hash_entry
*)
3103 elf_link_hash_lookup (elftab
, sym_name
,
3104 FALSE
, FALSE
, FALSE
);
3107 if (sym_hash
== NULL
)
3110 if (! (sym_hash
->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
3111 && ! (sym_hash
->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
3115 /* Note that we've changed things. */
3116 elf_section_data (section
)->relocs
= internal_relocs
;
3117 elf_section_data (section
)->this_hdr
.contents
= contents
;
3118 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3120 /* Count how many bytes we're going to delete. */
3121 if (sym_hash
->movm_args
)
3124 if (sym_hash
->stack_size
> 0)
3126 if (sym_hash
->stack_size
<= 128)
3132 /* Note that we've deleted prologue bytes for this
3134 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
3136 /* Actually delete the bytes. */
3137 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
3143 /* Something changed. Not strictly necessary, but
3144 may lead to more relaxing opportunities. */
3149 /* Look for any global functions in this section which
3150 need insns deleted from their prologues. */
3151 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
3152 - symtab_hdr
->sh_info
);
3153 hashes
= elf_sym_hashes (input_bfd
);
3154 end_hashes
= hashes
+ symcount
;
3155 for (; hashes
< end_hashes
; hashes
++)
3157 struct elf32_mn10300_link_hash_entry
*sym_hash
;
3159 sym_hash
= (struct elf32_mn10300_link_hash_entry
*) *hashes
;
3160 if ((sym_hash
->root
.root
.type
== bfd_link_hash_defined
3161 || sym_hash
->root
.root
.type
== bfd_link_hash_defweak
)
3162 && sym_hash
->root
.root
.u
.def
.section
== section
3163 && ! (sym_hash
->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
3164 && ! (sym_hash
->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
3168 struct elf_link_hash_entry
**hh
;
3170 /* Note that we've changed things. */
3171 elf_section_data (section
)->relocs
= internal_relocs
;
3172 elf_section_data (section
)->this_hdr
.contents
= contents
;
3173 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3175 /* Count how many bytes we're going to delete. */
3176 if (sym_hash
->movm_args
)
3179 if (sym_hash
->stack_size
> 0)
3181 if (sym_hash
->stack_size
<= 128)
3187 /* Note that we've deleted prologue bytes for this
3189 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
3191 /* Actually delete the bytes. */
3192 symval
= sym_hash
->root
.root
.u
.def
.value
;
3193 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
3199 /* There may be other C++ functions symbols with the same
3200 address. If so then mark these as having had their
3201 prologue bytes deleted as well. */
3202 for (hh
= elf_sym_hashes (input_bfd
); hh
< end_hashes
; hh
++)
3204 struct elf32_mn10300_link_hash_entry
*h
;
3206 h
= (struct elf32_mn10300_link_hash_entry
*) * hh
;
3209 && (h
->root
.root
.type
== bfd_link_hash_defined
3210 || h
->root
.root
.type
== bfd_link_hash_defweak
)
3211 && h
->root
.root
.u
.def
.section
== section
3212 && ! (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
3213 && h
->root
.root
.u
.def
.value
== symval
3214 && h
->root
.type
== STT_FUNC
)
3215 h
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
3218 /* Something changed. Not strictly necessary, but
3219 may lead to more relaxing opportunities. */
3224 /* Cache or free any memory we allocated for the relocs. */
3225 if (internal_relocs
!= NULL
3226 && elf_section_data (section
)->relocs
!= internal_relocs
)
3227 free (internal_relocs
);
3228 internal_relocs
= NULL
;
3230 /* Cache or free any memory we allocated for the contents. */
3231 if (contents
!= NULL
3232 && elf_section_data (section
)->this_hdr
.contents
!= contents
)
3234 if (! link_info
->keep_memory
)
3237 /* Cache the section contents for elf_link_input_bfd. */
3238 elf_section_data (section
)->this_hdr
.contents
= contents
;
3243 /* Cache or free any memory we allocated for the symbols. */
3245 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3247 if (! link_info
->keep_memory
)
3250 /* Cache the symbols for elf_link_input_bfd. */
3251 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3257 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
3259 internal_relocs
= NULL
;
3261 /* For error_return. */
3264 /* We don't have to do anything for a relocatable link, if
3265 this section does not have relocs, or if this is not a
3267 if (bfd_link_relocatable (link_info
)
3268 || (sec
->flags
& SEC_RELOC
) == 0
3269 || sec
->reloc_count
== 0
3270 || (sec
->flags
& SEC_CODE
) == 0)
3273 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3275 /* Get a copy of the native relocations. */
3276 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
3277 link_info
->keep_memory
);
3278 if (internal_relocs
== NULL
)
3281 /* Scan for worst case alignment gap changes. Note that this logic
3282 is not ideal; what we should do is run this scan for every
3283 opcode/address range and adjust accordingly, but that's
3284 expensive. Worst case is that for an alignment of N bytes, we
3285 move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc
3286 all before it. Plus, this still doesn't cover cross-section
3287 jumps with section alignment. */
3288 irelend
= internal_relocs
+ sec
->reloc_count
;
3289 align_gap_adjustment
= 0;
3290 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
3292 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_ALIGN
)
3294 bfd_vma adj
= 1 << irel
->r_addend
;
3295 bfd_vma aend
= irel
->r_offset
;
3297 aend
= BFD_ALIGN (aend
, 1 << irel
->r_addend
);
3298 adj
= 2 * adj
- adj
- 1;
3300 /* Record the biggest adjustmnet. Skip any alignment at the
3301 end of our section. */
3302 if (align_gap_adjustment
< adj
3303 && aend
< sec
->output_section
->vma
+ sec
->output_offset
+ sec
->size
)
3304 align_gap_adjustment
= adj
;
3308 /* Walk through them looking for relaxing opportunities. */
3309 irelend
= internal_relocs
+ sec
->reloc_count
;
3310 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
3313 bfd_signed_vma jump_offset
;
3314 asection
*sym_sec
= NULL
;
3315 struct elf32_mn10300_link_hash_entry
*h
= NULL
;
3317 /* If this isn't something that can be relaxed, then ignore
3319 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_NONE
3320 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_8
3321 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_MAX
)
3324 /* Get the section contents if we haven't done so already. */
3325 if (contents
== NULL
)
3327 /* Get cached copy if it exists. */
3328 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
3329 contents
= elf_section_data (sec
)->this_hdr
.contents
;
3332 /* Go get them off disk. */
3333 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
3338 /* Read this BFD's symbols if we haven't done so already. */
3339 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
3341 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3342 if (isymbuf
== NULL
)
3343 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
3344 symtab_hdr
->sh_info
, 0,
3346 if (isymbuf
== NULL
)
3350 /* Get the value of the symbol referred to by the reloc. */
3351 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
3353 Elf_Internal_Sym
*isym
;
3354 const char *sym_name
;
3357 /* A local symbol. */
3358 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
3359 if (isym
->st_shndx
== SHN_UNDEF
)
3360 sym_sec
= bfd_und_section_ptr
;
3361 else if (isym
->st_shndx
== SHN_ABS
)
3362 sym_sec
= bfd_abs_section_ptr
;
3363 else if (isym
->st_shndx
== SHN_COMMON
)
3364 sym_sec
= bfd_com_section_ptr
;
3366 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3368 sym_name
= bfd_elf_string_from_elf_section (abfd
,
3369 symtab_hdr
->sh_link
,
3372 if ((sym_sec
->flags
& SEC_MERGE
)
3373 && sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3375 symval
= isym
->st_value
;
3377 /* GAS may reduce relocations against symbols in SEC_MERGE
3378 sections to a relocation against the section symbol when
3379 the original addend was zero. When the reloc is against
3380 a section symbol we should include the addend in the
3381 offset passed to _bfd_merged_section_offset, since the
3382 location of interest is the original symbol. On the
3383 other hand, an access to "sym+addend" where "sym" is not
3384 a section symbol should not include the addend; Such an
3385 access is presumed to be an offset from "sym"; The
3386 location of interest is just "sym". */
3387 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
3388 symval
+= irel
->r_addend
;
3390 symval
= _bfd_merged_section_offset (abfd
, & sym_sec
,
3391 elf_section_data (sym_sec
)->sec_info
,
3394 if (ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
3395 symval
+= irel
->r_addend
;
3397 symval
+= sym_sec
->output_section
->vma
3398 + sym_sec
->output_offset
- irel
->r_addend
;
3401 symval
= (isym
->st_value
3402 + sym_sec
->output_section
->vma
3403 + sym_sec
->output_offset
);
3405 /* Tack on an ID so we can uniquely identify this
3406 local symbol in the global hash table. */
3407 new_name
= bfd_malloc ((bfd_size_type
) strlen (sym_name
) + 10);
3408 if (new_name
== NULL
)
3410 sprintf (new_name
, "%s_%08x", sym_name
, sym_sec
->id
);
3411 sym_name
= new_name
;
3413 h
= (struct elf32_mn10300_link_hash_entry
*)
3414 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
3415 sym_name
, FALSE
, FALSE
, FALSE
);
3422 /* An external symbol. */
3423 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
3424 h
= (struct elf32_mn10300_link_hash_entry
*)
3425 (elf_sym_hashes (abfd
)[indx
]);
3426 BFD_ASSERT (h
!= NULL
);
3427 if (h
->root
.root
.type
!= bfd_link_hash_defined
3428 && h
->root
.root
.type
!= bfd_link_hash_defweak
)
3429 /* This appears to be a reference to an undefined
3430 symbol. Just ignore it--it will be caught by the
3431 regular reloc processing. */
3434 /* Check for a reference to a discarded symbol and ignore it. */
3435 if (h
->root
.root
.u
.def
.section
->output_section
== NULL
)
3438 sym_sec
= h
->root
.root
.u
.def
.section
->output_section
;
3440 symval
= (h
->root
.root
.u
.def
.value
3441 + h
->root
.root
.u
.def
.section
->output_section
->vma
3442 + h
->root
.root
.u
.def
.section
->output_offset
);
3445 /* For simplicity of coding, we are going to modify the section
3446 contents, the section relocs, and the BFD symbol table. We
3447 must tell the rest of the code not to free up this
3448 information. It would be possible to instead create a table
3449 of changes which have to be made, as is done in coff-mips.c;
3450 that would be more work, but would require less memory when
3451 the linker is run. */
3453 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
3454 branch/call, also deal with "call" -> "calls" conversions and
3455 insertion of prologue data into "call" instructions. */
3456 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL32
3457 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PLT32
)
3459 bfd_vma value
= symval
;
3461 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PLT32
3463 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_INTERNAL
3464 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_HIDDEN
3465 && h
->root
.plt
.offset
!= (bfd_vma
) -1)
3469 splt
= hash_table
->root
.splt
;
3470 value
= ((splt
->output_section
->vma
3471 + splt
->output_offset
3472 + h
->root
.plt
.offset
)
3473 - (sec
->output_section
->vma
3474 + sec
->output_offset
3478 /* If we've got a "call" instruction that needs to be turned
3479 into a "calls" instruction, do so now. It saves a byte. */
3480 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
3484 /* Get the opcode. */
3485 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3487 /* Make sure we're working with a "call" instruction! */
3490 /* Note that we've changed the relocs, section contents,
3492 elf_section_data (sec
)->relocs
= internal_relocs
;
3493 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3494 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3496 /* Fix the opcode. */
3497 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 1);
3498 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
3500 /* Fix irel->r_offset and irel->r_addend. */
3501 irel
->r_offset
+= 1;
3502 irel
->r_addend
+= 1;
3504 /* Delete one byte of data. */
3505 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3506 irel
->r_offset
+ 3, 1))
3509 /* That will change things, so, we should relax again.
3510 Note that this is not required, and it may be slow. */
3516 /* We've got a "call" instruction which needs some data
3517 from target function filled in. */
3520 /* Get the opcode. */
3521 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3523 /* Insert data from the target function into the "call"
3524 instruction if needed. */
3527 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 4);
3528 bfd_put_8 (abfd
, h
->stack_size
+ h
->movm_stack_size
,
3529 contents
+ irel
->r_offset
+ 5);
3533 /* Deal with pc-relative gunk. */
3534 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
3535 value
-= irel
->r_offset
;
3536 value
+= irel
->r_addend
;
3538 /* See if the value will fit in 16 bits, note the high value is
3539 0x7fff + 2 as the target will be two bytes closer if we are
3540 able to relax, if it's in the same section. */
3541 if (sec
->output_section
== sym_sec
->output_section
)
3542 jump_offset
= 0x8001;
3544 jump_offset
= 0x7fff;
3546 /* Account for jumps across alignment boundaries using
3547 align_gap_adjustment. */
3548 if ((bfd_signed_vma
) value
< jump_offset
- (bfd_signed_vma
) align_gap_adjustment
3549 && ((bfd_signed_vma
) value
> -0x8000 + (bfd_signed_vma
) align_gap_adjustment
))
3553 /* Get the opcode. */
3554 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3556 if (code
!= 0xdc && code
!= 0xdd && code
!= 0xff)
3559 /* Note that we've changed the relocs, section contents, etc. */
3560 elf_section_data (sec
)->relocs
= internal_relocs
;
3561 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3562 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3564 /* Fix the opcode. */
3566 bfd_put_8 (abfd
, 0xcc, contents
+ irel
->r_offset
- 1);
3567 else if (code
== 0xdd)
3568 bfd_put_8 (abfd
, 0xcd, contents
+ irel
->r_offset
- 1);
3569 else if (code
== 0xff)
3570 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3572 /* Fix the relocation's type. */
3573 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3574 (ELF32_R_TYPE (irel
->r_info
)
3575 == (int) R_MN10300_PLT32
)
3579 /* Delete two bytes of data. */
3580 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3581 irel
->r_offset
+ 1, 2))
3584 /* That will change things, so, we should relax again.
3585 Note that this is not required, and it may be slow. */
3590 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
3592 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL16
)
3594 bfd_vma value
= symval
;
3596 /* If we've got a "call" instruction that needs to be turned
3597 into a "calls" instruction, do so now. It saves a byte. */
3598 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
3602 /* Get the opcode. */
3603 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3605 /* Make sure we're working with a "call" instruction! */
3608 /* Note that we've changed the relocs, section contents,
3610 elf_section_data (sec
)->relocs
= internal_relocs
;
3611 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3612 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3614 /* Fix the opcode. */
3615 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 1);
3616 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
3618 /* Fix irel->r_offset and irel->r_addend. */
3619 irel
->r_offset
+= 1;
3620 irel
->r_addend
+= 1;
3622 /* Delete one byte of data. */
3623 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3624 irel
->r_offset
+ 1, 1))
3627 /* That will change things, so, we should relax again.
3628 Note that this is not required, and it may be slow. */
3636 /* Get the opcode. */
3637 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3639 /* Insert data from the target function into the "call"
3640 instruction if needed. */
3643 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 2);
3644 bfd_put_8 (abfd
, h
->stack_size
+ h
->movm_stack_size
,
3645 contents
+ irel
->r_offset
+ 3);
3649 /* Deal with pc-relative gunk. */
3650 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
3651 value
-= irel
->r_offset
;
3652 value
+= irel
->r_addend
;
3654 /* See if the value will fit in 8 bits, note the high value is
3655 0x7f + 1 as the target will be one bytes closer if we are
3657 if ((long) value
< 0x80 && (long) value
> -0x80)
3661 /* Get the opcode. */
3662 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3667 /* Note that we've changed the relocs, section contents, etc. */
3668 elf_section_data (sec
)->relocs
= internal_relocs
;
3669 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3670 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3672 /* Fix the opcode. */
3673 bfd_put_8 (abfd
, 0xca, contents
+ irel
->r_offset
- 1);
3675 /* Fix the relocation's type. */
3676 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3679 /* Delete one byte of data. */
3680 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3681 irel
->r_offset
+ 1, 1))
3684 /* That will change things, so, we should relax again.
3685 Note that this is not required, and it may be slow. */
3690 /* Try to eliminate an unconditional 8 bit pc-relative branch
3691 which immediately follows a conditional 8 bit pc-relative
3692 branch around the unconditional branch.
3699 This happens when the bCC can't reach lab2 at assembly time,
3700 but due to other relaxations it can reach at link time. */
3701 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL8
)
3703 Elf_Internal_Rela
*nrel
;
3704 bfd_vma value
= symval
;
3707 /* Deal with pc-relative gunk. */
3708 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
3709 value
-= irel
->r_offset
;
3710 value
+= irel
->r_addend
;
3712 /* Do nothing if this reloc is the last byte in the section. */
3713 if (irel
->r_offset
== sec
->size
)
3716 /* See if the next instruction is an unconditional pc-relative
3717 branch, more often than not this test will fail, so we
3718 test it first to speed things up. */
3719 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
3723 /* Also make sure the next relocation applies to the next
3724 instruction and that it's a pc-relative 8 bit branch. */
3727 || irel
->r_offset
+ 2 != nrel
->r_offset
3728 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10300_PCREL8
)
3731 /* Make sure our destination immediately follows the
3732 unconditional branch. */
3733 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
3734 + irel
->r_offset
+ 3))
3737 /* Now make sure we are a conditional branch. This may not
3738 be necessary, but why take the chance.
3740 Note these checks assume that R_MN10300_PCREL8 relocs
3741 only occur on bCC and bCCx insns. If they occured
3742 elsewhere, we'd need to know the start of this insn
3743 for this check to be accurate. */
3744 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3745 if (code
!= 0xc0 && code
!= 0xc1 && code
!= 0xc2
3746 && code
!= 0xc3 && code
!= 0xc4 && code
!= 0xc5
3747 && code
!= 0xc6 && code
!= 0xc7 && code
!= 0xc8
3748 && code
!= 0xc9 && code
!= 0xe8 && code
!= 0xe9
3749 && code
!= 0xea && code
!= 0xeb)
3752 /* We also have to be sure there is no symbol/label
3753 at the unconditional branch. */
3754 if (mn10300_elf_symbol_address_p (abfd
, sec
, isymbuf
,
3755 irel
->r_offset
+ 1))
3758 /* Note that we've changed the relocs, section contents, etc. */
3759 elf_section_data (sec
)->relocs
= internal_relocs
;
3760 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3761 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3763 /* Reverse the condition of the first branch. */
3809 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
3811 /* Set the reloc type and symbol for the first branch
3812 from the second branch. */
3813 irel
->r_info
= nrel
->r_info
;
3815 /* Make the reloc for the second branch a null reloc. */
3816 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
3819 /* Delete two bytes of data. */
3820 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3821 irel
->r_offset
+ 1, 2))
3824 /* That will change things, so, we should relax again.
3825 Note that this is not required, and it may be slow. */
3829 /* Try to turn a 24 immediate, displacement or absolute address
3830 into a 8 immediate, displacement or absolute address. */
3831 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_24
)
3833 bfd_vma value
= symval
;
3834 value
+= irel
->r_addend
;
3836 /* See if the value will fit in 8 bits. */
3837 if ((long) value
< 0x7f && (long) value
> -0x80)
3841 /* AM33 insns which have 24 operands are 6 bytes long and
3842 will have 0xfd as the first byte. */
3844 /* Get the first opcode. */
3845 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 3);
3849 /* Get the second opcode. */
3850 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
3852 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3853 equivalent instructions exists. */
3854 if (code
!= 0x6b && code
!= 0x7b
3855 && code
!= 0x8b && code
!= 0x9b
3856 && ((code
& 0x0f) == 0x09 || (code
& 0x0f) == 0x08
3857 || (code
& 0x0f) == 0x0a || (code
& 0x0f) == 0x0b
3858 || (code
& 0x0f) == 0x0e))
3860 /* Not safe if the high bit is on as relaxing may
3861 move the value out of high mem and thus not fit
3862 in a signed 8bit value. This is currently over
3864 if ((value
& 0x80) == 0)
3866 /* Note that we've changed the relocation contents,
3868 elf_section_data (sec
)->relocs
= internal_relocs
;
3869 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3870 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3872 /* Fix the opcode. */
3873 bfd_put_8 (abfd
, 0xfb, contents
+ irel
->r_offset
- 3);
3874 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
3876 /* Fix the relocation's type. */
3878 ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3881 /* Delete two bytes of data. */
3882 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3883 irel
->r_offset
+ 1, 2))
3886 /* That will change things, so, we should relax
3887 again. Note that this is not required, and it
3897 /* Try to turn a 32bit immediate, displacement or absolute address
3898 into a 16bit immediate, displacement or absolute address. */
3899 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_32
3900 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOT32
3901 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTOFF32
)
3903 bfd_vma value
= symval
;
3905 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_MN10300_32
)
3909 sgot
= hash_table
->root
.sgot
;
3910 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOT32
)
3912 value
= sgot
->output_offset
;
3915 value
+= h
->root
.got
.offset
;
3917 value
+= (elf_local_got_offsets
3918 (abfd
)[ELF32_R_SYM (irel
->r_info
)]);
3920 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTOFF32
)
3921 value
-= sgot
->output_section
->vma
;
3922 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTPC32
)
3923 value
= (sgot
->output_section
->vma
3924 - (sec
->output_section
->vma
3925 + sec
->output_offset
3931 value
+= irel
->r_addend
;
3933 /* See if the value will fit in 24 bits.
3934 We allow any 16bit match here. We prune those we can't
3936 if ((long) value
< 0x7fffff && (long) value
> -0x800000)
3940 /* AM33 insns which have 32bit operands are 7 bytes long and
3941 will have 0xfe as the first byte. */
3943 /* Get the first opcode. */
3944 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 3);
3948 /* Get the second opcode. */
3949 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
3951 /* All the am33 32 -> 24 relaxing possibilities. */
3952 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3953 equivalent instructions exists. */
3954 if (code
!= 0x6b && code
!= 0x7b
3955 && code
!= 0x8b && code
!= 0x9b
3956 && (ELF32_R_TYPE (irel
->r_info
)
3957 != (int) R_MN10300_GOTPC32
)
3958 && ((code
& 0x0f) == 0x09 || (code
& 0x0f) == 0x08
3959 || (code
& 0x0f) == 0x0a || (code
& 0x0f) == 0x0b
3960 || (code
& 0x0f) == 0x0e))
3962 /* Not safe if the high bit is on as relaxing may
3963 move the value out of high mem and thus not fit
3964 in a signed 16bit value. This is currently over
3966 if ((value
& 0x8000) == 0)
3968 /* Note that we've changed the relocation contents,
3970 elf_section_data (sec
)->relocs
= internal_relocs
;
3971 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3972 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3974 /* Fix the opcode. */
3975 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 3);
3976 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
3978 /* Fix the relocation's type. */
3980 ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3981 (ELF32_R_TYPE (irel
->r_info
)
3982 == (int) R_MN10300_GOTOFF32
)
3983 ? R_MN10300_GOTOFF24
3984 : (ELF32_R_TYPE (irel
->r_info
)
3985 == (int) R_MN10300_GOT32
)
3989 /* Delete one byte of data. */
3990 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3991 irel
->r_offset
+ 3, 1))
3994 /* That will change things, so, we should relax
3995 again. Note that this is not required, and it
4004 /* See if the value will fit in 16 bits.
4005 We allow any 16bit match here. We prune those we can't
4007 if ((long) value
< 0x7fff && (long) value
> -0x8000)
4011 /* Most insns which have 32bit operands are 6 bytes long;
4012 exceptions are pcrel insns and bit insns.
4014 We handle pcrel insns above. We don't bother trying
4015 to handle the bit insns here.
4017 The first byte of the remaining insns will be 0xfc. */
4019 /* Get the first opcode. */
4020 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
4025 /* Get the second opcode. */
4026 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
4028 if ((code
& 0xf0) < 0x80)
4029 switch (code
& 0xf0)
4031 /* mov (d32,am),dn -> mov (d32,am),dn
4032 mov dm,(d32,am) -> mov dn,(d32,am)
4033 mov (d32,am),an -> mov (d32,am),an
4034 mov dm,(d32,am) -> mov dn,(d32,am)
4035 movbu (d32,am),dn -> movbu (d32,am),dn
4036 movbu dm,(d32,am) -> movbu dn,(d32,am)
4037 movhu (d32,am),dn -> movhu (d32,am),dn
4038 movhu dm,(d32,am) -> movhu dn,(d32,am) */
4047 /* Not safe if the high bit is on as relaxing may
4048 move the value out of high mem and thus not fit
4049 in a signed 16bit value. */
4051 && (value
& 0x8000))
4054 /* Note that we've changed the relocation contents, etc. */
4055 elf_section_data (sec
)->relocs
= internal_relocs
;
4056 elf_section_data (sec
)->this_hdr
.contents
= contents
;
4057 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
4059 /* Fix the opcode. */
4060 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
4061 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
4063 /* Fix the relocation's type. */
4064 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
4065 (ELF32_R_TYPE (irel
->r_info
)
4066 == (int) R_MN10300_GOTOFF32
)
4067 ? R_MN10300_GOTOFF16
4068 : (ELF32_R_TYPE (irel
->r_info
)
4069 == (int) R_MN10300_GOT32
)
4071 : (ELF32_R_TYPE (irel
->r_info
)
4072 == (int) R_MN10300_GOTPC32
)
4073 ? R_MN10300_GOTPC16
:
4076 /* Delete two bytes of data. */
4077 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
4078 irel
->r_offset
+ 2, 2))
4081 /* That will change things, so, we should relax again.
4082 Note that this is not required, and it may be slow. */
4086 else if ((code
& 0xf0) == 0x80
4087 || (code
& 0xf0) == 0x90)
4088 switch (code
& 0xf3)
4090 /* mov dn,(abs32) -> mov dn,(abs16)
4091 movbu dn,(abs32) -> movbu dn,(abs16)
4092 movhu dn,(abs32) -> movhu dn,(abs16) */
4096 /* Note that we've changed the relocation contents, etc. */
4097 elf_section_data (sec
)->relocs
= internal_relocs
;
4098 elf_section_data (sec
)->this_hdr
.contents
= contents
;
4099 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
4101 if ((code
& 0xf3) == 0x81)
4102 code
= 0x01 + (code
& 0x0c);
4103 else if ((code
& 0xf3) == 0x82)
4104 code
= 0x02 + (code
& 0x0c);
4105 else if ((code
& 0xf3) == 0x83)
4106 code
= 0x03 + (code
& 0x0c);
4110 /* Fix the opcode. */
4111 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
4113 /* Fix the relocation's type. */
4114 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
4115 (ELF32_R_TYPE (irel
->r_info
)
4116 == (int) R_MN10300_GOTOFF32
)
4117 ? R_MN10300_GOTOFF16
4118 : (ELF32_R_TYPE (irel
->r_info
)
4119 == (int) R_MN10300_GOT32
)
4121 : (ELF32_R_TYPE (irel
->r_info
)
4122 == (int) R_MN10300_GOTPC32
)
4123 ? R_MN10300_GOTPC16
:
4126 /* The opcode got shorter too, so we have to fix the
4127 addend and offset too! */
4128 irel
->r_offset
-= 1;
4130 /* Delete three bytes of data. */
4131 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
4132 irel
->r_offset
+ 1, 3))
4135 /* That will change things, so, we should relax again.
4136 Note that this is not required, and it may be slow. */
4140 /* mov am,(abs32) -> mov am,(abs16)
4141 mov am,(d32,sp) -> mov am,(d16,sp)
4142 mov dm,(d32,sp) -> mov dm,(d32,sp)
4143 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
4144 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
4150 /* sp-based offsets are zero-extended. */
4151 if (code
>= 0x90 && code
<= 0x93
4152 && (long) value
< 0)
4155 /* Note that we've changed the relocation contents, etc. */
4156 elf_section_data (sec
)->relocs
= internal_relocs
;
4157 elf_section_data (sec
)->this_hdr
.contents
= contents
;
4158 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
4160 /* Fix the opcode. */
4161 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
4162 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
4164 /* Fix the relocation's type. */
4165 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
4166 (ELF32_R_TYPE (irel
->r_info
)
4167 == (int) R_MN10300_GOTOFF32
)
4168 ? R_MN10300_GOTOFF16
4169 : (ELF32_R_TYPE (irel
->r_info
)
4170 == (int) R_MN10300_GOT32
)
4172 : (ELF32_R_TYPE (irel
->r_info
)
4173 == (int) R_MN10300_GOTPC32
)
4174 ? R_MN10300_GOTPC16
:
4177 /* Delete two bytes of data. */
4178 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
4179 irel
->r_offset
+ 2, 2))
4182 /* That will change things, so, we should relax again.
4183 Note that this is not required, and it may be slow. */
4187 else if ((code
& 0xf0) < 0xf0)
4188 switch (code
& 0xfc)
4190 /* mov imm32,dn -> mov imm16,dn
4191 mov imm32,an -> mov imm16,an
4192 mov (abs32),dn -> mov (abs16),dn
4193 movbu (abs32),dn -> movbu (abs16),dn
4194 movhu (abs32),dn -> movhu (abs16),dn */
4200 /* Not safe if the high bit is on as relaxing may
4201 move the value out of high mem and thus not fit
4202 in a signed 16bit value. */
4204 && (value
& 0x8000))
4207 /* "mov imm16, an" zero-extends the immediate. */
4208 if ((code
& 0xfc) == 0xdc
4209 && (long) value
< 0)
4212 /* Note that we've changed the relocation contents, etc. */
4213 elf_section_data (sec
)->relocs
= internal_relocs
;
4214 elf_section_data (sec
)->this_hdr
.contents
= contents
;
4215 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
4217 if ((code
& 0xfc) == 0xcc)
4218 code
= 0x2c + (code
& 0x03);
4219 else if ((code
& 0xfc) == 0xdc)
4220 code
= 0x24 + (code
& 0x03);
4221 else if ((code
& 0xfc) == 0xa4)
4222 code
= 0x30 + (code
& 0x03);
4223 else if ((code
& 0xfc) == 0xa8)
4224 code
= 0x34 + (code
& 0x03);
4225 else if ((code
& 0xfc) == 0xac)
4226 code
= 0x38 + (code
& 0x03);
4230 /* Fix the opcode. */
4231 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
4233 /* Fix the relocation's type. */
4234 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
4235 (ELF32_R_TYPE (irel
->r_info
)
4236 == (int) R_MN10300_GOTOFF32
)
4237 ? R_MN10300_GOTOFF16
4238 : (ELF32_R_TYPE (irel
->r_info
)
4239 == (int) R_MN10300_GOT32
)
4241 : (ELF32_R_TYPE (irel
->r_info
)
4242 == (int) R_MN10300_GOTPC32
)
4243 ? R_MN10300_GOTPC16
:
4246 /* The opcode got shorter too, so we have to fix the
4247 addend and offset too! */
4248 irel
->r_offset
-= 1;
4250 /* Delete three bytes of data. */
4251 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
4252 irel
->r_offset
+ 1, 3))
4255 /* That will change things, so, we should relax again.
4256 Note that this is not required, and it may be slow. */
4260 /* mov (abs32),an -> mov (abs16),an
4261 mov (d32,sp),an -> mov (d16,sp),an
4262 mov (d32,sp),dn -> mov (d16,sp),dn
4263 movbu (d32,sp),dn -> movbu (d16,sp),dn
4264 movhu (d32,sp),dn -> movhu (d16,sp),dn
4265 add imm32,dn -> add imm16,dn
4266 cmp imm32,dn -> cmp imm16,dn
4267 add imm32,an -> add imm16,an
4268 cmp imm32,an -> cmp imm16,an
4269 and imm32,dn -> and imm16,dn
4270 or imm32,dn -> or imm16,dn
4271 xor imm32,dn -> xor imm16,dn
4272 btst imm32,dn -> btst imm16,dn */
4288 /* cmp imm16, an zero-extends the immediate. */
4290 && (long) value
< 0)
4293 /* So do sp-based offsets. */
4294 if (code
>= 0xb0 && code
<= 0xb3
4295 && (long) value
< 0)
4298 /* Note that we've changed the relocation contents, etc. */
4299 elf_section_data (sec
)->relocs
= internal_relocs
;
4300 elf_section_data (sec
)->this_hdr
.contents
= contents
;
4301 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
4303 /* Fix the opcode. */
4304 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
4305 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
4307 /* Fix the relocation's type. */
4308 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
4309 (ELF32_R_TYPE (irel
->r_info
)
4310 == (int) R_MN10300_GOTOFF32
)
4311 ? R_MN10300_GOTOFF16
4312 : (ELF32_R_TYPE (irel
->r_info
)
4313 == (int) R_MN10300_GOT32
)
4315 : (ELF32_R_TYPE (irel
->r_info
)
4316 == (int) R_MN10300_GOTPC32
)
4317 ? R_MN10300_GOTPC16
:
4320 /* Delete two bytes of data. */
4321 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
4322 irel
->r_offset
+ 2, 2))
4325 /* That will change things, so, we should relax again.
4326 Note that this is not required, and it may be slow. */
4330 else if (code
== 0xfe)
4332 /* add imm32,sp -> add imm16,sp */
4334 /* Note that we've changed the relocation contents, etc. */
4335 elf_section_data (sec
)->relocs
= internal_relocs
;
4336 elf_section_data (sec
)->this_hdr
.contents
= contents
;
4337 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
4339 /* Fix the opcode. */
4340 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
4341 bfd_put_8 (abfd
, 0xfe, contents
+ irel
->r_offset
- 1);
4343 /* Fix the relocation's type. */
4344 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
4345 (ELF32_R_TYPE (irel
->r_info
)
4346 == (int) R_MN10300_GOT32
)
4348 : (ELF32_R_TYPE (irel
->r_info
)
4349 == (int) R_MN10300_GOTOFF32
)
4350 ? R_MN10300_GOTOFF16
4351 : (ELF32_R_TYPE (irel
->r_info
)
4352 == (int) R_MN10300_GOTPC32
)
4353 ? R_MN10300_GOTPC16
:
4356 /* Delete two bytes of data. */
4357 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
4358 irel
->r_offset
+ 2, 2))
4361 /* That will change things, so, we should relax again.
4362 Note that this is not required, and it may be slow. */
4371 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
4373 if (! link_info
->keep_memory
)
4377 /* Cache the symbols for elf_link_input_bfd. */
4378 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
4382 if (contents
!= NULL
4383 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
4385 if (! link_info
->keep_memory
)
4389 /* Cache the section contents for elf_link_input_bfd. */
4390 elf_section_data (sec
)->this_hdr
.contents
= contents
;
4394 if (internal_relocs
!= NULL
4395 && elf_section_data (sec
)->relocs
!= internal_relocs
)
4396 free (internal_relocs
);
4402 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
4404 if (contents
!= NULL
4405 && elf_section_data (section
)->this_hdr
.contents
!= contents
)
4407 if (internal_relocs
!= NULL
4408 && elf_section_data (section
)->relocs
!= internal_relocs
)
4409 free (internal_relocs
);
4414 /* This is a version of bfd_generic_get_relocated_section_contents
4415 which uses mn10300_elf_relocate_section. */
4418 mn10300_elf_get_relocated_section_contents (bfd
*output_bfd
,
4419 struct bfd_link_info
*link_info
,
4420 struct bfd_link_order
*link_order
,
4422 bfd_boolean relocatable
,
4425 Elf_Internal_Shdr
*symtab_hdr
;
4426 asection
*input_section
= link_order
->u
.indirect
.section
;
4427 bfd
*input_bfd
= input_section
->owner
;
4428 asection
**sections
= NULL
;
4429 Elf_Internal_Rela
*internal_relocs
= NULL
;
4430 Elf_Internal_Sym
*isymbuf
= NULL
;
4432 /* We only need to handle the case of relaxing, or of having a
4433 particular set of section contents, specially. */
4435 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
4436 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
4441 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4443 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
4444 (size_t) input_section
->size
);
4446 if ((input_section
->flags
& SEC_RELOC
) != 0
4447 && input_section
->reloc_count
> 0)
4450 Elf_Internal_Sym
*isym
, *isymend
;
4453 internal_relocs
= _bfd_elf_link_read_relocs (input_bfd
, input_section
,
4455 if (internal_relocs
== NULL
)
4458 if (symtab_hdr
->sh_info
!= 0)
4460 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
4461 if (isymbuf
== NULL
)
4462 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
4463 symtab_hdr
->sh_info
, 0,
4465 if (isymbuf
== NULL
)
4469 amt
= symtab_hdr
->sh_info
;
4470 amt
*= sizeof (asection
*);
4471 sections
= bfd_malloc (amt
);
4472 if (sections
== NULL
&& amt
!= 0)
4475 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
4476 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
4480 if (isym
->st_shndx
== SHN_UNDEF
)
4481 isec
= bfd_und_section_ptr
;
4482 else if (isym
->st_shndx
== SHN_ABS
)
4483 isec
= bfd_abs_section_ptr
;
4484 else if (isym
->st_shndx
== SHN_COMMON
)
4485 isec
= bfd_com_section_ptr
;
4487 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
4492 if (! mn10300_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
4493 input_section
, data
, internal_relocs
,
4497 if (sections
!= NULL
)
4499 if (isymbuf
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
4501 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
4502 free (internal_relocs
);
4508 if (sections
!= NULL
)
4510 if (isymbuf
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
4512 if (internal_relocs
!= NULL
4513 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
4514 free (internal_relocs
);
4518 /* Assorted hash table functions. */
4520 /* Initialize an entry in the link hash table. */
4522 /* Create an entry in an MN10300 ELF linker hash table. */
4524 static struct bfd_hash_entry
*
4525 elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry
*entry
,
4526 struct bfd_hash_table
*table
,
4529 struct elf32_mn10300_link_hash_entry
*ret
=
4530 (struct elf32_mn10300_link_hash_entry
*) entry
;
4532 /* Allocate the structure if it has not already been allocated by a
4535 ret
= (struct elf32_mn10300_link_hash_entry
*)
4536 bfd_hash_allocate (table
, sizeof (* ret
));
4538 return (struct bfd_hash_entry
*) ret
;
4540 /* Call the allocation method of the superclass. */
4541 ret
= (struct elf32_mn10300_link_hash_entry
*)
4542 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
4546 ret
->direct_calls
= 0;
4547 ret
->stack_size
= 0;
4549 ret
->movm_stack_size
= 0;
4552 ret
->tls_type
= GOT_UNKNOWN
;
4555 return (struct bfd_hash_entry
*) ret
;
4559 _bfd_mn10300_copy_indirect_symbol (struct bfd_link_info
* info
,
4560 struct elf_link_hash_entry
* dir
,
4561 struct elf_link_hash_entry
* ind
)
4563 struct elf32_mn10300_link_hash_entry
* edir
;
4564 struct elf32_mn10300_link_hash_entry
* eind
;
4566 edir
= elf_mn10300_hash_entry (dir
);
4567 eind
= elf_mn10300_hash_entry (ind
);
4569 if (ind
->root
.type
== bfd_link_hash_indirect
4570 && dir
->got
.refcount
<= 0)
4572 edir
->tls_type
= eind
->tls_type
;
4573 eind
->tls_type
= GOT_UNKNOWN
;
4575 edir
->direct_calls
= eind
->direct_calls
;
4576 edir
->stack_size
= eind
->stack_size
;
4577 edir
->movm_args
= eind
->movm_args
;
4578 edir
->movm_stack_size
= eind
->movm_stack_size
;
4579 edir
->flags
= eind
->flags
;
4581 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
4584 /* Destroy an mn10300 ELF linker hash table. */
4587 elf32_mn10300_link_hash_table_free (bfd
*obfd
)
4589 struct elf32_mn10300_link_hash_table
*ret
4590 = (struct elf32_mn10300_link_hash_table
*) obfd
->link
.hash
;
4592 obfd
->link
.hash
= &ret
->static_hash_table
->root
.root
;
4593 _bfd_elf_link_hash_table_free (obfd
);
4594 obfd
->is_linker_output
= TRUE
;
4595 obfd
->link
.hash
= &ret
->root
.root
;
4596 _bfd_elf_link_hash_table_free (obfd
);
4599 /* Create an mn10300 ELF linker hash table. */
4601 static struct bfd_link_hash_table
*
4602 elf32_mn10300_link_hash_table_create (bfd
*abfd
)
4604 struct elf32_mn10300_link_hash_table
*ret
;
4605 bfd_size_type amt
= sizeof (* ret
);
4607 ret
= bfd_zmalloc (amt
);
4611 amt
= sizeof (struct elf_link_hash_table
);
4612 ret
->static_hash_table
= bfd_zmalloc (amt
);
4613 if (ret
->static_hash_table
== NULL
)
4619 if (!_bfd_elf_link_hash_table_init (&ret
->static_hash_table
->root
, abfd
,
4620 elf32_mn10300_link_hash_newfunc
,
4621 sizeof (struct elf32_mn10300_link_hash_entry
),
4624 free (ret
->static_hash_table
);
4629 abfd
->is_linker_output
= FALSE
;
4630 abfd
->link
.hash
= NULL
;
4631 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
4632 elf32_mn10300_link_hash_newfunc
,
4633 sizeof (struct elf32_mn10300_link_hash_entry
),
4636 abfd
->is_linker_output
= TRUE
;
4637 abfd
->link
.hash
= &ret
->static_hash_table
->root
.root
;
4638 _bfd_elf_link_hash_table_free (abfd
);
4642 ret
->root
.root
.hash_table_free
= elf32_mn10300_link_hash_table_free
;
4644 ret
->tls_ldm_got
.offset
= -1;
4646 return & ret
->root
.root
;
4649 static unsigned long
4650 elf_mn10300_mach (flagword flags
)
4652 switch (flags
& EF_MN10300_MACH
)
4654 case E_MN10300_MACH_MN10300
:
4656 return bfd_mach_mn10300
;
4658 case E_MN10300_MACH_AM33
:
4659 return bfd_mach_am33
;
4661 case E_MN10300_MACH_AM33_2
:
4662 return bfd_mach_am33_2
;
4666 /* The final processing done just before writing out a MN10300 ELF object
4667 file. This gets the MN10300 architecture right based on the machine
4671 _bfd_mn10300_elf_final_write_processing (bfd
*abfd
)
4675 switch (bfd_get_mach (abfd
))
4678 case bfd_mach_mn10300
:
4679 val
= E_MN10300_MACH_MN10300
;
4683 val
= E_MN10300_MACH_AM33
;
4686 case bfd_mach_am33_2
:
4687 val
= E_MN10300_MACH_AM33_2
;
4691 elf_elfheader (abfd
)->e_flags
&= ~ (EF_MN10300_MACH
);
4692 elf_elfheader (abfd
)->e_flags
|= val
;
4693 return _bfd_elf_final_write_processing (abfd
);
4697 _bfd_mn10300_elf_object_p (bfd
*abfd
)
4699 bfd_default_set_arch_mach (abfd
, bfd_arch_mn10300
,
4700 elf_mn10300_mach (elf_elfheader (abfd
)->e_flags
));
4704 /* Merge backend specific data from an object file to the output
4705 object file when linking. */
4708 _bfd_mn10300_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
4710 bfd
*obfd
= info
->output_bfd
;
4712 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4713 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4716 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
4717 && bfd_get_mach (obfd
) < bfd_get_mach (ibfd
))
4719 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
4720 bfd_get_mach (ibfd
)))
4727 #define PLT0_ENTRY_SIZE 15
4728 #define PLT_ENTRY_SIZE 20
4729 #define PIC_PLT_ENTRY_SIZE 24
4731 static const bfd_byte elf_mn10300_plt0_entry
[PLT0_ENTRY_SIZE
] =
4733 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
4734 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
4735 0xf0, 0xf4, /* jmp (a0) */
4738 static const bfd_byte elf_mn10300_plt_entry
[PLT_ENTRY_SIZE
] =
4740 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
4741 0xf0, 0xf4, /* jmp (a0) */
4742 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4743 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
4746 static const bfd_byte elf_mn10300_pic_plt_entry
[PIC_PLT_ENTRY_SIZE
] =
4748 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
4749 0xf0, 0xf4, /* jmp (a0) */
4750 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4751 0xf8, 0x22, 8, /* mov (8,a2),a0 */
4752 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
4753 0xf0, 0xf4, /* jmp (a0) */
4756 /* Return size of the first PLT entry. */
4757 #define elf_mn10300_sizeof_plt0(info) \
4758 (bfd_link_pic (info) ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4760 /* Return size of a PLT entry. */
4761 #define elf_mn10300_sizeof_plt(info) \
4762 (bfd_link_pic (info) ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4764 /* Return offset of the PLT0 address in an absolute PLT entry. */
4765 #define elf_mn10300_plt_plt0_offset(info) 16
4767 /* Return offset of the linker in PLT0 entry. */
4768 #define elf_mn10300_plt0_linker_offset(info) 2
4770 /* Return offset of the GOT id in PLT0 entry. */
4771 #define elf_mn10300_plt0_gotid_offset(info) 9
4773 /* Return offset of the temporary in PLT entry. */
4774 #define elf_mn10300_plt_temp_offset(info) 8
4776 /* Return offset of the symbol in PLT entry. */
4777 #define elf_mn10300_plt_symbol_offset(info) 2
4779 /* Return offset of the relocation in PLT entry. */
4780 #define elf_mn10300_plt_reloc_offset(info) 11
4782 /* The name of the dynamic interpreter. This is put in the .interp
4785 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4787 /* Create dynamic sections when linking against a dynamic object. */
4790 _bfd_mn10300_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
4794 const struct elf_backend_data
* bed
= get_elf_backend_data (abfd
);
4795 struct elf32_mn10300_link_hash_table
*htab
= elf32_mn10300_hash_table (info
);
4798 switch (bed
->s
->arch_size
)
4809 bfd_set_error (bfd_error_bad_value
);
4813 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4814 .rel[a].bss sections. */
4815 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
4816 | SEC_LINKER_CREATED
);
4818 s
= bfd_make_section_anyway_with_flags (abfd
,
4819 (bed
->default_use_rela_p
4820 ? ".rela.plt" : ".rel.plt"),
4821 flags
| SEC_READONLY
);
4822 htab
->root
.srelplt
= s
;
4824 || !bfd_set_section_alignment (s
, ptralign
))
4827 if (! _bfd_mn10300_elf_create_got_section (abfd
, info
))
4830 if (bed
->want_dynbss
)
4832 /* The .dynbss section is a place to put symbols which are defined
4833 by dynamic objects, are referenced by regular objects, and are
4834 not functions. We must allocate space for them in the process
4835 image and use a R_*_COPY reloc to tell the dynamic linker to
4836 initialize them at run time. The linker script puts the .dynbss
4837 section into the .bss section of the final image. */
4838 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
4839 SEC_ALLOC
| SEC_LINKER_CREATED
);
4843 /* The .rel[a].bss section holds copy relocs. This section is not
4844 normally needed. We need to create it here, though, so that the
4845 linker will map it to an output section. We can't just create it
4846 only if we need it, because we will not know whether we need it
4847 until we have seen all the input files, and the first time the
4848 main linker code calls BFD after examining all the input files
4849 (size_dynamic_sections) the input sections have already been
4850 mapped to the output sections. If the section turns out not to
4851 be needed, we can discard it later. We will never need this
4852 section when generating a shared object, since they do not use
4854 if (! bfd_link_pic (info
))
4856 s
= bfd_make_section_anyway_with_flags (abfd
,
4857 (bed
->default_use_rela_p
4858 ? ".rela.bss" : ".rel.bss"),
4859 flags
| SEC_READONLY
);
4861 || !bfd_set_section_alignment (s
, ptralign
))
4869 /* Adjust a symbol defined by a dynamic object and referenced by a
4870 regular object. The current definition is in some section of the
4871 dynamic object, but we're not including those sections. We have to
4872 change the definition to something the rest of the link can
4876 _bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info
* info
,
4877 struct elf_link_hash_entry
* h
)
4879 struct elf32_mn10300_link_hash_table
*htab
= elf32_mn10300_hash_table (info
);
4883 dynobj
= htab
->root
.dynobj
;
4885 /* Make sure we know what is going on here. */
4886 BFD_ASSERT (dynobj
!= NULL
4891 && !h
->def_regular
)));
4893 /* If this is a function, put it in the procedure linkage table. We
4894 will fill in the contents of the procedure linkage table later,
4895 when we know the address of the .got section. */
4896 if (h
->type
== STT_FUNC
4899 if (! bfd_link_pic (info
)
4903 /* This case can occur if we saw a PLT reloc in an input
4904 file, but the symbol was never referred to by a dynamic
4905 object. In such a case, we don't actually need to build
4906 a procedure linkage table, and we can just do a REL32
4908 BFD_ASSERT (h
->needs_plt
);
4912 /* Make sure this symbol is output as a dynamic symbol. */
4913 if (h
->dynindx
== -1)
4915 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4919 s
= htab
->root
.splt
;
4920 BFD_ASSERT (s
!= NULL
);
4922 /* If this is the first .plt entry, make room for the special
4925 s
->size
+= elf_mn10300_sizeof_plt0 (info
);
4927 /* If this symbol is not defined in a regular file, and we are
4928 not generating a shared library, then set the symbol to this
4929 location in the .plt. This is required to make function
4930 pointers compare as equal between the normal executable and
4931 the shared library. */
4932 if (! bfd_link_pic (info
)
4935 h
->root
.u
.def
.section
= s
;
4936 h
->root
.u
.def
.value
= s
->size
;
4939 h
->plt
.offset
= s
->size
;
4941 /* Make room for this entry. */
4942 s
->size
+= elf_mn10300_sizeof_plt (info
);
4944 /* We also need to make an entry in the .got.plt section, which
4945 will be placed in the .got section by the linker script. */
4946 s
= htab
->root
.sgotplt
;
4947 BFD_ASSERT (s
!= NULL
);
4950 /* We also need to make an entry in the .rela.plt section. */
4951 s
= htab
->root
.srelplt
;
4952 BFD_ASSERT (s
!= NULL
);
4953 s
->size
+= sizeof (Elf32_External_Rela
);
4958 /* If this is a weak symbol, and there is a real definition, the
4959 processor independent code will have arranged for us to see the
4960 real definition first, and we can just use the same value. */
4961 if (h
->is_weakalias
)
4963 struct elf_link_hash_entry
*def
= weakdef (h
);
4964 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
4965 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
4966 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
4970 /* This is a reference to a symbol defined by a dynamic object which
4971 is not a function. */
4973 /* If we are creating a shared library, we must presume that the
4974 only references to the symbol are via the global offset table.
4975 For such cases we need not do anything here; the relocations will
4976 be handled correctly by relocate_section. */
4977 if (bfd_link_pic (info
))
4980 /* If there are no references to this symbol that do not use the
4981 GOT, we don't need to generate a copy reloc. */
4982 if (!h
->non_got_ref
)
4985 /* We must allocate the symbol in our .dynbss section, which will
4986 become part of the .bss section of the executable. There will be
4987 an entry for this symbol in the .dynsym section. The dynamic
4988 object will contain position independent code, so all references
4989 from the dynamic object to this symbol will go through the global
4990 offset table. The dynamic linker will use the .dynsym entry to
4991 determine the address it must put in the global offset table, so
4992 both the dynamic object and the regular object will refer to the
4993 same memory location for the variable. */
4995 s
= bfd_get_linker_section (dynobj
, ".dynbss");
4996 BFD_ASSERT (s
!= NULL
);
4998 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4999 copy the initial value out of the dynamic object and into the
5000 runtime process image. We need to remember the offset into the
5001 .rela.bss section we are going to use. */
5002 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
5006 srel
= bfd_get_linker_section (dynobj
, ".rela.bss");
5007 BFD_ASSERT (srel
!= NULL
);
5008 srel
->size
+= sizeof (Elf32_External_Rela
);
5012 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
5015 /* Set the sizes of the dynamic sections. */
5018 _bfd_mn10300_elf_size_dynamic_sections (bfd
* output_bfd
,
5019 struct bfd_link_info
* info
)
5021 struct elf32_mn10300_link_hash_table
*htab
= elf32_mn10300_hash_table (info
);
5026 bfd_boolean reltext
;
5028 dynobj
= htab
->root
.dynobj
;
5029 BFD_ASSERT (dynobj
!= NULL
);
5031 if (elf_hash_table (info
)->dynamic_sections_created
)
5033 /* Set the contents of the .interp section to the interpreter. */
5034 if (bfd_link_executable (info
) && !info
->nointerp
)
5036 s
= bfd_get_linker_section (dynobj
, ".interp");
5037 BFD_ASSERT (s
!= NULL
);
5038 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
5039 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
5044 /* We may have created entries in the .rela.got section.
5045 However, if we are not creating the dynamic sections, we will
5046 not actually use these entries. Reset the size of .rela.got,
5047 which will cause it to get stripped from the output file
5049 s
= htab
->root
.sgot
;
5054 if (htab
->tls_ldm_got
.refcount
> 0)
5056 s
= htab
->root
.srelgot
;
5057 BFD_ASSERT (s
!= NULL
);
5058 s
->size
+= sizeof (Elf32_External_Rela
);
5061 /* The check_relocs and adjust_dynamic_symbol entry points have
5062 determined the sizes of the various dynamic sections. Allocate
5067 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
5071 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
5074 /* It's OK to base decisions on the section name, because none
5075 of the dynobj section names depend upon the input files. */
5076 name
= bfd_section_name (s
);
5078 if (streq (name
, ".plt"))
5080 /* Remember whether there is a PLT. */
5083 else if (CONST_STRNEQ (name
, ".rela"))
5089 /* Remember whether there are any reloc sections other
5091 if (! streq (name
, ".rela.plt"))
5093 const char * outname
;
5097 /* If this relocation section applies to a read only
5098 section, then we probably need a DT_TEXTREL
5099 entry. The entries in the .rela.plt section
5100 really apply to the .got section, which we
5101 created ourselves and so know is not readonly. */
5102 outname
= bfd_section_name (s
->output_section
);
5103 target
= bfd_get_section_by_name (output_bfd
, outname
+ 5);
5105 && (target
->flags
& SEC_READONLY
) != 0
5106 && (target
->flags
& SEC_ALLOC
) != 0)
5110 /* We use the reloc_count field as a counter if we need
5111 to copy relocs into the output file. */
5115 else if (! CONST_STRNEQ (name
, ".got")
5116 && ! streq (name
, ".dynbss"))
5117 /* It's not one of our sections, so don't allocate space. */
5122 /* If we don't need this section, strip it from the
5123 output file. This is mostly to handle .rela.bss and
5124 .rela.plt. We must create both sections in
5125 create_dynamic_sections, because they must be created
5126 before the linker maps input sections to output
5127 sections. The linker does that before
5128 adjust_dynamic_symbol is called, and it is that
5129 function which decides whether anything needs to go
5130 into these sections. */
5131 s
->flags
|= SEC_EXCLUDE
;
5135 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
5138 /* Allocate memory for the section contents. We use bfd_zalloc
5139 here in case unused entries are not reclaimed before the
5140 section's contents are written out. This should not happen,
5141 but this way if it does, we get a R_MN10300_NONE reloc
5142 instead of garbage. */
5143 s
->contents
= bfd_zalloc (dynobj
, s
->size
);
5144 if (s
->contents
== NULL
)
5148 if (elf_hash_table (info
)->dynamic_sections_created
)
5150 /* Add some entries to the .dynamic section. We fill in the
5151 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
5152 but we must add the entries now so that we get the correct
5153 size for the .dynamic section. The DT_DEBUG entry is filled
5154 in by the dynamic linker and used by the debugger. */
5155 if (! bfd_link_pic (info
))
5157 if (!_bfd_elf_add_dynamic_entry (info
, DT_DEBUG
, 0))
5163 if (!_bfd_elf_add_dynamic_entry (info
, DT_PLTGOT
, 0)
5164 || !_bfd_elf_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
5165 || !_bfd_elf_add_dynamic_entry (info
, DT_PLTREL
, DT_RELA
)
5166 || !_bfd_elf_add_dynamic_entry (info
, DT_JMPREL
, 0))
5172 if (!_bfd_elf_add_dynamic_entry (info
, DT_RELA
, 0)
5173 || !_bfd_elf_add_dynamic_entry (info
, DT_RELASZ
, 0)
5174 || !_bfd_elf_add_dynamic_entry (info
, DT_RELAENT
,
5175 sizeof (Elf32_External_Rela
)))
5181 if (!_bfd_elf_add_dynamic_entry (info
, DT_TEXTREL
, 0))
5189 /* Finish up dynamic symbol handling. We set the contents of various
5190 dynamic sections here. */
5193 _bfd_mn10300_elf_finish_dynamic_symbol (bfd
* output_bfd
,
5194 struct bfd_link_info
* info
,
5195 struct elf_link_hash_entry
* h
,
5196 Elf_Internal_Sym
* sym
)
5198 struct elf32_mn10300_link_hash_table
*htab
= elf32_mn10300_hash_table (info
);
5201 dynobj
= htab
->root
.dynobj
;
5203 if (h
->plt
.offset
!= (bfd_vma
) -1)
5210 Elf_Internal_Rela rel
;
5212 /* This symbol has an entry in the procedure linkage table. Set
5215 BFD_ASSERT (h
->dynindx
!= -1);
5217 splt
= htab
->root
.splt
;
5218 sgot
= htab
->root
.sgotplt
;
5219 srel
= htab
->root
.srelplt
;
5220 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
5222 /* Get the index in the procedure linkage table which
5223 corresponds to this symbol. This is the index of this symbol
5224 in all the symbols for which we are making plt entries. The
5225 first entry in the procedure linkage table is reserved. */
5226 plt_index
= ((h
->plt
.offset
- elf_mn10300_sizeof_plt0 (info
))
5227 / elf_mn10300_sizeof_plt (info
));
5229 /* Get the offset into the .got table of the entry that
5230 corresponds to this function. Each .got entry is 4 bytes.
5231 The first three are reserved. */
5232 got_offset
= (plt_index
+ 3) * 4;
5234 /* Fill in the entry in the procedure linkage table. */
5235 if (! bfd_link_pic (info
))
5237 memcpy (splt
->contents
+ h
->plt
.offset
, elf_mn10300_plt_entry
,
5238 elf_mn10300_sizeof_plt (info
));
5239 bfd_put_32 (output_bfd
,
5240 (sgot
->output_section
->vma
5241 + sgot
->output_offset
5243 (splt
->contents
+ h
->plt
.offset
5244 + elf_mn10300_plt_symbol_offset (info
)));
5246 bfd_put_32 (output_bfd
,
5247 (1 - h
->plt
.offset
- elf_mn10300_plt_plt0_offset (info
)),
5248 (splt
->contents
+ h
->plt
.offset
5249 + elf_mn10300_plt_plt0_offset (info
)));
5253 memcpy (splt
->contents
+ h
->plt
.offset
, elf_mn10300_pic_plt_entry
,
5254 elf_mn10300_sizeof_plt (info
));
5256 bfd_put_32 (output_bfd
, got_offset
,
5257 (splt
->contents
+ h
->plt
.offset
5258 + elf_mn10300_plt_symbol_offset (info
)));
5261 bfd_put_32 (output_bfd
, plt_index
* sizeof (Elf32_External_Rela
),
5262 (splt
->contents
+ h
->plt
.offset
5263 + elf_mn10300_plt_reloc_offset (info
)));
5265 /* Fill in the entry in the global offset table. */
5266 bfd_put_32 (output_bfd
,
5267 (splt
->output_section
->vma
5268 + splt
->output_offset
5270 + elf_mn10300_plt_temp_offset (info
)),
5271 sgot
->contents
+ got_offset
);
5273 /* Fill in the entry in the .rela.plt section. */
5274 rel
.r_offset
= (sgot
->output_section
->vma
5275 + sgot
->output_offset
5277 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_JMP_SLOT
);
5279 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
5280 (bfd_byte
*) ((Elf32_External_Rela
*) srel
->contents
5283 if (!h
->def_regular
)
5284 /* Mark the symbol as undefined, rather than as defined in
5285 the .plt section. Leave the value alone. */
5286 sym
->st_shndx
= SHN_UNDEF
;
5289 if (h
->got
.offset
!= (bfd_vma
) -1)
5293 Elf_Internal_Rela rel
;
5295 /* This symbol has an entry in the global offset table. Set it up. */
5296 sgot
= htab
->root
.sgot
;
5297 srel
= htab
->root
.srelgot
;
5298 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
5300 rel
.r_offset
= (sgot
->output_section
->vma
5301 + sgot
->output_offset
5302 + (h
->got
.offset
& ~1));
5304 switch (elf_mn10300_hash_entry (h
)->tls_type
)
5307 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
5308 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
+ 4);
5309 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_TLS_DTPMOD
);
5311 bfd_elf32_swap_reloca_out (output_bfd
, & rel
,
5312 (bfd_byte
*) ((Elf32_External_Rela
*) srel
->contents
5313 + srel
->reloc_count
));
5314 ++ srel
->reloc_count
;
5315 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_TLS_DTPOFF
);
5321 /* We originally stored the addend in the GOT, but at this
5322 point, we want to move it to the reloc instead as that's
5323 where the dynamic linker wants it. */
5324 rel
.r_addend
= bfd_get_32 (output_bfd
, sgot
->contents
+ h
->got
.offset
);
5325 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
5326 if (h
->dynindx
== -1)
5327 rel
.r_info
= ELF32_R_INFO (0, R_MN10300_TLS_TPOFF
);
5329 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_TLS_TPOFF
);
5333 /* If this is a -Bsymbolic link, and the symbol is defined
5334 locally, we just want to emit a RELATIVE reloc. Likewise if
5335 the symbol was forced to be local because of a version file.
5336 The entry in the global offset table will already have been
5337 initialized in the relocate_section function. */
5338 if (bfd_link_pic (info
)
5339 && (info
->symbolic
|| h
->dynindx
== -1)
5342 rel
.r_info
= ELF32_R_INFO (0, R_MN10300_RELATIVE
);
5343 rel
.r_addend
= (h
->root
.u
.def
.value
5344 + h
->root
.u
.def
.section
->output_section
->vma
5345 + h
->root
.u
.def
.section
->output_offset
);
5349 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
5350 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_GLOB_DAT
);
5355 if (ELF32_R_TYPE (rel
.r_info
) != R_MN10300_NONE
)
5357 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
5358 (bfd_byte
*) ((Elf32_External_Rela
*) srel
->contents
5359 + srel
->reloc_count
));
5360 ++ srel
->reloc_count
;
5367 Elf_Internal_Rela rel
;
5369 /* This symbol needs a copy reloc. Set it up. */
5370 BFD_ASSERT (h
->dynindx
!= -1
5371 && (h
->root
.type
== bfd_link_hash_defined
5372 || h
->root
.type
== bfd_link_hash_defweak
));
5374 s
= bfd_get_linker_section (dynobj
, ".rela.bss");
5375 BFD_ASSERT (s
!= NULL
);
5377 rel
.r_offset
= (h
->root
.u
.def
.value
5378 + h
->root
.u
.def
.section
->output_section
->vma
5379 + h
->root
.u
.def
.section
->output_offset
);
5380 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_COPY
);
5382 bfd_elf32_swap_reloca_out (output_bfd
, & rel
,
5383 (bfd_byte
*) ((Elf32_External_Rela
*) s
->contents
5388 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5389 if (h
== elf_hash_table (info
)->hdynamic
5390 || h
== elf_hash_table (info
)->hgot
)
5391 sym
->st_shndx
= SHN_ABS
;
5396 /* Finish up the dynamic sections. */
5399 _bfd_mn10300_elf_finish_dynamic_sections (bfd
* output_bfd
,
5400 struct bfd_link_info
* info
)
5405 struct elf32_mn10300_link_hash_table
*htab
= elf32_mn10300_hash_table (info
);
5407 dynobj
= htab
->root
.dynobj
;
5408 sgot
= htab
->root
.sgotplt
;
5409 BFD_ASSERT (sgot
!= NULL
);
5410 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
5412 if (elf_hash_table (info
)->dynamic_sections_created
)
5415 Elf32_External_Dyn
* dyncon
;
5416 Elf32_External_Dyn
* dynconend
;
5418 BFD_ASSERT (sdyn
!= NULL
);
5420 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
5421 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
5423 for (; dyncon
< dynconend
; dyncon
++)
5425 Elf_Internal_Dyn dyn
;
5428 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5436 s
= htab
->root
.sgot
;
5440 s
= htab
->root
.srelplt
;
5442 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
5443 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5447 s
= htab
->root
.srelplt
;
5448 dyn
.d_un
.d_val
= s
->size
;
5449 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5454 /* Fill in the first entry in the procedure linkage table. */
5455 splt
= htab
->root
.splt
;
5456 if (splt
&& splt
->size
> 0)
5458 if (bfd_link_pic (info
))
5460 memcpy (splt
->contents
, elf_mn10300_pic_plt_entry
,
5461 elf_mn10300_sizeof_plt (info
));
5465 memcpy (splt
->contents
, elf_mn10300_plt0_entry
, PLT0_ENTRY_SIZE
);
5466 bfd_put_32 (output_bfd
,
5467 sgot
->output_section
->vma
+ sgot
->output_offset
+ 4,
5468 splt
->contents
+ elf_mn10300_plt0_gotid_offset (info
));
5469 bfd_put_32 (output_bfd
,
5470 sgot
->output_section
->vma
+ sgot
->output_offset
+ 8,
5471 splt
->contents
+ elf_mn10300_plt0_linker_offset (info
));
5474 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5475 really seem like the right value. */
5476 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
5478 /* UnixWare sets the entsize of .plt to 4, but this is incorrect
5479 as it means that the size of the PLT0 section (15 bytes) is not
5480 a multiple of the sh_entsize. Some ELF tools flag this as an
5481 error. We could pad PLT0 to 16 bytes, but that would introduce
5482 compatibilty issues with previous toolchains, so instead we
5483 just set the entry size to 1. */
5484 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 1;
5488 /* Fill in the first three entries in the global offset table. */
5492 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
5494 bfd_put_32 (output_bfd
,
5495 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
5497 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
5498 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
5501 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
5506 /* Classify relocation types, such that combreloc can sort them
5509 static enum elf_reloc_type_class
5510 _bfd_mn10300_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5511 const asection
*rel_sec ATTRIBUTE_UNUSED
,
5512 const Elf_Internal_Rela
*rela
)
5514 switch ((int) ELF32_R_TYPE (rela
->r_info
))
5516 case R_MN10300_RELATIVE
: return reloc_class_relative
;
5517 case R_MN10300_JMP_SLOT
: return reloc_class_plt
;
5518 case R_MN10300_COPY
: return reloc_class_copy
;
5519 default: return reloc_class_normal
;
5523 /* Allocate space for an MN10300 extension to the bfd elf data structure. */
5526 mn10300_elf_mkobject (bfd
*abfd
)
5528 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_mn10300_obj_tdata
),
5532 #define bfd_elf32_mkobject mn10300_elf_mkobject
5535 #define TARGET_LITTLE_SYM mn10300_elf32_vec
5536 #define TARGET_LITTLE_NAME "elf32-mn10300"
5537 #define ELF_ARCH bfd_arch_mn10300
5538 #define ELF_TARGET_ID MN10300_ELF_DATA
5539 #define ELF_MACHINE_CODE EM_MN10300
5540 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
5541 #define ELF_MAXPAGESIZE 0x1000
5544 #define elf_info_to_howto mn10300_info_to_howto
5545 #define elf_info_to_howto_rel NULL
5546 #define elf_backend_can_gc_sections 1
5547 #define elf_backend_rela_normal 1
5548 #define elf_backend_check_relocs mn10300_elf_check_relocs
5549 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
5550 #define elf_backend_relocate_section mn10300_elf_relocate_section
5551 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
5552 #define bfd_elf32_bfd_get_relocated_section_contents \
5553 mn10300_elf_get_relocated_section_contents
5554 #define bfd_elf32_bfd_link_hash_table_create \
5555 elf32_mn10300_link_hash_table_create
5557 #ifndef elf_symbol_leading_char
5558 #define elf_symbol_leading_char '_'
5561 /* So we can set bits in e_flags. */
5562 #define elf_backend_final_write_processing \
5563 _bfd_mn10300_elf_final_write_processing
5564 #define elf_backend_object_p _bfd_mn10300_elf_object_p
5566 #define bfd_elf32_bfd_merge_private_bfd_data \
5567 _bfd_mn10300_elf_merge_private_bfd_data
5569 #define elf_backend_can_gc_sections 1
5570 #define elf_backend_create_dynamic_sections \
5571 _bfd_mn10300_elf_create_dynamic_sections
5572 #define elf_backend_adjust_dynamic_symbol \
5573 _bfd_mn10300_elf_adjust_dynamic_symbol
5574 #define elf_backend_size_dynamic_sections \
5575 _bfd_mn10300_elf_size_dynamic_sections
5576 #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
5577 #define elf_backend_finish_dynamic_symbol \
5578 _bfd_mn10300_elf_finish_dynamic_symbol
5579 #define elf_backend_finish_dynamic_sections \
5580 _bfd_mn10300_elf_finish_dynamic_sections
5581 #define elf_backend_copy_indirect_symbol \
5582 _bfd_mn10300_copy_indirect_symbol
5583 #define elf_backend_reloc_type_class \
5584 _bfd_mn10300_elf_reloc_type_class
5586 #define elf_backend_want_got_plt 1
5587 #define elf_backend_plt_readonly 1
5588 #define elf_backend_want_plt_sym 0
5589 #define elf_backend_got_header_size 12
5590 #define elf_backend_dtrel_excludes_plt 1
5592 #include "elf32-target.h"