1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2006, 2007 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
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.
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.
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,
22 # This file is sourced from elf32.em, and defines extra spu specific
27 #include "elf32-spu.h"
29 /* Non-zero if no overlay processing should be done. */
30 static int no_overlays = 0;
32 /* Non-zero if we want stubs on all calls out of overlay regions. */
33 static int non_overlay_stubs = 0;
35 /* Whether to emit symbols for stubs. */
36 static int emit_stub_syms = 0;
38 /* Non-zero to perform stack space analysis. */
39 static int stack_analysis = 0;
41 /* Whether to emit symbols with stack requirements for each function. */
42 static int emit_stack_syms = 0;
44 /* Range of valid addresses for loadable sections. */
45 static bfd_vma local_store_lo = 0;
46 static bfd_vma local_store_hi = 0x3ffff;
48 static const char ovl_mgr[] = {
51 ../binutils/bin2c < ${srcdir}/emultempl/spu_ovl.o >> e${EMULATION_NAME}.c
56 static const struct _ovl_stream ovl_mgr_stream = {
58 ovl_mgr + sizeof (ovl_mgr)
61 static asection *toe = NULL;
67 extern const bfd_target bfd_elf32_spu_vec;
69 return link_info.hash->creator == &bfd_elf32_spu_vec;
72 /* Create our note section. */
78 && !link_info.relocatable
79 && link_info.input_bfds != NULL
80 && !spu_elf_create_sections (output_bfd, &link_info,
81 stack_analysis, emit_stack_syms))
82 einfo ("%X%P: can not create note section: %E\n");
84 gld${EMULATION_NAME}_after_open ();
87 /* Add section S at the end of output section OUTPUT_NAME.
89 Really, we should be duplicating ldlang.c map_input_to_output_sections
90 logic here, ie. using the linker script to find where the section
91 goes. That's rather a lot of code, and we don't want to run
92 map_input_to_output_sections again because most sections are already
93 mapped. So cheat, and put the section in a fixed place, ignoring any
94 attempt via a linker script to put .stub, .ovtab, and built-in
95 overlay manager code somewhere else. */
98 spu_place_special_section (asection *s, const char *output_name)
100 lang_output_section_statement_type *os;
102 os = lang_output_section_find (output_name);
105 const char *save = s->name;
106 s->name = output_name;
107 gld${EMULATION_NAME}_place_orphan (s);
111 lang_add_section (&os->children, s, os);
113 s->output_section->size += s->size;
116 /* Load built-in overlay manager, and tweak overlay section alignment. */
119 spu_elf_load_ovl_mgr (void)
121 lang_output_section_statement_type *os;
122 struct elf_link_hash_entry *h;
124 h = elf_link_hash_lookup (elf_hash_table (&link_info),
125 "__ovly_load", FALSE, FALSE, FALSE);
128 && (h->root.type == bfd_link_hash_defined
129 || h->root.type == bfd_link_hash_defweak)
132 /* User supplied __ovly_load. */
134 else if (ovl_mgr_stream.start == ovl_mgr_stream.end)
135 einfo ("%F%P: no built-in overlay manager\n");
138 lang_input_statement_type *ovl_is;
140 ovl_is = lang_add_input_file ("builtin ovl_mgr",
141 lang_input_file_is_file_enum,
144 if (!spu_elf_open_builtin_lib (&ovl_is->the_bfd, &ovl_mgr_stream))
145 einfo ("%X%P: can not open built-in overlay manager: %E\n");
150 if (!load_symbols (ovl_is, NULL))
151 einfo ("%X%P: can not load built-in overlay manager: %E\n");
153 /* Map overlay manager sections to output sections. */
154 for (in = ovl_is->the_bfd->sections; in != NULL; in = in->next)
155 if ((in->flags & (SEC_ALLOC | SEC_LOAD))
156 == (SEC_ALLOC | SEC_LOAD))
157 spu_place_special_section (in, ".text");
161 /* Ensure alignment of overlay sections is sufficient. */
162 for (os = &lang_output_section_statement.head->output_section_statement;
165 if (os->bfd_section != NULL
166 && spu_elf_section_data (os->bfd_section) != NULL
167 && spu_elf_section_data (os->bfd_section)->ovl_index != 0)
169 if (os->bfd_section->alignment_power < 4)
170 os->bfd_section->alignment_power = 4;
172 /* Also ensure size rounds up. */
173 os->block_value = 16;
177 /* Go find if we need to do anything special for overlays. */
180 spu_before_allocation (void)
183 && !link_info.relocatable
186 /* Size the sections. This is premature, but we need to know the
187 rough layout so that overlays can be found. */
188 expld.phase = lang_mark_phase_enum;
189 expld.dataseg.phase = exp_dataseg_none;
190 one_lang_size_sections_pass (NULL, TRUE);
192 /* Find overlays by inspecting section vmas. */
193 if (spu_elf_find_overlays (output_bfd, &link_info))
195 asection *stub, *ovtab;
197 if (!spu_elf_size_stubs (output_bfd, &link_info, non_overlay_stubs,
198 stack_analysis, &stub, &ovtab, &toe))
199 einfo ("%X%P: can not size overlay stubs: %E\n");
203 spu_place_special_section (stub, ".text");
204 spu_place_special_section (ovtab, ".data");
205 spu_place_special_section (toe, ".toe");
207 spu_elf_load_ovl_mgr ();
211 /* We must not cache anything from the preliminary sizing. */
212 lang_reset_memory_regions ();
215 gld${EMULATION_NAME}_before_allocation ();
218 /* Final emulation specific call. */
221 gld${EMULATION_NAME}_finish (void)
225 need_laying_out = bfd_elf_discard_info (output_bfd, &link_info);
227 gld${EMULATION_NAME}_map_segments (need_laying_out);
229 if (is_spu_target () && local_store_lo < local_store_hi)
233 s = spu_elf_check_vma (output_bfd, local_store_lo, local_store_hi);
235 einfo ("%X%P: %A exceeds local store range\n", s);
239 && !spu_elf_build_stubs (&link_info,
240 emit_stub_syms || link_info.emitrelocations,
242 einfo ("%X%P: can not build overlay stubs: %E\n");
249 if grep -q 'ld_elf.*ppc.*_emulation' ldemul-list.h; then
251 #include "filenames.h"
253 #include <sys/wait.h>
260 static struct tflist *tmp_file_list;
262 static void clean_tmp (void)
264 for (; tmp_file_list != NULL; tmp_file_list = tmp_file_list->next)
265 unlink (tmp_file_list->name);
269 base_name (const char *path)
271 const char *file = strrchr (path, '/');
272 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
274 char *bslash = strrchr (path, '\\\\');
276 if (file == NULL || (bslash != NULL && bslash > file))
291 /* This function is called when building a ppc32 or ppc64 executable
292 to handle embedded spu images. */
293 extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
296 embedded_spu_file (lang_input_statement_type *entry, const char *flags)
306 union lang_statement_union **old_stat_tail;
307 union lang_statement_union **old_file_tail;
308 union lang_statement_union *new_ent;
309 lang_input_statement_type *search;
311 if (entry->the_bfd->format != bfd_object
312 || strcmp (entry->the_bfd->xvec->name, "elf32-spu") != 0
313 || (entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_EXEC
314 && entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_DYN))
317 /* Use the filename as the symbol marking the program handle struct. */
318 sym = base_name (entry->the_bfd->filename);
320 handle = xstrdup (sym);
321 for (p = handle; *p; ++p)
322 if (!(ISALNUM (*p) || *p == '$' || *p == '.'))
325 if (tmp_file_list == NULL)
327 tf = xmalloc (sizeof (*tf));
328 tf->next = tmp_file_list;
331 memcpy (tf->name, "ldXXXXXX", sizeof (tf->name));
334 fd = mkstemp (oname);
336 oname = mktemp (oname);
339 fd = open (oname, O_RDWR | O_CREAT | O_EXCL, 0600);
345 for (search = (lang_input_statement_type *) input_file_chain.head;
347 search = (lang_input_statement_type *) search->next_real_file)
348 if (search->filename != NULL)
350 const char *infile = base_name (search->filename);
352 if (strncmp (infile, "crtbegin", 8) == 0)
354 if (infile[8] == 'S')
355 flags = concat (flags, " -fPIC", NULL);
356 else if (infile[8] == 'T')
357 flags = concat (flags, " -fpie", NULL);
362 /* Use fork() and exec() rather than system() so that we don't
363 need to worry about quoting args. */
367 cmd[3] = entry->the_bfd->filename;
370 if (trace_file_tries)
372 info_msg (_("running: %s \"%s\" \"%s\" \"%s\" \"%s\"\n"),
373 cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]);
382 execvp (cmd[0], (char *const *) cmd);
383 if (strcmp ("embedspu", EMBEDSPU) != 0)
386 execvp (cmd[0], (char *const *) cmd);
392 #define WAITFOR(PID, STAT) waitpid (PID, STAT, 0)
394 #define WAITFOR(PID, STAT) wait (STAT)
396 if (WAITFOR (pid, &status) != pid
397 || !WIFEXITED (status)
398 || WEXITSTATUS (status) != 0)
402 old_stat_tail = stat_ptr->tail;
403 old_file_tail = input_file_chain.tail;
404 if (lang_add_input_file (oname, lang_input_file_is_file_enum, NULL) == NULL)
407 /* lang_add_input_file put the new list entry at the end of the statement
408 and input file lists. Move it to just after the current entry. */
409 new_ent = *old_stat_tail;
410 *old_stat_tail = NULL;
411 stat_ptr->tail = old_stat_tail;
412 *old_file_tail = NULL;
413 input_file_chain.tail = old_file_tail;
414 new_ent->header.next = entry->header.next;
415 entry->header.next = new_ent;
416 new_ent->input_statement.next_real_file = entry->next_real_file;
417 entry->next_real_file = new_ent;
419 /* Ensure bfd sections are excluded from the output. */
420 bfd_section_list_clear (entry->the_bfd);
421 entry->loaded = TRUE;
428 # Define some shell vars to insert bits of code into the standard elf
429 # parse_args and list_options functions.
431 PARSE_AND_LIST_PROLOGUE='
432 #define OPTION_SPU_PLUGIN 301
433 #define OPTION_SPU_NO_OVERLAYS (OPTION_SPU_PLUGIN + 1)
434 #define OPTION_SPU_STUB_SYMS (OPTION_SPU_NO_OVERLAYS + 1)
435 #define OPTION_SPU_NON_OVERLAY_STUBS (OPTION_SPU_STUB_SYMS + 1)
436 #define OPTION_SPU_LOCAL_STORE (OPTION_SPU_NON_OVERLAY_STUBS + 1)
437 #define OPTION_SPU_STACK_ANALYSIS (OPTION_SPU_LOCAL_STORE + 1)
438 #define OPTION_SPU_STACK_SYMS (OPTION_SPU_STACK_ANALYSIS + 1)
441 PARSE_AND_LIST_LONGOPTS='
442 { "plugin", no_argument, NULL, OPTION_SPU_PLUGIN },
443 { "no-overlays", no_argument, NULL, OPTION_SPU_NO_OVERLAYS },
444 { "emit-stub-syms", no_argument, NULL, OPTION_SPU_STUB_SYMS },
445 { "extra-overlay-stubs", no_argument, NULL, OPTION_SPU_NON_OVERLAY_STUBS },
446 { "local-store", required_argument, NULL, OPTION_SPU_LOCAL_STORE },
447 { "stack-analysis", no_argument, NULL, OPTION_SPU_STACK_ANALYSIS },
448 { "emit-stack-syms", no_argument, NULL, OPTION_SPU_STACK_SYMS },
451 PARSE_AND_LIST_OPTIONS='
453 --plugin Make SPU plugin.\n\
454 --no-overlays No overlay handling.\n\
455 --emit-stub-syms Add symbols on overlay call stubs.\n\
456 --extra-overlay-stubs Add stubs on all calls out of overlay regions.\n\
457 --local-store=lo:hi Valid address range.\n\
458 --stack-analysis Estimate maximum stack requirement.\n\
459 --emit-stack-syms Add __stack_func giving stack needed for each func.\n"
463 PARSE_AND_LIST_ARGS_CASES='
464 case OPTION_SPU_PLUGIN:
468 case OPTION_SPU_NO_OVERLAYS:
472 case OPTION_SPU_STUB_SYMS:
476 case OPTION_SPU_NON_OVERLAY_STUBS:
477 non_overlay_stubs = 1;
480 case OPTION_SPU_LOCAL_STORE:
483 local_store_lo = strtoul (optarg, &end, 0);
484 if (*end == '\'':'\'')
486 local_store_hi = strtoul (end + 1, &end, 0);
490 einfo (_("%P%F: invalid --local-store address range `%s'\''\n"), optarg);
494 case OPTION_SPU_STACK_ANALYSIS:
498 case OPTION_SPU_STACK_SYMS:
503 LDEMUL_AFTER_OPEN=spu_after_open
504 LDEMUL_BEFORE_ALLOCATION=spu_before_allocation
505 LDEMUL_FINISH=gld${EMULATION_NAME}_finish