cr16elf.em 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # This shell script emits a C file. -*- C -*-
  2. # Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. # Contributed by M R Swami Reddy <MR.Swami.Reddy@nsc.com>
  4. #
  5. # This file is part of the GNU Binutils.
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  20. # MA 02110-1301, USA.
  21. #
  22. # This file is sourced from elf32.em, and defines extra cr16-elf
  23. # specific routines.
  24. #
  25. fragment <<EOF
  26. #include "ldctor.h"
  27. static void check_sections (bfd *, asection *, void *);
  28. /* This function is run after all the input files have been opened. */
  29. static void
  30. cr16_elf_after_open (void)
  31. {
  32. /* Call the standard elf routine. */
  33. gld${EMULATION_NAME}_after_open ();
  34. if (command_line.embedded_relocs
  35. && !bfd_link_relocatable (&link_info))
  36. {
  37. bfd *abfd;
  38. /* In the embedded relocs mode we create a .emreloc section for each
  39. input file with a nonzero .data section. The BFD backend will fill in
  40. these sections with magic numbers which can be used to relocate the
  41. data section at run time. */
  42. for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
  43. {
  44. asection *datasec;
  45. /* As first-order business, make sure that each input BFD is either
  46. COFF or ELF. We need to call a special BFD backend function to
  47. generate the embedded relocs, and we have such functions only for
  48. COFF and ELF. */
  49. if (bfd_get_flavour (abfd) != bfd_target_coff_flavour
  50. && bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  51. einfo ("%F%B: all input objects must be COFF or ELF for --embedded-relocs\n");
  52. datasec = bfd_get_section_by_name (abfd, ".data.rel");
  53. /* Note that we assume that the reloc_count field has already
  54. been set up. We could call bfd_get_reloc_upper_bound, but
  55. that returns the size of a memory buffer rather than a reloc
  56. count. We do not want to call bfd_canonicalize_reloc,
  57. because although it would always work it would force us to
  58. read in the relocs into BFD canonical form, which would waste
  59. a significant amount of time and memory. */
  60. if (datasec != NULL && datasec->reloc_count > 0)
  61. {
  62. asection *relsec;
  63. relsec = bfd_make_section (abfd, ".emreloc");
  64. if (relsec == NULL
  65. || ! bfd_set_section_flags (abfd, relsec,
  66. (SEC_ALLOC
  67. | SEC_LOAD
  68. | SEC_HAS_CONTENTS
  69. | SEC_IN_MEMORY))
  70. || ! bfd_set_section_alignment (abfd, relsec, 2)
  71. || ! bfd_set_section_size (abfd, relsec,
  72. datasec->reloc_count * 8))
  73. einfo ("%F%B: can not create .emreloc section: %E\n");
  74. }
  75. /* Double check that all other data sections are empty, as is
  76. required for embedded PIC code. */
  77. bfd_map_over_sections (abfd, check_sections, datasec);
  78. }
  79. }
  80. }
  81. /* Check that of the data sections, only the .data section has
  82. relocs. This is called via bfd_map_over_sections. */
  83. static void
  84. check_sections (bfd *abfd, asection *sec, void *datasec)
  85. {
  86. if ((strncmp (bfd_get_section_name (abfd, sec), ".data.rel", 9) == 0)
  87. && sec != datasec
  88. && sec->reloc_count == 0 )
  89. einfo ("%B%X: section %s has relocs; can not use --embedded-relocs\n",
  90. abfd, bfd_get_section_name (abfd, sec));
  91. }
  92. static void
  93. cr16elf_after_parse (void)
  94. {
  95. /* Always behave as if called with --sort-common command line
  96. option.
  97. This is to emulate the CRTools' method of keeping variables
  98. of different alignment in separate sections. */
  99. config.sort_common = TRUE;
  100. /* Don't create a demand-paged executable, since this feature isn't
  101. meaninful in CR16 embedded systems. Moreover, when magic_demand_paged
  102. is true the link sometimes fails. */
  103. config.magic_demand_paged = FALSE;
  104. gld${EMULATION_NAME}_after_parse ();
  105. }
  106. /* This is called after the sections have been attached to output
  107. sections, but before any sizes or addresses have been set. */
  108. static void
  109. cr16elf_before_allocation (void)
  110. {
  111. /* Call the default first. */
  112. gld${EMULATION_NAME}_before_allocation ();
  113. if (command_line.embedded_relocs
  114. && (!bfd_link_relocatable (&link_info)))
  115. {
  116. bfd *abfd;
  117. /* If we are generating embedded relocs, call a special BFD backend
  118. routine to do the work. */
  119. for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
  120. {
  121. asection *datasec, *relsec;
  122. char *errmsg;
  123. datasec = bfd_get_section_by_name (abfd, ".data.rel");
  124. if (datasec == NULL || datasec->reloc_count == 0)
  125. continue;
  126. relsec = bfd_get_section_by_name (abfd, ".emreloc");
  127. ASSERT (relsec != NULL);
  128. if (! bfd_cr16_elf32_create_embedded_relocs (abfd, &link_info,
  129. datasec, relsec,
  130. &errmsg))
  131. {
  132. if (errmsg == NULL)
  133. einfo ("%B%X: can not create runtime reloc information: %E\n",
  134. abfd);
  135. else
  136. einfo ("%X%B: can not create runtime reloc information: %s\n",
  137. abfd, errmsg);
  138. }
  139. }
  140. }
  141. /* Enable relaxation by default if the "--no-relax" option was not
  142. specified. This is done here instead of in the before_parse hook
  143. because there is a check in main() to prohibit use of --relax and
  144. -r together. */
  145. if (RELAXATION_DISABLED_BY_DEFAULT)
  146. ENABLE_RELAXATION;
  147. }
  148. EOF
  149. # Put these extra cr16-elf routines in ld_${EMULATION_NAME}_emulation
  150. #
  151. LDEMUL_AFTER_OPEN=cr16_elf_after_open
  152. LDEMUL_AFTER_PARSE=cr16elf_after_parse
  153. LDEMUL_BEFORE_ALLOCATION=cr16elf_before_allocation