msp430.em 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. # This shell script emits a C file. -*- C -*-
  2. # It does some substitutions.
  3. fragment <<EOF
  4. /* This file is is generated by a shell script. DO NOT EDIT! */
  5. /* Emulate the original gld for the given ${EMULATION_NAME}
  6. Copyright (C) 2014-2015 Free Software Foundation, Inc.
  7. Written by Steve Chamberlain steve@cygnus.com
  8. Extended for the MSP430 by Nick Clifton nickc@redhat.com
  9. This file is part of the GNU Binutils.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 3 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  21. MA 02110-1301, USA. */
  22. #define TARGET_IS_${EMULATION_NAME}
  23. #include "sysdep.h"
  24. #include "bfd.h"
  25. #include "bfdlink.h"
  26. #include "ld.h"
  27. #include "ldmain.h"
  28. #include "ldmisc.h"
  29. #include "ldexp.h"
  30. #include "ldlang.h"
  31. #include "ldfile.h"
  32. #include "ldemul.h"
  33. #include "libiberty.h"
  34. EOF
  35. # Import any needed special functions and/or overrides.
  36. #
  37. if test -n "$EXTRA_EM_FILE" ; then
  38. source_em ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
  39. fi
  40. if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
  41. fragment <<EOF
  42. static void
  43. gld${EMULATION_NAME}_before_parse (void)
  44. {
  45. #ifndef TARGET_ /* I.e., if not generic. */
  46. ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown);
  47. #endif /* not TARGET_ */
  48. /* The MSP430 port *needs* linker relaxtion in order to cope with large
  49. functions where conditional branches do not fit into a +/- 1024 byte range. */
  50. if (!bfd_link_relocatable (&link_info))
  51. TARGET_ENABLE_RELAXATION;
  52. }
  53. EOF
  54. fi
  55. if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
  56. fragment <<EOF
  57. static char *
  58. gld${EMULATION_NAME}_get_script (int *isfile)
  59. EOF
  60. if test x"$COMPILE_IN" = xyes
  61. then
  62. # Scripts compiled in.
  63. # sed commands to quote an ld script as a C string.
  64. sc="-f stringify.sed"
  65. fragment <<EOF
  66. {
  67. *isfile = 0;
  68. if (bfd_link_relocatable (&link_info) && config.build_constructors)
  69. return
  70. EOF
  71. sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
  72. echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
  73. sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
  74. echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
  75. sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
  76. echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
  77. sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
  78. echo ' ; else return' >> e${EMULATION_NAME}.c
  79. sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
  80. echo '; }' >> e${EMULATION_NAME}.c
  81. else
  82. # Scripts read from the filesystem.
  83. fragment <<EOF
  84. {
  85. *isfile = 1;
  86. if (bfd_link_relocatable (&link_info) && config.build_constructors)
  87. return "ldscripts/${EMULATION_NAME}.xu";
  88. else if (bfd_link_relocatable (&link_info))
  89. return "ldscripts/${EMULATION_NAME}.xr";
  90. else if (!config.text_read_only)
  91. return "ldscripts/${EMULATION_NAME}.xbn";
  92. else if (!config.magic_demand_paged)
  93. return "ldscripts/${EMULATION_NAME}.xn";
  94. else
  95. return "ldscripts/${EMULATION_NAME}.x";
  96. }
  97. EOF
  98. fi
  99. fi
  100. if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
  101. fragment <<EOF
  102. /* Helper function for place_orphan that computes the size
  103. of sections already mapped to the given statement. */
  104. static bfd_size_type
  105. scan_children (lang_statement_union_type * l)
  106. {
  107. bfd_size_type amount = 0;
  108. while (l != NULL)
  109. {
  110. switch (l->header.type)
  111. {
  112. case lang_input_section_enum:
  113. if (l->input_section.section->flags & SEC_ALLOC)
  114. amount += l->input_section.section->size;
  115. break;
  116. case lang_constructors_statement_enum:
  117. case lang_assignment_statement_enum:
  118. break;
  119. case lang_wild_statement_enum:
  120. amount += scan_children (l->wild_statement.children.head);
  121. break;
  122. default:
  123. fprintf (stderr, "msp430 orphan placer: unhandled lang type %d\n", l->header.type);
  124. break;
  125. }
  126. l = l->header.next;
  127. }
  128. return amount;
  129. }
  130. /* Place an orphan section. We use this to put .either sections
  131. into either their lower or their upper equivalents. */
  132. static lang_output_section_statement_type *
  133. gld${EMULATION_NAME}_place_orphan (asection * s,
  134. const char * secname,
  135. int constraint)
  136. {
  137. char * lower_name;
  138. char * upper_name;
  139. char * name;
  140. lang_output_section_statement_type * lower;
  141. lang_output_section_statement_type * upper;
  142. lang_output_section_statement_type * os;
  143. if ((s->flags & SEC_ALLOC) == 0)
  144. return NULL;
  145. if (bfd_link_relocatable (&link_info))
  146. return NULL;
  147. /* If constraints are involved let the linker handle the placement normally. */
  148. if (constraint != 0)
  149. return NULL;
  150. /* We only need special handling for .either sections. */
  151. if (strncmp (secname, ".either.", 8) != 0)
  152. return NULL;
  153. /* Skip the .either prefix. */
  154. secname += 7;
  155. /* Compute the names of the corresponding upper and lower
  156. sections. If the input section name contains another period,
  157. only use the part of the name before the second dot. */
  158. if (strchr (secname + 1, '.') != NULL)
  159. {
  160. name = ACONCAT ((secname, NULL));
  161. * strchr (name + 1, '.') = 0;
  162. }
  163. else
  164. name = (char *) secname;
  165. lower_name = ACONCAT ((".lower", name, NULL));
  166. upper_name = ACONCAT ((".upper", name, NULL));
  167. /* Find the corresponding lower and upper sections. */
  168. lower = lang_output_section_find (lower_name);
  169. upper = lang_output_section_find (upper_name);
  170. /* If the upper section does not exist, try again without the suffix. */
  171. if (upper == NULL)
  172. upper = lang_output_section_find (name);
  173. if (lower == NULL)
  174. {
  175. os = upper;
  176. if (upper == NULL)
  177. {
  178. einfo ("%P: error: no section named %s or %s in linker script\n", lower_name, upper_name);
  179. return NULL;
  180. }
  181. }
  182. else if (upper == NULL)
  183. os = lower;
  184. else if (lower->region == NULL)
  185. os = lower;
  186. /* If the section is too big for the region containing
  187. the lower section then do not even try to use it. */
  188. else if (lower->region->length < s->size)
  189. os = upper;
  190. else
  191. {
  192. bfd_size_type amount = 0;
  193. struct lang_output_section_statement_struct * p;
  194. amount += scan_children (lower->children.head);
  195. /* Also check forwards for other statements assigned to the same region. */
  196. for (p = lower->next; p != NULL; p = p->next)
  197. if (p->region == lower->region)
  198. amount += scan_children (p->children.head);
  199. /* Scan backwards as well. */
  200. for (p = lower->prev; p != NULL; p = p->prev)
  201. if (p->region == lower->region)
  202. amount += scan_children (p->children.head);
  203. if (amount + s->size >= lower->region->length)
  204. os = upper;
  205. else
  206. os = lower;
  207. }
  208. lang_add_section (& os->children, s, NULL, os);
  209. return os;
  210. }
  211. EOF
  212. fi
  213. fragment <<EOF
  214. struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
  215. {
  216. ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
  217. ${LDEMUL_SYSLIB-syslib_default},
  218. ${LDEMUL_HLL-hll_default},
  219. ${LDEMUL_AFTER_PARSE-after_parse_default},
  220. ${LDEMUL_AFTER_OPEN-after_open_default},
  221. ${LDEMUL_AFTER_ALLOCATION-after_allocation_default},
  222. ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
  223. ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
  224. ${LDEMUL_BEFORE_ALLOCATION-before_allocation_default},
  225. ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
  226. "${EMULATION_NAME}",
  227. "${OUTPUT_FORMAT}",
  228. ${LDEMUL_FINISH-finish_default},
  229. ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
  230. ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-NULL},
  231. ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
  232. ${LDEMUL_SET_SYMBOLS-NULL},
  233. ${LDEMUL_PARSE_ARGS-NULL},
  234. ${LDEMUL_ADD_OPTIONS-NULL},
  235. ${LDEMUL_HANDLE_OPTION-NULL},
  236. ${LDEMUL_UNRECOGNIZED_FILE-NULL},
  237. ${LDEMUL_LIST_OPTIONS-NULL},
  238. ${LDEMUL_RECOGNIZED_FILE-NULL},
  239. ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
  240. ${LDEMUL_NEW_VERS_PATTERN-NULL},
  241. ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL}
  242. };
  243. EOF
  244. #
  245. # Local Variables:
  246. # mode: c
  247. # End: