]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/emultempl/m68hc1xelf.em
Correct spelling of "relocatable".
[thirdparty/binutils-gdb.git] / ld / emultempl / m68hc1xelf.em
1 # This shell script emits a C file. -*- C -*-
2 # Copyright 1991, 1993, 1994, 1997, 1999, 2000, 2001, 2002, 2003
3 # Free Software Foundation, Inc.
4 #
5 # This file is part of GLD, the Gnu Linker.
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #
21
22 # This file is sourced from elf32.em, and defines extra m68hc12-elf
23 # and m68hc11-elf specific routines. It is used to generate the
24 # HC11/HC12 trampolines to call a far function by using a normal 'jsr/bsr'.
25 #
26 # - The HC11/HC12 relocations are checked to see if there is a
27 # R_M68HC11_16 relocation to a symbol marked with STO_M68HC12_FAR.
28 # This relocation cannot be made on the symbol but must be made on
29 # its trampoline
30 # The trampolines to generate are collected during this pass
31 # (See elf32_m68hc11_size_stubs)
32 #
33 # - The trampolines are generated in a ".tramp" section. The generation
34 # takes care of HC11 and HC12 specificities.
35 # (See elf32_m68hc11_build_stubs)
36 #
37 # - During relocation the R_M68HC11_16 relocation to the far symbols
38 # are redirected to the trampoline that was generated.
39 #
40 # Copied from hppaelf and adapted for M68HC11/M68HC12 specific needs.
41 #
42 cat >>e${EMULATION_NAME}.c <<EOF
43
44 #include "ldctor.h"
45 #include "elf32-m68hc1x.h"
46
47 static void m68hc11elf_create_output_section_statements PARAMS ((void));
48 static asection *m68hc11elf_add_stub_section
49 PARAMS ((const char *, asection *));
50 static void gld${EMULATION_NAME}_finish PARAMS ((void));
51 static void m68hc11_elf_${EMULATION_NAME}_before_allocation PARAMS ((void));
52
53
54 /* Fake input file for stubs. */
55 static lang_input_statement_type *stub_file;
56
57 /* By default the HC11/HC12 trampolines to call a far function using
58 a normal 'bsr' and 'jsr' convention are generated during the link.
59 The --no-trampoline option prevents that. */
60 static int no_trampoline = 0;
61
62 /* Name of memory bank window in the MEMORY description.
63 This is set by --bank-window option. */
64 static const char* bank_window_name = 0;
65
66 static void
67 m68hc11_elf_${EMULATION_NAME}_before_allocation ()
68 {
69 lang_memory_region_type* region;
70 int ret;
71
72 gld${EMULATION_NAME}_before_allocation ();
73
74 /* If generating a relocatable output file, then we don't
75 have to generate the trampolines. */
76 if (link_info.relocatable)
77 return;
78
79 ret = elf32_m68hc11_setup_section_lists (output_bfd, &link_info);
80 if (ret != 0 && no_trampoline == 0)
81 {
82 if (ret < 0)
83 {
84 einfo ("%X%P: can not size stub section: %E\n");
85 return;
86 }
87
88 /* Call into the BFD backend to do the real work. */
89 if (!elf32_m68hc11_size_stubs (output_bfd,
90 stub_file->the_bfd,
91 &link_info,
92 &m68hc11elf_add_stub_section))
93 {
94 einfo ("%X%P: can not size stub section: %E\n");
95 return;
96 }
97 }
98
99 if (bank_window_name == 0)
100 return;
101
102 /* The 'bank_window_name' memory region is a special region that describes
103 the memory bank window to access to paged memory. For 68HC12
104 this is fixed and should be:
105
106 window (rx) : ORIGIN = 0x8000, LENGTH = 16K
107
108 But for 68HC11 this is board specific. The definition of such
109 memory region allows to control how this paged memory is accessed. */
110 region = lang_memory_region_lookup (bank_window_name);
111
112 /* Check the length to see if it was defined in the script. */
113 if (region->length != 0)
114 {
115 struct m68hc11_page_info *pinfo;
116 unsigned i;
117
118 /* Get default values */
119 m68hc11_elf_get_bank_parameters (&link_info);
120 pinfo = &m68hc11_elf_hash_table (&link_info)->pinfo;
121
122 /* And override them with the region definition. */
123 pinfo->bank_size = region->length;
124 pinfo->bank_shift = 0;
125 for (i = pinfo->bank_size; i != 0; i >>= 1)
126 pinfo->bank_shift++;
127 pinfo->bank_shift--;
128 pinfo->bank_size = 1L << pinfo->bank_shift;
129 pinfo->bank_mask = (1 << pinfo->bank_shift) - 1;
130 pinfo->bank_physical = region->origin;
131 pinfo->bank_physical_end = region->origin + pinfo->bank_size;
132
133 if (pinfo->bank_size != region->length)
134 {
135 einfo (_("warning: the size of the 'window' memory region "
136 "is not a power of 2\n"));
137 einfo (_("warning: its size %d is truncated to %d\n"),
138 region->length, pinfo->bank_size);
139 }
140 }
141 }
142
143 /* This is called before the input files are opened. We create a new
144 fake input file to hold the stub sections. */
145
146 static void
147 m68hc11elf_create_output_section_statements ()
148 {
149 stub_file = lang_add_input_file ("linker stubs",
150 lang_input_file_is_fake_enum,
151 NULL);
152 stub_file->the_bfd = bfd_create ("linker stubs", output_bfd);
153 if (stub_file->the_bfd == NULL
154 || !bfd_set_arch_mach (stub_file->the_bfd,
155 bfd_get_arch (output_bfd),
156 bfd_get_mach (output_bfd)))
157 {
158 einfo ("%X%P: can not create BFD %E\n");
159 return;
160 }
161
162 ldlang_add_file (stub_file);
163 }
164
165
166 struct hook_stub_info
167 {
168 lang_statement_list_type add;
169 asection *input_section;
170 };
171
172 /* Traverse the linker tree to find the spot where the stub goes. */
173
174 static bfd_boolean hook_in_stub
175 PARAMS ((struct hook_stub_info *, lang_statement_union_type **));
176
177 static bfd_boolean
178 hook_in_stub (info, lp)
179 struct hook_stub_info *info;
180 lang_statement_union_type **lp;
181 {
182 lang_statement_union_type *l;
183 bfd_boolean ret;
184
185 for (; (l = *lp) != NULL; lp = &l->header.next)
186 {
187 switch (l->header.type)
188 {
189 case lang_constructors_statement_enum:
190 ret = hook_in_stub (info, &constructor_list.head);
191 if (ret)
192 return ret;
193 break;
194
195 case lang_output_section_statement_enum:
196 ret = hook_in_stub (info,
197 &l->output_section_statement.children.head);
198 if (ret)
199 return ret;
200 break;
201
202 case lang_wild_statement_enum:
203 ret = hook_in_stub (info, &l->wild_statement.children.head);
204 if (ret)
205 return ret;
206 break;
207
208 case lang_group_statement_enum:
209 ret = hook_in_stub (info, &l->group_statement.children.head);
210 if (ret)
211 return ret;
212 break;
213
214 case lang_input_section_enum:
215 if (l->input_section.section == info->input_section
216 || strcmp (bfd_get_section_name (output_section,
217 l->input_section.section),
218 bfd_get_section_name (output_section,
219 info->input_section)) == 0)
220 {
221 /* We've found our section. Insert the stub immediately
222 before its associated input section. */
223 *lp = info->add.head;
224 *(info->add.tail) = l;
225 return TRUE;
226 }
227 break;
228
229 case lang_data_statement_enum:
230 case lang_reloc_statement_enum:
231 case lang_object_symbols_statement_enum:
232 case lang_output_statement_enum:
233 case lang_target_statement_enum:
234 case lang_input_statement_enum:
235 case lang_assignment_statement_enum:
236 case lang_padding_statement_enum:
237 case lang_address_statement_enum:
238 case lang_fill_statement_enum:
239 break;
240
241 default:
242 FAIL ();
243 break;
244 }
245 }
246 return FALSE;
247 }
248
249
250 /* Call-back for elf32_m68hc11_size_stubs. */
251
252 /* Create a new stub section, and arrange for it to be linked
253 immediately before INPUT_SECTION. */
254
255 static asection *
256 m68hc11elf_add_stub_section (stub_sec_name, tramp_section)
257 const char *stub_sec_name;
258 asection *tramp_section;
259 {
260 asection *stub_sec;
261 flagword flags;
262 asection *output_section;
263 const char *secname;
264 lang_output_section_statement_type *os;
265 struct hook_stub_info info;
266
267 stub_sec = bfd_make_section_anyway (stub_file->the_bfd, stub_sec_name);
268 if (stub_sec == NULL)
269 goto err_ret;
270
271 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
272 | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
273 if (!bfd_set_section_flags (stub_file->the_bfd, stub_sec, flags))
274 goto err_ret;
275
276 output_section = tramp_section->output_section;
277 secname = bfd_get_section_name (output_section->owner, output_section);
278 os = lang_output_section_find (secname);
279
280 /* Try to put the new section at the same place as an existing
281 .tramp section. Such .tramp section exists in most cases and
282 contains the trampoline code. This way we put the generated trampoline
283 at the correct place. */
284 info.input_section = tramp_section;
285 lang_list_init (&info.add);
286 lang_add_section (&info.add, stub_sec, os, stub_file);
287
288 if (info.add.head == NULL)
289 goto err_ret;
290
291 if (hook_in_stub (&info, &os->children.head))
292 return stub_sec;
293
294 err_ret:
295 einfo ("%X%P: can not make stub section: %E\n");
296 return NULL;
297 }
298
299 /* Final emulation specific call. For the 68HC12 we use this opportunity
300 to build linker stubs. */
301
302 static void
303 gld${EMULATION_NAME}_finish ()
304 {
305 /* Now build the linker stubs. */
306 if (stub_file->the_bfd->sections != NULL)
307 {
308 /* Call again the trampoline analyzer to initialize the trampoline
309 stubs with the correct symbol addresses. Since there could have
310 been relaxation, the symbol addresses that were found during
311 first call may no longer be correct. */
312 if (!elf32_m68hc11_size_stubs (output_bfd,
313 stub_file->the_bfd,
314 &link_info, 0))
315 {
316 einfo ("%X%P: can not size stub section: %E\n");
317 return;
318 }
319 if (!elf32_m68hc11_build_stubs (output_bfd, &link_info))
320 einfo ("%X%P: can not build stubs: %E\n");
321 }
322 }
323
324
325 /* Avoid processing the fake stub_file in vercheck, stat_needed and
326 check_needed routines. */
327
328 static void m68hc11_for_each_input_file_wrapper
329 PARAMS ((lang_input_statement_type *));
330 static void m68hc11_lang_for_each_input_file
331 PARAMS ((void (*) (lang_input_statement_type *)));
332
333 static void (*real_func) PARAMS ((lang_input_statement_type *));
334
335 static void m68hc11_for_each_input_file_wrapper (l)
336 lang_input_statement_type *l;
337 {
338 if (l != stub_file)
339 (*real_func) (l);
340 }
341
342 static void
343 m68hc11_lang_for_each_input_file (func)
344 void (*func) PARAMS ((lang_input_statement_type *));
345 {
346 real_func = func;
347 lang_for_each_input_file (&m68hc11_for_each_input_file_wrapper);
348 }
349
350 #define lang_for_each_input_file m68hc11_lang_for_each_input_file
351
352 EOF
353
354 # Define some shell vars to insert bits of code into the standard elf
355 # parse_args and list_options functions.
356 #
357 PARSE_AND_LIST_PROLOGUE='
358 #define OPTION_NO_TRAMPOLINE 300
359 #define OPTION_BANK_WINDOW 301
360 '
361
362 # The options are repeated below so that no abbreviations are allowed.
363 # Otherwise -s matches stub-group-size
364 PARSE_AND_LIST_LONGOPTS='
365 { "no-trampoline", no_argument, NULL, OPTION_NO_TRAMPOLINE },
366 { "bank-window", required_argument, NULL, OPTION_BANK_WINDOW },
367 '
368
369 PARSE_AND_LIST_OPTIONS='
370 fprintf (file, _(""
371 " --no-trampoline Do not generate the far trampolines used to call\n"
372 " a far function using 'jsr' or 'bsr'.\n"
373 " --bank-window NAME Specify the name of the memory region describing\n"
374 " the layout of the memory bank window.\n"
375 ));
376 '
377
378 PARSE_AND_LIST_ARGS_CASES='
379 case OPTION_NO_TRAMPOLINE:
380 no_trampoline = 1;
381 break;
382 case OPTION_BANK_WINDOW:
383 bank_window_name = optarg;
384 break;
385 '
386
387 # Put these extra m68hc11elf routines in ld_${EMULATION_NAME}_emulation
388 #
389 LDEMUL_BEFORE_ALLOCATION=m68hc11_elf_${EMULATION_NAME}_before_allocation
390 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
391 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=m68hc11elf_create_output_section_statements