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