* emultempl/spuelf.em (_binary_builtin_ovl_mgr_start): Rename
[external/binutils.git] / ld / emultempl / spuelf.em
1 # This shell script emits a C file. -*- C -*-
2 #   Copyright 2006 Free Software Foundation, Inc.
3 #
4 # This file is part of GLD, the Gnu Linker.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
19 #
20
21 # This file is sourced from elf32.em, and defines extra spu specific
22 # features.
23 #
24 cat >>e${EMULATION_NAME}.c <<EOF
25 #include "ldctor.h"
26 #include "elf32-spu.h"
27
28 /* Non-zero if no overlay processing should be done.  */
29 static int no_overlays = 0;
30
31 /* Non-zero if we want stubs on all calls out of overlay regions.  */
32 static int non_overlay_stubs = 0;
33
34 /* Whether to emit symbols for stubs.  */
35 static int emit_stub_syms = 0;
36
37 /* Range of valid addresses for loadable sections.  */
38 static bfd_vma local_store_lo = 0;
39 static bfd_vma local_store_hi = 0x3ffff;
40
41 extern void *_binary_spu_ovl_o_start;
42 extern void *_binary_spu_ovl_o_end;
43
44 static const struct _ovl_stream ovl_mgr_stream = {
45   &_binary_spu_ovl_o_start,
46   &_binary_spu_ovl_o_end
47 };
48
49 static asection *toe = NULL;
50
51
52 static int
53 is_spu_target (void)
54 {
55   extern const bfd_target bfd_elf32_spu_vec;
56
57   return link_info.hash->creator == &bfd_elf32_spu_vec;
58 }
59
60 /* Create our note section.  */
61
62 static void
63 spu_after_open (void)
64 {
65   if (is_spu_target ()
66       && !link_info.relocatable
67       && link_info.input_bfds != NULL
68       && !spu_elf_create_sections (output_bfd, &link_info))
69     einfo ("%X%P: can not create note section: %E\n");
70
71   gld${EMULATION_NAME}_after_open ();
72 }
73
74 /* Add section S at the end of output section OUTPUT_NAME.
75
76    Really, we should be duplicating ldlang.c map_input_to_output_sections
77    logic here, ie. using the linker script to find where the section
78    goes.  That's rather a lot of code, and we don't want to run
79    map_input_to_output_sections again because most sections are already
80    mapped.  So cheat, and put the section in a fixed place, ignoring any
81    attempt via a linker script to put .stub, .ovtab, and built-in
82    overlay manager code somewhere else.  */
83
84 static void
85 spu_place_special_section (asection *s, const char *output_name)
86 {
87   lang_output_section_statement_type *os;
88
89   os = lang_output_section_find (output_name);
90   if (os == NULL)
91     {
92       const char *save = s->name;
93       s->name = output_name;
94       gld${EMULATION_NAME}_place_orphan (s);
95       s->name = save;
96     }
97   else
98     lang_add_section (&os->children, s, os);
99
100   s->output_section->size += s->size;
101 }
102
103 /* Load built-in overlay manager, and tweak overlay section alignment.  */
104
105 static void
106 spu_elf_load_ovl_mgr (void)
107 {
108   lang_output_section_statement_type *os;
109   struct elf_link_hash_entry *h;
110
111   h = elf_link_hash_lookup (elf_hash_table (&link_info),
112                             "__ovly_load", FALSE, FALSE, FALSE);
113
114   if (h != NULL
115       && (h->root.type == bfd_link_hash_defined
116           || h->root.type == bfd_link_hash_defweak)
117       && h->def_regular)
118     {
119       /* User supplied __ovly_load.  */
120     }
121   else if (ovl_mgr_stream.start == ovl_mgr_stream.end)
122     einfo ("%F%P: no built-in overlay manager\n");
123   else
124     {
125       lang_input_statement_type *ovl_is;
126
127       ovl_is = lang_add_input_file ("builtin ovl_mgr",
128                                     lang_input_file_is_file_enum,
129                                     NULL);
130
131       if (!spu_elf_open_builtin_lib (&ovl_is->the_bfd, &ovl_mgr_stream))
132         einfo ("%X%P: can not open built-in overlay manager: %E\n");
133       else
134         {
135           asection *in;
136
137           if (!load_symbols (ovl_is, NULL))
138             einfo ("%X%P: can not load built-in overlay manager: %E\n");
139
140           /* Map overlay manager sections to output sections.  */
141           for (in = ovl_is->the_bfd->sections; in != NULL; in = in->next)
142             if ((in->flags & (SEC_ALLOC | SEC_LOAD))
143                 == (SEC_ALLOC | SEC_LOAD))
144               spu_place_special_section (in, ".text");
145         }
146     }
147
148   /* Ensure alignment of overlay sections is sufficient.  */
149   for (os = &lang_output_section_statement.head->output_section_statement;
150        os != NULL;
151        os = os->next)
152     if (os->bfd_section != NULL
153         && spu_elf_section_data (os->bfd_section) != NULL
154         && spu_elf_section_data (os->bfd_section)->ovl_index != 0)
155       {
156         if (os->bfd_section->alignment_power < 4)
157           os->bfd_section->alignment_power = 4;
158
159         /* Also ensure size rounds up.  */
160         os->block_value = 16;
161       }
162 }
163
164 /* Go find if we need to do anything special for overlays.  */
165
166 static void
167 spu_before_allocation (void)
168 {
169   if (is_spu_target ()
170       && !link_info.relocatable
171       && !no_overlays)
172     {
173       /* Size the sections.  This is premature, but we need to know the
174          rough layout so that overlays can be found.  */
175       expld.phase = lang_mark_phase_enum;
176       expld.dataseg.phase = exp_dataseg_none;
177       one_lang_size_sections_pass (NULL, TRUE);
178
179       /* Find overlays by inspecting section vmas.  */
180       if (spu_elf_find_overlays (output_bfd, &link_info))
181         {
182           asection *stub, *ovtab;
183
184           if (!spu_elf_size_stubs (output_bfd, &link_info, non_overlay_stubs,
185                                    &stub, &ovtab, &toe))
186             einfo ("%X%P: can not size overlay stubs: %E\n");
187
188           if (stub != NULL)
189             {
190               spu_place_special_section (stub, ".text");
191               spu_place_special_section (ovtab, ".data");
192               spu_place_special_section (toe, ".toe");
193
194               spu_elf_load_ovl_mgr ();
195             }
196         }
197
198       /* We must not cache anything from the preliminary sizing.  */
199       lang_reset_memory_regions ();
200     }
201
202   gld${EMULATION_NAME}_before_allocation ();
203 }
204
205 /* Final emulation specific call.  */
206
207 static void
208 gld${EMULATION_NAME}_finish (void)
209 {
210   int need_laying_out;
211
212   need_laying_out = bfd_elf_discard_info (output_bfd, &link_info);
213
214   gld${EMULATION_NAME}_map_segments (need_laying_out);
215
216   if (is_spu_target () && local_store_lo < local_store_hi)
217     {
218       asection *s;
219
220       s = spu_elf_check_vma (output_bfd, local_store_lo, local_store_hi);
221       if (s != NULL)
222         einfo ("%X%P: %A exceeds local store range\n", s);
223     }
224
225   if (toe != NULL
226       && !spu_elf_build_stubs (&link_info,
227                                emit_stub_syms || link_info.emitrelocations,
228                                toe))
229     einfo ("%X%P: can not build overlay stubs: %E\n");
230
231   finish_default ();
232 }
233
234 EOF
235
236 # Define some shell vars to insert bits of code into the standard elf
237 # parse_args and list_options functions.
238 #
239 PARSE_AND_LIST_PROLOGUE='
240 #define OPTION_SPU_PLUGIN               301
241 #define OPTION_SPU_NO_OVERLAYS          (OPTION_SPU_PLUGIN + 1)
242 #define OPTION_SPU_STUB_SYMS            (OPTION_SPU_NO_OVERLAYS + 1)
243 #define OPTION_SPU_NON_OVERLAY_STUBS    (OPTION_SPU_STUB_SYMS + 1)
244 #define OPTION_SPU_LOCAL_STORE          (OPTION_SPU_NON_OVERLAY_STUBS + 1)
245 '
246
247 PARSE_AND_LIST_LONGOPTS='
248   { "plugin", no_argument, NULL, OPTION_SPU_PLUGIN },
249   { "no-overlays", no_argument, NULL, OPTION_SPU_NO_OVERLAYS },
250   { "emit-stub-syms", no_argument, NULL, OPTION_SPU_STUB_SYMS },
251   { "extra-overlay-stubs", no_argument, NULL, OPTION_SPU_NON_OVERLAY_STUBS },
252   { "local-store", required_argument, NULL, OPTION_SPU_LOCAL_STORE },
253 '
254
255 PARSE_AND_LIST_OPTIONS='
256   fprintf (file, _("\
257   --plugin              Make SPU plugin.\n\
258   --no-overlays         No overlay handling.\n\
259   --emit-stub-syms      Add symbols on overlay call stubs.\n\
260   --extra-overlay-stubs Add stubs on all calls out of overlay regions.\n\
261   --local-store=lo:hi   Valid address range.\n"
262                    ));
263 '
264
265 PARSE_AND_LIST_ARGS_CASES='
266     case OPTION_SPU_PLUGIN:
267       spu_elf_plugin (1);
268       break;
269
270     case OPTION_SPU_NO_OVERLAYS:
271       no_overlays = 1;
272       break;
273
274     case OPTION_SPU_STUB_SYMS:
275       emit_stub_syms = 1;
276       break;
277
278     case OPTION_SPU_NON_OVERLAY_STUBS:
279       non_overlay_stubs = 1;
280       break;
281
282     case OPTION_SPU_LOCAL_STORE:
283       {
284         char *end;
285         local_store_lo = strtoul (optarg, &end, 0);
286         if (*end == '\'':'\'')
287           {
288             local_store_hi = strtoul (end + 1, &end, 0);
289             if (*end == 0)
290               break;
291           }
292         einfo (_("%P%F: invalid --local-store address range `%s'\''\n"), optarg);
293       }
294       break;
295 '
296
297 LDEMUL_AFTER_OPEN=spu_after_open
298 LDEMUL_BEFORE_ALLOCATION=spu_before_allocation
299 LDEMUL_FINISH=gld${EMULATION_NAME}_finish