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