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