Enable relro by default for AArch64, ARM and SCORE.
[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   char * buf = NULL;
175   lang_output_section_statement_type * lower;
176   lang_output_section_statement_type * upper;
177   lang_output_section_statement_type * os;
178
179   if ((s->flags & SEC_ALLOC) == 0)
180     return NULL;
181
182   if (bfd_link_relocatable (&link_info))
183     return NULL;
184
185   /* If constraints are involved let the linker handle the placement normally.  */
186   if (constraint != 0)
187     return NULL;
188
189   /* We only need special handling for .either sections.  */
190   if (strncmp (secname, ".either.", 8) != 0)
191     return NULL;
192
193   /* Skip the .either prefix.  */
194   secname += 7;
195
196   /* Compute the names of the corresponding upper and lower
197      sections.  If the input section name contains another period,
198      only use the part of the name before the second dot.  */
199   if (strchr (secname + 1, '.') != NULL)
200     {
201       buf = name = xstrdup (secname);
202
203       * strchr (name + 1, '.') = 0;
204     }
205   else
206     name = (char *) secname;
207   
208   lower_name = concat (".lower", name, NULL);
209   upper_name = concat (".upper", name, NULL);
210
211   /* Find the corresponding lower and upper sections.  */
212   lower = lang_output_section_find (lower_name);
213   upper = lang_output_section_find (upper_name);
214   /* If the upper section does not exist, try again without the suffix.  */
215   if (upper == NULL)
216     upper = lang_output_section_find (name);
217
218   if (lower == NULL)
219     {
220       os = upper;
221       if (upper == NULL)
222         {
223           einfo ("%P: error: no section named %s or %s in linker script\n", lower_name, upper_name);
224           goto end;
225         }
226     }
227   else if (upper == NULL)
228     os = lower;
229   else if (lower->region == NULL)
230     os = lower;
231   /* If the section is too big for the region containing
232      the lower section then do not even try to use it.  */
233   else if (lower->region->length < s->size)
234     os = upper;
235   else
236     {
237       bfd_size_type amount = 0;
238       struct lang_output_section_statement_struct * p;
239
240       amount += scan_children (lower->children.head);
241
242       /* Also check forwards for other statements assigned to the same region.  */
243       for (p = lower->next; p != NULL; p = p->next)
244         if (p->region == lower->region)
245           amount += scan_children (p->children.head);
246
247       /* Scan backwards as well.  */      
248       for (p = lower->prev; p != NULL; p = p->prev)
249         if (p->region == lower->region)
250           amount += scan_children (p->children.head);
251
252       if (amount + s->size >= lower->region->length)
253         os = upper;
254       else
255         os = lower;
256     }
257
258   lang_add_section (& os->children, s, NULL, os);
259  end:
260   free (upper_name);
261   free (lower_name);
262   if (buf)
263     free (buf);
264   return os;
265 }
266 EOF
267 fi
268
269 fragment <<EOF
270
271 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
272 {
273   ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
274   ${LDEMUL_SYSLIB-syslib_default},
275   ${LDEMUL_HLL-hll_default},
276   ${LDEMUL_AFTER_PARSE-after_parse_default},
277   ${LDEMUL_AFTER_OPEN-after_open_default},
278   ${LDEMUL_AFTER_ALLOCATION-after_allocation_default},
279   ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
280   ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
281   ${LDEMUL_BEFORE_ALLOCATION-before_allocation_default},
282   ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
283   "${EMULATION_NAME}",
284   "${OUTPUT_FORMAT}",
285   ${LDEMUL_FINISH-finish_default},
286   ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
287   ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-NULL},
288   ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
289   ${LDEMUL_SET_SYMBOLS-NULL},
290   ${LDEMUL_PARSE_ARGS-NULL},
291   ${LDEMUL_ADD_OPTIONS-NULL},
292   ${LDEMUL_HANDLE_OPTION-NULL},
293   ${LDEMUL_UNRECOGNIZED_FILE-NULL},
294   ${LDEMUL_LIST_OPTIONS-NULL},
295   ${LDEMUL_RECOGNIZED_FILE-NULL},
296   ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
297   ${LDEMUL_NEW_VERS_PATTERN-NULL},
298   ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL}
299 };
300 EOF
301\f
302 # Local Variables:
303 # mode: c
304 # End: