* Makefile.am: Add dependency on ldemul-list.h for powerpc and
[deliverable/binutils-gdb.git] / ld / emultempl / spuelf.em
1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2006, 2007 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 static const char ovl_mgr[] = {
42 EOF
43
44 ../binutils/bin2c < ${srcdir}/emultempl/spu_ovl.o >> e${EMULATION_NAME}.c
45
46 cat >>e${EMULATION_NAME}.c <<EOF
47 };
48
49 static const struct _ovl_stream ovl_mgr_stream = {
50 ovl_mgr,
51 ovl_mgr + sizeof (ovl_mgr)
52 };
53
54 static asection *toe = NULL;
55
56
57 static int
58 is_spu_target (void)
59 {
60 extern const bfd_target bfd_elf32_spu_vec;
61
62 return link_info.hash->creator == &bfd_elf32_spu_vec;
63 }
64
65 /* Create our note section. */
66
67 static void
68 spu_after_open (void)
69 {
70 if (is_spu_target ()
71 && !link_info.relocatable
72 && link_info.input_bfds != NULL
73 && !spu_elf_create_sections (output_bfd, &link_info))
74 einfo ("%X%P: can not create note section: %E\n");
75
76 gld${EMULATION_NAME}_after_open ();
77 }
78
79 /* Add section S at the end of output section OUTPUT_NAME.
80
81 Really, we should be duplicating ldlang.c map_input_to_output_sections
82 logic here, ie. using the linker script to find where the section
83 goes. That's rather a lot of code, and we don't want to run
84 map_input_to_output_sections again because most sections are already
85 mapped. So cheat, and put the section in a fixed place, ignoring any
86 attempt via a linker script to put .stub, .ovtab, and built-in
87 overlay manager code somewhere else. */
88
89 static void
90 spu_place_special_section (asection *s, const char *output_name)
91 {
92 lang_output_section_statement_type *os;
93
94 os = lang_output_section_find (output_name);
95 if (os == NULL)
96 {
97 const char *save = s->name;
98 s->name = output_name;
99 gld${EMULATION_NAME}_place_orphan (s);
100 s->name = save;
101 }
102 else
103 lang_add_section (&os->children, s, os);
104
105 s->output_section->size += s->size;
106 }
107
108 /* Load built-in overlay manager, and tweak overlay section alignment. */
109
110 static void
111 spu_elf_load_ovl_mgr (void)
112 {
113 lang_output_section_statement_type *os;
114 struct elf_link_hash_entry *h;
115
116 h = elf_link_hash_lookup (elf_hash_table (&link_info),
117 "__ovly_load", FALSE, FALSE, FALSE);
118
119 if (h != NULL
120 && (h->root.type == bfd_link_hash_defined
121 || h->root.type == bfd_link_hash_defweak)
122 && h->def_regular)
123 {
124 /* User supplied __ovly_load. */
125 }
126 else if (ovl_mgr_stream.start == ovl_mgr_stream.end)
127 einfo ("%F%P: no built-in overlay manager\n");
128 else
129 {
130 lang_input_statement_type *ovl_is;
131
132 ovl_is = lang_add_input_file ("builtin ovl_mgr",
133 lang_input_file_is_file_enum,
134 NULL);
135
136 if (!spu_elf_open_builtin_lib (&ovl_is->the_bfd, &ovl_mgr_stream))
137 einfo ("%X%P: can not open built-in overlay manager: %E\n");
138 else
139 {
140 asection *in;
141
142 if (!load_symbols (ovl_is, NULL))
143 einfo ("%X%P: can not load built-in overlay manager: %E\n");
144
145 /* Map overlay manager sections to output sections. */
146 for (in = ovl_is->the_bfd->sections; in != NULL; in = in->next)
147 if ((in->flags & (SEC_ALLOC | SEC_LOAD))
148 == (SEC_ALLOC | SEC_LOAD))
149 spu_place_special_section (in, ".text");
150 }
151 }
152
153 /* Ensure alignment of overlay sections is sufficient. */
154 for (os = &lang_output_section_statement.head->output_section_statement;
155 os != NULL;
156 os = os->next)
157 if (os->bfd_section != NULL
158 && spu_elf_section_data (os->bfd_section) != NULL
159 && spu_elf_section_data (os->bfd_section)->ovl_index != 0)
160 {
161 if (os->bfd_section->alignment_power < 4)
162 os->bfd_section->alignment_power = 4;
163
164 /* Also ensure size rounds up. */
165 os->block_value = 16;
166 }
167 }
168
169 /* Go find if we need to do anything special for overlays. */
170
171 static void
172 spu_before_allocation (void)
173 {
174 if (is_spu_target ()
175 && !link_info.relocatable
176 && !no_overlays)
177 {
178 /* Size the sections. This is premature, but we need to know the
179 rough layout so that overlays can be found. */
180 expld.phase = lang_mark_phase_enum;
181 expld.dataseg.phase = exp_dataseg_none;
182 one_lang_size_sections_pass (NULL, TRUE);
183
184 /* Find overlays by inspecting section vmas. */
185 if (spu_elf_find_overlays (output_bfd, &link_info))
186 {
187 asection *stub, *ovtab;
188
189 if (!spu_elf_size_stubs (output_bfd, &link_info, non_overlay_stubs,
190 &stub, &ovtab, &toe))
191 einfo ("%X%P: can not size overlay stubs: %E\n");
192
193 if (stub != NULL)
194 {
195 spu_place_special_section (stub, ".text");
196 spu_place_special_section (ovtab, ".data");
197 spu_place_special_section (toe, ".toe");
198
199 spu_elf_load_ovl_mgr ();
200 }
201 }
202
203 /* We must not cache anything from the preliminary sizing. */
204 lang_reset_memory_regions ();
205 }
206
207 gld${EMULATION_NAME}_before_allocation ();
208 }
209
210 /* Final emulation specific call. */
211
212 static void
213 gld${EMULATION_NAME}_finish (void)
214 {
215 int need_laying_out;
216
217 need_laying_out = bfd_elf_discard_info (output_bfd, &link_info);
218
219 gld${EMULATION_NAME}_map_segments (need_laying_out);
220
221 if (is_spu_target () && local_store_lo < local_store_hi)
222 {
223 asection *s;
224
225 s = spu_elf_check_vma (output_bfd, local_store_lo, local_store_hi);
226 if (s != NULL)
227 einfo ("%X%P: %A exceeds local store range\n", s);
228 }
229
230 if (toe != NULL
231 && !spu_elf_build_stubs (&link_info,
232 emit_stub_syms || link_info.emitrelocations,
233 toe))
234 einfo ("%X%P: can not build overlay stubs: %E\n");
235
236 finish_default ();
237 }
238
239 EOF
240
241 if grep -q 'ld_elf.*ppc.*_emulation' ldemul-list.h; then
242 cat >>e${EMULATION_NAME}.c <<EOF
243 #include "filenames.h"
244 #include <fcntl.h>
245 #include <sys/wait.h>
246
247 struct tflist {
248 struct tflist *next;
249 char name[9];
250 };
251
252 static struct tflist *tmp_file_list;
253
254 static void clean_tmp (void)
255 {
256 for (; tmp_file_list != NULL; tmp_file_list = tmp_file_list->next)
257 unlink (tmp_file_list->name);
258 }
259
260 /* This function is called when building a ppc32 or ppc64 executable
261 to handle embedded spu images. */
262 extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
263
264 bfd_boolean
265 embedded_spu_file (lang_input_statement_type *entry, const char *flags)
266 {
267 const char *cmd[6];
268 const char *sym;
269 char *handle, *p;
270 struct tflist *tf;
271 char *oname;
272 int fd;
273 pid_t pid;
274 int status;
275 union lang_statement_union **old_stat_tail;
276 union lang_statement_union **old_file_tail;
277 union lang_statement_union *new_ent;
278
279 if (entry->the_bfd->format != bfd_object
280 || strcmp (entry->the_bfd->xvec->name, "elf32-spu") != 0
281 || (entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_EXEC
282 && entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_DYN))
283 return FALSE;
284
285 /* Use the filename as the symbol marking the program handle struct. */
286 sym = strrchr (entry->the_bfd->filename, '/');
287 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
288 {
289 char *bslash = strrchr (entry->the_bfd->filename, '\\');
290
291 if (sym == NULL || (bslash != NULL && bslash > sym))
292 sym = bslash;
293 if (sym == NULL
294 && entry->the_bfd->filename[0] != '\0'
295 && entry->the_bfd->filename[1] == ':')
296 sym = entry->the_bfd->filename + 1;
297 }
298 #endif
299 if (sym == NULL)
300 sym = entry->the_bfd->filename;
301 else
302 ++sym;
303
304 handle = xstrdup (sym);
305 for (p = handle; *p; ++p)
306 if (!(ISALNUM (*p) || *p == '$' || *p == '.'))
307 *p = '_';
308
309 if (tmp_file_list == NULL)
310 atexit (clean_tmp);
311 tf = xmalloc (sizeof (*tf));
312 tf->next = tmp_file_list;
313 tmp_file_list = tf;
314 oname = tf->name;
315 memcpy (tf->name, "ldXXXXXX", sizeof (tf->name));
316
317 #ifdef HAVE_MKSTEMP
318 fd = mkstemp (oname);
319 #else
320 oname = mktemp (oname);
321 if (oname == NULL)
322 return FALSE;
323 fd = open (oname, O_RDWR | O_CREAT | O_EXCL, 0600);
324 #endif
325 if (fd == -1)
326 return FALSE;
327 close (fd);
328
329 /* Use fork() and exec() rather than system() so that we don't
330 need to worry about quoting args. */
331 cmd[0] = "embedspu";
332 cmd[1] = flags;
333 cmd[2] = handle;
334 cmd[3] = entry->the_bfd->filename;
335 cmd[4] = oname;
336 cmd[5] = NULL;
337 if (trace_file_tries)
338 {
339 info_msg (_("running: %s \"%s\" \"%s\" \"%s\" \"%s\"\n"),
340 cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]);
341 fflush (stdout);
342 }
343
344 pid = fork ();
345 if (pid == -1)
346 return FALSE;
347 if (pid == 0)
348 {
349 execvp (cmd[0], (char *const *) cmd);
350 perror (cmd[0]);
351 _exit (127);
352 }
353 #ifdef HAVE_WAITPID
354 #define WAITFOR(PID, STAT) waitpid (PID, STAT, 0)
355 #else
356 #define WAITFOR(PID, STAT) wait (STAT)
357 #endif
358 if (WAITFOR (pid, &status) != pid
359 || !WIFEXITED (status)
360 || WEXITSTATUS (status) != 0)
361 return FALSE;
362 #undef WAITFOR
363
364 old_stat_tail = stat_ptr->tail;
365 old_file_tail = input_file_chain.tail;
366 if (lang_add_input_file (oname, lang_input_file_is_file_enum, NULL) == NULL)
367 return FALSE;
368
369 /* lang_add_input_file put the new list entry at the end of the statement
370 and input file lists. Move it to just after the current entry. */
371 new_ent = *old_stat_tail;
372 *old_stat_tail = NULL;
373 stat_ptr->tail = old_stat_tail;
374 *old_file_tail = NULL;
375 input_file_chain.tail = old_file_tail;
376 new_ent->header.next = entry->header.next;
377 entry->header.next = new_ent;
378 new_ent->input_statement.next_real_file = entry->next_real_file;
379 entry->next_real_file = new_ent;
380
381 /* Ensure bfd sections are excluded from the output. */
382 bfd_section_list_clear (entry->the_bfd);
383 entry->loaded = TRUE;
384 return TRUE;
385 }
386
387 EOF
388 fi
389
390 # Define some shell vars to insert bits of code into the standard elf
391 # parse_args and list_options functions.
392 #
393 PARSE_AND_LIST_PROLOGUE='
394 #define OPTION_SPU_PLUGIN 301
395 #define OPTION_SPU_NO_OVERLAYS (OPTION_SPU_PLUGIN + 1)
396 #define OPTION_SPU_STUB_SYMS (OPTION_SPU_NO_OVERLAYS + 1)
397 #define OPTION_SPU_NON_OVERLAY_STUBS (OPTION_SPU_STUB_SYMS + 1)
398 #define OPTION_SPU_LOCAL_STORE (OPTION_SPU_NON_OVERLAY_STUBS + 1)
399 '
400
401 PARSE_AND_LIST_LONGOPTS='
402 { "plugin", no_argument, NULL, OPTION_SPU_PLUGIN },
403 { "no-overlays", no_argument, NULL, OPTION_SPU_NO_OVERLAYS },
404 { "emit-stub-syms", no_argument, NULL, OPTION_SPU_STUB_SYMS },
405 { "extra-overlay-stubs", no_argument, NULL, OPTION_SPU_NON_OVERLAY_STUBS },
406 { "local-store", required_argument, NULL, OPTION_SPU_LOCAL_STORE },
407 '
408
409 PARSE_AND_LIST_OPTIONS='
410 fprintf (file, _("\
411 --plugin Make SPU plugin.\n\
412 --no-overlays No overlay handling.\n\
413 --emit-stub-syms Add symbols on overlay call stubs.\n\
414 --extra-overlay-stubs Add stubs on all calls out of overlay regions.\n\
415 --local-store=lo:hi Valid address range.\n"
416 ));
417 '
418
419 PARSE_AND_LIST_ARGS_CASES='
420 case OPTION_SPU_PLUGIN:
421 spu_elf_plugin (1);
422 break;
423
424 case OPTION_SPU_NO_OVERLAYS:
425 no_overlays = 1;
426 break;
427
428 case OPTION_SPU_STUB_SYMS:
429 emit_stub_syms = 1;
430 break;
431
432 case OPTION_SPU_NON_OVERLAY_STUBS:
433 non_overlay_stubs = 1;
434 break;
435
436 case OPTION_SPU_LOCAL_STORE:
437 {
438 char *end;
439 local_store_lo = strtoul (optarg, &end, 0);
440 if (*end == '\'':'\'')
441 {
442 local_store_hi = strtoul (end + 1, &end, 0);
443 if (*end == 0)
444 break;
445 }
446 einfo (_("%P%F: invalid --local-store address range `%s'\''\n"), optarg);
447 }
448 break;
449 '
450
451 LDEMUL_AFTER_OPEN=spu_after_open
452 LDEMUL_BEFORE_ALLOCATION=spu_before_allocation
453 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
This page took 0.039656 seconds and 4 git commands to generate.