include/
[deliverable/binutils-gdb.git] / ld / emultempl / spuelf.em
1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
3 #
4 # This file is part of the GNU Binutils.
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 3 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
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 # MA 02110-1301, USA.
20 #
21
22 # This file is sourced from elf32.em, and defines extra spu specific
23 # features.
24 #
25 fragment <<EOF
26 #include "ldctor.h"
27 #include "elf32-spu.h"
28
29 /* Non-zero if no overlay processing should be done. */
30 static int no_overlays = 0;
31
32 /* Non-zero if we want stubs on all calls out of overlay regions. */
33 static int non_overlay_stubs = 0;
34
35 /* Whether to emit symbols for stubs. */
36 static int emit_stub_syms = 0;
37
38 /* Non-zero to perform stack space analysis. */
39 static int stack_analysis = 0;
40
41 /* Whether to emit symbols with stack requirements for each function. */
42 static int emit_stack_syms = 0;
43
44 /* Range of valid addresses for loadable sections. */
45 static bfd_vma local_store_lo = 0;
46 static bfd_vma local_store_hi = 0x3ffff;
47
48 /* Control --auto-overlay feature. */
49 static int auto_overlay = 0;
50 static char *auto_overlay_file = 0;
51 static unsigned int auto_overlay_fixed = 0;
52 static unsigned int auto_overlay_reserved = 0;
53 int my_argc;
54 char **my_argv;
55
56 static const char ovl_mgr[] = {
57 EOF
58
59 ../binutils/bin2c < ${srcdir}/emultempl/spu_ovl.o >> e${EMULATION_NAME}.c
60
61 fragment <<EOF
62 };
63
64 static const struct _ovl_stream ovl_mgr_stream = {
65 ovl_mgr,
66 ovl_mgr + sizeof (ovl_mgr)
67 };
68
69
70 static int
71 is_spu_target (void)
72 {
73 extern const bfd_target bfd_elf32_spu_vec;
74
75 return link_info.output_bfd->xvec == &bfd_elf32_spu_vec;
76 }
77
78 /* Create our note section. */
79
80 static void
81 spu_after_open (void)
82 {
83 if (is_spu_target ()
84 && !link_info.relocatable
85 && link_info.input_bfds != NULL
86 && !spu_elf_create_sections (&link_info,
87 stack_analysis, emit_stack_syms))
88 einfo ("%X%P: can not create note section: %E\n");
89
90 gld${EMULATION_NAME}_after_open ();
91 }
92
93 /* If O is NULL, add section S at the end of output section OUTPUT_NAME.
94 If O is not NULL, add section S at the beginning of output section O.
95
96 Really, we should be duplicating ldlang.c map_input_to_output_sections
97 logic here, ie. using the linker script to find where the section
98 goes. That's rather a lot of code, and we don't want to run
99 map_input_to_output_sections again because most sections are already
100 mapped. So cheat, and put the section in a fixed place, ignoring any
101 attempt via a linker script to put .stub, .ovtab, and built-in
102 overlay manager code somewhere else. */
103
104 static void
105 spu_place_special_section (asection *s, asection *o, const char *output_name)
106 {
107 lang_output_section_statement_type *os;
108
109 os = lang_output_section_find (o != NULL ? o->name : output_name);
110 if (os == NULL)
111 {
112 const char *save = s->name;
113 s->name = output_name;
114 gld${EMULATION_NAME}_place_orphan (s);
115 s->name = save;
116 }
117 else if (o != NULL && os->children.head != NULL)
118 {
119 lang_statement_list_type add;
120
121 lang_list_init (&add);
122 lang_add_section (&add, s, os);
123 *add.tail = os->children.head;
124 os->children.head = add.head;
125 }
126 else
127 lang_add_section (&os->children, s, os);
128
129 s->output_section->size += s->size;
130 }
131
132 /* Load built-in overlay manager, and tweak overlay section alignment. */
133
134 static void
135 spu_elf_load_ovl_mgr (void)
136 {
137 lang_output_section_statement_type *os;
138 struct elf_link_hash_entry *h;
139
140 h = elf_link_hash_lookup (elf_hash_table (&link_info),
141 "__ovly_load", FALSE, FALSE, FALSE);
142
143 if (h != NULL
144 && (h->root.type == bfd_link_hash_defined
145 || h->root.type == bfd_link_hash_defweak)
146 && h->def_regular)
147 {
148 /* User supplied __ovly_load. */
149 }
150 else if (ovl_mgr_stream.start == ovl_mgr_stream.end)
151 einfo ("%F%P: no built-in overlay manager\n");
152 else
153 {
154 lang_input_statement_type *ovl_is;
155
156 ovl_is = lang_add_input_file ("builtin ovl_mgr",
157 lang_input_file_is_file_enum,
158 NULL);
159
160 if (!spu_elf_open_builtin_lib (&ovl_is->the_bfd, &ovl_mgr_stream))
161 einfo ("%X%P: can not open built-in overlay manager: %E\n");
162 else
163 {
164 asection *in;
165
166 if (!load_symbols (ovl_is, NULL))
167 einfo ("%X%P: can not load built-in overlay manager: %E\n");
168
169 /* Map overlay manager sections to output sections. */
170 for (in = ovl_is->the_bfd->sections; in != NULL; in = in->next)
171 if ((in->flags & (SEC_ALLOC | SEC_LOAD))
172 == (SEC_ALLOC | SEC_LOAD))
173 spu_place_special_section (in, NULL, ".text");
174 }
175 }
176
177 /* Ensure alignment of overlay sections is sufficient. */
178 for (os = &lang_output_section_statement.head->output_section_statement;
179 os != NULL;
180 os = os->next)
181 if (os->bfd_section != NULL
182 && spu_elf_section_data (os->bfd_section) != NULL
183 && spu_elf_section_data (os->bfd_section)->u.o.ovl_index != 0)
184 {
185 if (os->bfd_section->alignment_power < 4)
186 os->bfd_section->alignment_power = 4;
187
188 /* Also ensure size rounds up. */
189 os->block_value = 16;
190 }
191 }
192
193 /* Go find if we need to do anything special for overlays. */
194
195 static void
196 spu_before_allocation (void)
197 {
198 if (is_spu_target ()
199 && !link_info.relocatable
200 && !no_overlays)
201 {
202 /* Size the sections. This is premature, but we need to know the
203 rough layout so that overlays can be found. */
204 expld.phase = lang_mark_phase_enum;
205 expld.dataseg.phase = exp_dataseg_none;
206 one_lang_size_sections_pass (NULL, TRUE);
207
208 /* Find overlays by inspecting section vmas. */
209 if (spu_elf_find_overlays (&link_info))
210 {
211 int ret;
212
213 if (auto_overlay != 0)
214 {
215 einfo ("%P: --auto-overlay ignored with user overlay script\n");
216 auto_overlay = 0;
217 }
218
219 ret = spu_elf_size_stubs (&link_info,
220 spu_place_special_section,
221 non_overlay_stubs);
222 if (ret == 0)
223 einfo ("%X%P: can not size overlay stubs: %E\n");
224 else if (ret == 2)
225 spu_elf_load_ovl_mgr ();
226 }
227
228 /* We must not cache anything from the preliminary sizing. */
229 lang_reset_memory_regions ();
230 }
231
232 gld${EMULATION_NAME}_before_allocation ();
233 }
234
235 struct tflist {
236 struct tflist *next;
237 char name[9];
238 };
239
240 static struct tflist *tmp_file_list;
241
242 static void clean_tmp (void)
243 {
244 for (; tmp_file_list != NULL; tmp_file_list = tmp_file_list->next)
245 unlink (tmp_file_list->name);
246 }
247
248 static int
249 new_tmp_file (char **fname)
250 {
251 struct tflist *tf;
252 int fd;
253
254 if (tmp_file_list == NULL)
255 atexit (clean_tmp);
256 tf = xmalloc (sizeof (*tf));
257 tf->next = tmp_file_list;
258 tmp_file_list = tf;
259 memcpy (tf->name, "ldXXXXXX", sizeof (tf->name));
260 *fname = tf->name;
261 #ifdef HAVE_MKSTEMP
262 fd = mkstemp (*fname);
263 #else
264 *fname = mktemp (*fname);
265 if (*fname == NULL)
266 return -1;
267 fd = open (fname, O_RDWR | O_CREAT | O_EXCL, 0600);
268 #endif
269 return fd;
270 }
271
272 static FILE *
273 spu_elf_open_overlay_script (void)
274 {
275 FILE *script = NULL;
276
277 if (auto_overlay_file == NULL)
278 {
279 int fd = new_tmp_file (&auto_overlay_file);
280 if (fd == -1)
281 goto file_err;
282 script = fdopen (fd, "w");
283 }
284 else
285 script = fopen (auto_overlay_file, "w");
286
287 if (script == NULL)
288 {
289 file_err:
290 einfo ("%F%P: can not open script: %E\n");
291 }
292 return script;
293 }
294
295 static void
296 spu_elf_relink (void)
297 {
298 char **argv = xmalloc ((my_argc + 4) * sizeof (*argv));
299
300 memcpy (argv, my_argv, my_argc * sizeof (*argv));
301 argv[my_argc++] = "--no-auto-overlay";
302 if (tmp_file_list->name == auto_overlay_file)
303 argv[my_argc - 1] = concat (argv[my_argc - 1], "=",
304 auto_overlay_file, (const char *) NULL);
305 argv[my_argc++] = "-T";
306 argv[my_argc++] = auto_overlay_file;
307 argv[my_argc] = 0;
308 execvp (argv[0], (char *const *) argv);
309 perror (argv[0]);
310 _exit (127);
311 }
312
313 /* Final emulation specific call. */
314
315 static void
316 gld${EMULATION_NAME}_finish (void)
317 {
318 int need_laying_out;
319
320 need_laying_out = bfd_elf_discard_info (link_info.output_bfd, &link_info);
321
322 gld${EMULATION_NAME}_map_segments (need_laying_out);
323
324 if (is_spu_target ())
325 {
326 if (local_store_lo < local_store_hi)
327 {
328 asection *s;
329
330 s = spu_elf_check_vma (&link_info, auto_overlay,
331 local_store_lo, local_store_hi,
332 auto_overlay_fixed, auto_overlay_reserved,
333 spu_elf_load_ovl_mgr,
334 spu_elf_open_overlay_script,
335 spu_elf_relink);
336 if (s != NULL && !auto_overlay)
337 einfo ("%X%P: %A exceeds local store range\n", s);
338 }
339 else if (auto_overlay)
340 einfo ("%P: --auto-overlay ignored with zero local store range\n");
341
342 if (!spu_elf_build_stubs (&link_info,
343 emit_stub_syms || link_info.emitrelocations))
344 einfo ("%X%P: can not build overlay stubs: %E\n");
345 }
346
347 finish_default ();
348 }
349
350 static char *
351 gld${EMULATION_NAME}_choose_target (int argc, char *argv[])
352 {
353 link_info.path_separator = ':';
354 my_argc = argc;
355 my_argv = argv;
356 return ldemul_default_target (argc, argv);
357 }
358
359 EOF
360
361 if grep -q 'ld_elf.*ppc.*_emulation' ldemul-list.h; then
362 fragment <<EOF
363 #include "filenames.h"
364 #include <fcntl.h>
365 #include <sys/wait.h>
366
367 static const char *
368 base_name (const char *path)
369 {
370 const char *file = strrchr (path, '/');
371 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
372 {
373 char *bslash = strrchr (path, '\\\\');
374
375 if (file == NULL || (bslash != NULL && bslash > file))
376 file = bslash;
377 if (file == NULL
378 && path[0] != '\0'
379 && path[1] == ':')
380 file = path + 1;
381 }
382 #endif
383 if (file == NULL)
384 file = path;
385 else
386 ++file;
387 return file;
388 }
389
390 /* This function is called when building a ppc32 or ppc64 executable
391 to handle embedded spu images. */
392 extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
393
394 bfd_boolean
395 embedded_spu_file (lang_input_statement_type *entry, const char *flags)
396 {
397 const char *cmd[6];
398 const char *sym;
399 char *handle, *p;
400 char *oname;
401 int fd;
402 pid_t pid;
403 int status;
404 union lang_statement_union **old_stat_tail;
405 union lang_statement_union **old_file_tail;
406 union lang_statement_union *new_ent;
407 lang_input_statement_type *search;
408
409 if (entry->the_bfd->format != bfd_object
410 || strcmp (entry->the_bfd->xvec->name, "elf32-spu") != 0
411 || (entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_EXEC
412 && entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_DYN))
413 return FALSE;
414
415 /* Use the filename as the symbol marking the program handle struct. */
416 sym = base_name (entry->the_bfd->filename);
417
418 handle = xstrdup (sym);
419 for (p = handle; *p; ++p)
420 if (!(ISALNUM (*p) || *p == '$' || *p == '.'))
421 *p = '_';
422
423 fd = new_tmp_file (&oname);
424 if (fd == -1)
425 return FALSE;
426 close (fd);
427
428 for (search = (lang_input_statement_type *) input_file_chain.head;
429 search != NULL;
430 search = (lang_input_statement_type *) search->next_real_file)
431 if (search->filename != NULL)
432 {
433 const char *infile = base_name (search->filename);
434
435 if (strncmp (infile, "crtbegin", 8) == 0)
436 {
437 if (infile[8] == 'S')
438 flags = concat (flags, " -fPIC", (const char *) NULL);
439 else if (infile[8] == 'T')
440 flags = concat (flags, " -fpie", (const char *) NULL);
441 break;
442 }
443 }
444
445 /* Use fork() and exec() rather than system() so that we don't
446 need to worry about quoting args. */
447 cmd[0] = EMBEDSPU;
448 cmd[1] = flags;
449 cmd[2] = handle;
450 cmd[3] = entry->the_bfd->filename;
451 cmd[4] = oname;
452 cmd[5] = NULL;
453 if (trace_file_tries)
454 {
455 info_msg (_("running: %s \"%s\" \"%s\" \"%s\" \"%s\"\n"),
456 cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]);
457 fflush (stdout);
458 }
459
460 pid = fork ();
461 if (pid == -1)
462 return FALSE;
463 if (pid == 0)
464 {
465 execvp (cmd[0], (char *const *) cmd);
466 if (strcmp ("embedspu", EMBEDSPU) != 0)
467 {
468 cmd[0] = "embedspu";
469 execvp (cmd[0], (char *const *) cmd);
470 }
471 perror (cmd[0]);
472 _exit (127);
473 }
474 #ifdef HAVE_WAITPID
475 #define WAITFOR(PID, STAT) waitpid (PID, STAT, 0)
476 #else
477 #define WAITFOR(PID, STAT) wait (STAT)
478 #endif
479 if (WAITFOR (pid, &status) != pid
480 || !WIFEXITED (status)
481 || WEXITSTATUS (status) != 0)
482 return FALSE;
483 #undef WAITFOR
484
485 old_stat_tail = stat_ptr->tail;
486 old_file_tail = input_file_chain.tail;
487 if (lang_add_input_file (oname, lang_input_file_is_file_enum, NULL) == NULL)
488 return FALSE;
489
490 /* lang_add_input_file put the new list entry at the end of the statement
491 and input file lists. Move it to just after the current entry. */
492 new_ent = *old_stat_tail;
493 *old_stat_tail = NULL;
494 stat_ptr->tail = old_stat_tail;
495 *old_file_tail = NULL;
496 input_file_chain.tail = old_file_tail;
497 new_ent->header.next = entry->header.next;
498 entry->header.next = new_ent;
499 new_ent->input_statement.next_real_file = entry->next_real_file;
500 entry->next_real_file = new_ent;
501
502 /* Ensure bfd sections are excluded from the output. */
503 bfd_section_list_clear (entry->the_bfd);
504 entry->loaded = TRUE;
505 return TRUE;
506 }
507
508 EOF
509 fi
510
511 # Define some shell vars to insert bits of code into the standard elf
512 # parse_args and list_options functions.
513 #
514 PARSE_AND_LIST_PROLOGUE='
515 #define OPTION_SPU_PLUGIN 301
516 #define OPTION_SPU_NO_OVERLAYS (OPTION_SPU_PLUGIN + 1)
517 #define OPTION_SPU_STUB_SYMS (OPTION_SPU_NO_OVERLAYS + 1)
518 #define OPTION_SPU_NON_OVERLAY_STUBS (OPTION_SPU_STUB_SYMS + 1)
519 #define OPTION_SPU_LOCAL_STORE (OPTION_SPU_NON_OVERLAY_STUBS + 1)
520 #define OPTION_SPU_STACK_ANALYSIS (OPTION_SPU_LOCAL_STORE + 1)
521 #define OPTION_SPU_STACK_SYMS (OPTION_SPU_STACK_ANALYSIS + 1)
522 #define OPTION_SPU_AUTO_OVERLAY (OPTION_SPU_STACK_SYMS + 1)
523 #define OPTION_SPU_AUTO_RELINK (OPTION_SPU_AUTO_OVERLAY + 1)
524 #define OPTION_SPU_OVERLAY_RODATA (OPTION_SPU_AUTO_RELINK + 1)
525 #define OPTION_SPU_FIXED_SPACE (OPTION_SPU_OVERLAY_RODATA + 1)
526 #define OPTION_SPU_RESERVED_SPACE (OPTION_SPU_FIXED_SPACE + 1)
527 #define OPTION_SPU_NO_AUTO_OVERLAY (OPTION_SPU_RESERVED_SPACE + 1)
528 '
529
530 PARSE_AND_LIST_LONGOPTS='
531 { "plugin", no_argument, NULL, OPTION_SPU_PLUGIN },
532 { "no-overlays", no_argument, NULL, OPTION_SPU_NO_OVERLAYS },
533 { "emit-stub-syms", no_argument, NULL, OPTION_SPU_STUB_SYMS },
534 { "extra-overlay-stubs", no_argument, NULL, OPTION_SPU_NON_OVERLAY_STUBS },
535 { "local-store", required_argument, NULL, OPTION_SPU_LOCAL_STORE },
536 { "stack-analysis", no_argument, NULL, OPTION_SPU_STACK_ANALYSIS },
537 { "emit-stack-syms", no_argument, NULL, OPTION_SPU_STACK_SYMS },
538 { "auto-overlay", optional_argument, NULL, OPTION_SPU_AUTO_OVERLAY },
539 { "auto-relink", no_argument, NULL, OPTION_SPU_AUTO_RELINK },
540 { "overlay-rodata", no_argument, NULL, OPTION_SPU_OVERLAY_RODATA },
541 { "fixed-space", required_argument, NULL, OPTION_SPU_FIXED_SPACE },
542 { "reserved-space", required_argument, NULL, OPTION_SPU_RESERVED_SPACE },
543 { "no-auto-overlay", optional_argument, NULL, OPTION_SPU_NO_AUTO_OVERLAY },
544 '
545
546 PARSE_AND_LIST_OPTIONS='
547 fprintf (file, _("\
548 --plugin Make SPU plugin.\n\
549 --no-overlays No overlay handling.\n\
550 --emit-stub-syms Add symbols on overlay call stubs.\n\
551 --extra-overlay-stubs Add stubs on all calls out of overlay regions.\n\
552 --local-store=lo:hi Valid address range.\n\
553 --stack-analysis Estimate maximum stack requirement.\n\
554 --emit-stack-syms Add sym giving stack needed for each func.\n\
555 --auto-overlay [=filename] Create an overlay script in filename if\n\
556 executable does not fit in local store.\n\
557 --auto-relink Rerun linker using auto-overlay script.\n\
558 --overlay-rodata Place read-only data with associated function\n\
559 code in overlays.\n\
560 --fixed-space=bytes Local store for non-overlay code and data.\n\
561 --reserved-space=bytes Local store for stack and heap.\n"
562 ));
563 '
564
565 PARSE_AND_LIST_ARGS_CASES='
566 case OPTION_SPU_PLUGIN:
567 spu_elf_plugin (1);
568 break;
569
570 case OPTION_SPU_NO_OVERLAYS:
571 no_overlays = 1;
572 break;
573
574 case OPTION_SPU_STUB_SYMS:
575 emit_stub_syms = 1;
576 break;
577
578 case OPTION_SPU_NON_OVERLAY_STUBS:
579 non_overlay_stubs = 1;
580 break;
581
582 case OPTION_SPU_LOCAL_STORE:
583 {
584 char *end;
585 local_store_lo = strtoul (optarg, &end, 0);
586 if (*end == '\'':'\'')
587 {
588 local_store_hi = strtoul (end + 1, &end, 0);
589 if (*end == 0)
590 break;
591 }
592 einfo (_("%P%F: invalid --local-store address range `%s'\''\n"), optarg);
593 }
594 break;
595
596 case OPTION_SPU_STACK_ANALYSIS:
597 stack_analysis = 1;
598 break;
599
600 case OPTION_SPU_STACK_SYMS:
601 emit_stack_syms = 1;
602 break;
603
604 case OPTION_SPU_AUTO_OVERLAY:
605 auto_overlay |= 1;
606 if (optarg != NULL)
607 {
608 auto_overlay_file = optarg;
609 break;
610 }
611 /* Fall thru */
612
613 case OPTION_SPU_AUTO_RELINK:
614 auto_overlay |= 2;
615 break;
616
617 case OPTION_SPU_OVERLAY_RODATA:
618 auto_overlay |= 4;
619 break;
620
621 case OPTION_SPU_FIXED_SPACE:
622 {
623 char *end;
624 auto_overlay_fixed = strtoul (optarg, &end, 0);
625 if (*end != 0)
626 einfo (_("%P%F: invalid --fixed-space value `%s'\''\n"), optarg);
627 }
628 break;
629
630 case OPTION_SPU_RESERVED_SPACE:
631 {
632 char *end;
633 auto_overlay_reserved = strtoul (optarg, &end, 0);
634 if (*end != 0)
635 einfo (_("%P%F: invalid --reserved-space value `%s'\''\n"), optarg);
636 }
637 break;
638
639 case OPTION_SPU_NO_AUTO_OVERLAY:
640 auto_overlay = 0;
641 if (optarg != NULL)
642 {
643 struct tflist *tf;
644 size_t len;
645
646 if (tmp_file_list == NULL)
647 atexit (clean_tmp);
648
649 len = strlen (optarg) + 1;
650 tf = xmalloc (sizeof (*tf) - sizeof (tf->name) + len);
651 memcpy (tf->name, optarg, len);
652 tf->next = tmp_file_list;
653 tmp_file_list = tf;
654 break;
655 }
656 break;
657 '
658
659 LDEMUL_AFTER_OPEN=spu_after_open
660 LDEMUL_BEFORE_ALLOCATION=spu_before_allocation
661 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
662 LDEMUL_CHOOSE_TARGET=gld${EMULATION_NAME}_choose_target
This page took 0.045758 seconds and 5 git commands to generate.