Commit | Line | Data |
---|---|---|
e9f53129 | 1 | # This shell script emits a C file. -*- C -*- |
671ace2f | 2 | # Copyright 2006, 2007 Free Software Foundation, Inc. |
e9f53129 | 3 | # |
f96b4a7b | 4 | # This file is part of the GNU Binutils. |
e9f53129 AM |
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 | |
f96b4a7b | 8 | # the Free Software Foundation; either version 3 of the License, or |
e9f53129 AM |
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 | # | |
f96b4a7b NC |
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. | |
e9f53129 AM |
20 | # |
21 | ||
22 | # This file is sourced from elf32.em, and defines extra spu specific | |
23 | # features. | |
24 | # | |
25 | cat >>e${EMULATION_NAME}.c <<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 | ||
49fa1e15 AM |
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 | ||
e9f53129 AM |
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 | ||
671ace2f AM |
48 | static const char ovl_mgr[] = { |
49 | EOF | |
50 | ||
51 | ../binutils/bin2c < ${srcdir}/emultempl/spu_ovl.o >> e${EMULATION_NAME}.c | |
52 | ||
53 | cat >>e${EMULATION_NAME}.c <<EOF | |
54 | }; | |
e9f53129 AM |
55 | |
56 | static const struct _ovl_stream ovl_mgr_stream = { | |
671ace2f AM |
57 | ovl_mgr, |
58 | ovl_mgr + sizeof (ovl_mgr) | |
e9f53129 AM |
59 | }; |
60 | ||
61 | static asection *toe = NULL; | |
62 | ||
63 | ||
64 | static int | |
65 | is_spu_target (void) | |
66 | { | |
67 | extern const bfd_target bfd_elf32_spu_vec; | |
68 | ||
69 | return link_info.hash->creator == &bfd_elf32_spu_vec; | |
70 | } | |
71 | ||
72 | /* Create our note section. */ | |
73 | ||
74 | static void | |
75 | spu_after_open (void) | |
76 | { | |
77 | if (is_spu_target () | |
78 | && !link_info.relocatable | |
79 | && link_info.input_bfds != NULL | |
49fa1e15 AM |
80 | && !spu_elf_create_sections (output_bfd, &link_info, |
81 | stack_analysis, emit_stack_syms)) | |
e9f53129 AM |
82 | einfo ("%X%P: can not create note section: %E\n"); |
83 | ||
84 | gld${EMULATION_NAME}_after_open (); | |
85 | } | |
86 | ||
87 | /* Add section S at the end of output section OUTPUT_NAME. | |
88 | ||
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. */ | |
96 | ||
97 | static void | |
98 | spu_place_special_section (asection *s, const char *output_name) | |
99 | { | |
100 | lang_output_section_statement_type *os; | |
101 | ||
102 | os = lang_output_section_find (output_name); | |
103 | if (os == NULL) | |
104 | { | |
105 | const char *save = s->name; | |
106 | s->name = output_name; | |
107 | gld${EMULATION_NAME}_place_orphan (s); | |
108 | s->name = save; | |
109 | } | |
110 | else | |
111 | lang_add_section (&os->children, s, os); | |
112 | ||
113 | s->output_section->size += s->size; | |
114 | } | |
115 | ||
116 | /* Load built-in overlay manager, and tweak overlay section alignment. */ | |
117 | ||
118 | static void | |
119 | spu_elf_load_ovl_mgr (void) | |
120 | { | |
121 | lang_output_section_statement_type *os; | |
122 | struct elf_link_hash_entry *h; | |
123 | ||
124 | h = elf_link_hash_lookup (elf_hash_table (&link_info), | |
125 | "__ovly_load", FALSE, FALSE, FALSE); | |
126 | ||
127 | if (h != NULL | |
128 | && (h->root.type == bfd_link_hash_defined | |
129 | || h->root.type == bfd_link_hash_defweak) | |
130 | && h->def_regular) | |
131 | { | |
132 | /* User supplied __ovly_load. */ | |
133 | } | |
45d3b878 AM |
134 | else if (ovl_mgr_stream.start == ovl_mgr_stream.end) |
135 | einfo ("%F%P: no built-in overlay manager\n"); | |
e9f53129 AM |
136 | else |
137 | { | |
138 | lang_input_statement_type *ovl_is; | |
139 | ||
140 | ovl_is = lang_add_input_file ("builtin ovl_mgr", | |
141 | lang_input_file_is_file_enum, | |
142 | NULL); | |
143 | ||
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"); | |
146 | else | |
147 | { | |
148 | asection *in; | |
149 | ||
150 | if (!load_symbols (ovl_is, NULL)) | |
151 | einfo ("%X%P: can not load built-in overlay manager: %E\n"); | |
152 | ||
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"); | |
158 | } | |
159 | } | |
160 | ||
161 | /* Ensure alignment of overlay sections is sufficient. */ | |
162 | for (os = &lang_output_section_statement.head->output_section_statement; | |
163 | os != NULL; | |
164 | os = os->next) | |
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) | |
168 | { | |
169 | if (os->bfd_section->alignment_power < 4) | |
170 | os->bfd_section->alignment_power = 4; | |
171 | ||
172 | /* Also ensure size rounds up. */ | |
173 | os->block_value = 16; | |
174 | } | |
175 | } | |
176 | ||
177 | /* Go find if we need to do anything special for overlays. */ | |
178 | ||
179 | static void | |
180 | spu_before_allocation (void) | |
181 | { | |
182 | if (is_spu_target () | |
183 | && !link_info.relocatable | |
184 | && !no_overlays) | |
185 | { | |
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); | |
191 | ||
192 | /* Find overlays by inspecting section vmas. */ | |
193 | if (spu_elf_find_overlays (output_bfd, &link_info)) | |
194 | { | |
195 | asection *stub, *ovtab; | |
196 | ||
197 | if (!spu_elf_size_stubs (output_bfd, &link_info, non_overlay_stubs, | |
49fa1e15 | 198 | stack_analysis, &stub, &ovtab, &toe)) |
e9f53129 AM |
199 | einfo ("%X%P: can not size overlay stubs: %E\n"); |
200 | ||
201 | if (stub != NULL) | |
202 | { | |
203 | spu_place_special_section (stub, ".text"); | |
204 | spu_place_special_section (ovtab, ".data"); | |
205 | spu_place_special_section (toe, ".toe"); | |
206 | ||
207 | spu_elf_load_ovl_mgr (); | |
208 | } | |
209 | } | |
210 | ||
211 | /* We must not cache anything from the preliminary sizing. */ | |
212 | lang_reset_memory_regions (); | |
213 | } | |
214 | ||
215 | gld${EMULATION_NAME}_before_allocation (); | |
216 | } | |
217 | ||
218 | /* Final emulation specific call. */ | |
219 | ||
220 | static void | |
221 | gld${EMULATION_NAME}_finish (void) | |
222 | { | |
223 | int need_laying_out; | |
224 | ||
225 | need_laying_out = bfd_elf_discard_info (output_bfd, &link_info); | |
226 | ||
227 | gld${EMULATION_NAME}_map_segments (need_laying_out); | |
228 | ||
229 | if (is_spu_target () && local_store_lo < local_store_hi) | |
230 | { | |
231 | asection *s; | |
232 | ||
233 | s = spu_elf_check_vma (output_bfd, local_store_lo, local_store_hi); | |
234 | if (s != NULL) | |
235 | einfo ("%X%P: %A exceeds local store range\n", s); | |
236 | } | |
237 | ||
238 | if (toe != NULL | |
239 | && !spu_elf_build_stubs (&link_info, | |
240 | emit_stub_syms || link_info.emitrelocations, | |
241 | toe)) | |
242 | einfo ("%X%P: can not build overlay stubs: %E\n"); | |
243 | ||
244 | finish_default (); | |
245 | } | |
246 | ||
247 | EOF | |
248 | ||
dc27aea4 AM |
249 | if grep -q 'ld_elf.*ppc.*_emulation' ldemul-list.h; then |
250 | cat >>e${EMULATION_NAME}.c <<EOF | |
251 | #include "filenames.h" | |
252 | #include <fcntl.h> | |
253 | #include <sys/wait.h> | |
254 | ||
255 | struct tflist { | |
256 | struct tflist *next; | |
257 | char name[9]; | |
258 | }; | |
259 | ||
260 | static struct tflist *tmp_file_list; | |
261 | ||
262 | static void clean_tmp (void) | |
263 | { | |
264 | for (; tmp_file_list != NULL; tmp_file_list = tmp_file_list->next) | |
265 | unlink (tmp_file_list->name); | |
266 | } | |
267 | ||
3ba20bde AM |
268 | static const char * |
269 | base_name (const char *path) | |
270 | { | |
271 | const char *file = strrchr (path, '/'); | |
272 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
273 | { | |
0fd1c9f1 | 274 | char *bslash = strrchr (path, '\\\\'); |
3ba20bde AM |
275 | |
276 | if (file == NULL || (bslash != NULL && bslash > file)) | |
277 | file = bslash; | |
278 | if (file == NULL | |
279 | && path[0] != '\0' | |
280 | && path[1] == ':') | |
281 | file = path + 1; | |
282 | } | |
283 | #endif | |
284 | if (file == NULL) | |
285 | file = path; | |
286 | else | |
287 | ++file; | |
288 | return file; | |
289 | } | |
290 | ||
dc27aea4 AM |
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 *); | |
294 | ||
295 | bfd_boolean | |
296 | embedded_spu_file (lang_input_statement_type *entry, const char *flags) | |
297 | { | |
298 | const char *cmd[6]; | |
299 | const char *sym; | |
300 | char *handle, *p; | |
301 | struct tflist *tf; | |
302 | char *oname; | |
303 | int fd; | |
304 | pid_t pid; | |
305 | int status; | |
306 | union lang_statement_union **old_stat_tail; | |
307 | union lang_statement_union **old_file_tail; | |
308 | union lang_statement_union *new_ent; | |
3ba20bde | 309 | lang_input_statement_type *search; |
dc27aea4 AM |
310 | |
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)) | |
315 | return FALSE; | |
316 | ||
317 | /* Use the filename as the symbol marking the program handle struct. */ | |
3ba20bde | 318 | sym = base_name (entry->the_bfd->filename); |
dc27aea4 AM |
319 | |
320 | handle = xstrdup (sym); | |
321 | for (p = handle; *p; ++p) | |
322 | if (!(ISALNUM (*p) || *p == '$' || *p == '.')) | |
323 | *p = '_'; | |
324 | ||
325 | if (tmp_file_list == NULL) | |
326 | atexit (clean_tmp); | |
327 | tf = xmalloc (sizeof (*tf)); | |
328 | tf->next = tmp_file_list; | |
329 | tmp_file_list = tf; | |
330 | oname = tf->name; | |
331 | memcpy (tf->name, "ldXXXXXX", sizeof (tf->name)); | |
332 | ||
333 | #ifdef HAVE_MKSTEMP | |
334 | fd = mkstemp (oname); | |
335 | #else | |
336 | oname = mktemp (oname); | |
337 | if (oname == NULL) | |
338 | return FALSE; | |
339 | fd = open (oname, O_RDWR | O_CREAT | O_EXCL, 0600); | |
340 | #endif | |
341 | if (fd == -1) | |
342 | return FALSE; | |
343 | close (fd); | |
344 | ||
3ba20bde AM |
345 | for (search = (lang_input_statement_type *) input_file_chain.head; |
346 | search != NULL; | |
347 | search = (lang_input_statement_type *) search->next_real_file) | |
348 | { | |
349 | const char *infile = base_name (search->filename); | |
350 | ||
351 | if (infile != NULL | |
352 | && strncmp (infile, "crtbegin", 8) == 0) | |
353 | { | |
354 | if (infile[8] == 'S') | |
355 | flags = concat (flags, " -fPIC", NULL); | |
356 | else if (infile[8] == 'T') | |
357 | flags = concat (flags, " -fpie", NULL); | |
358 | break; | |
359 | } | |
360 | } | |
361 | ||
dc27aea4 AM |
362 | /* Use fork() and exec() rather than system() so that we don't |
363 | need to worry about quoting args. */ | |
42ba7415 | 364 | cmd[0] = EMBEDSPU; |
dc27aea4 AM |
365 | cmd[1] = flags; |
366 | cmd[2] = handle; | |
367 | cmd[3] = entry->the_bfd->filename; | |
368 | cmd[4] = oname; | |
369 | cmd[5] = NULL; | |
370 | if (trace_file_tries) | |
371 | { | |
372 | info_msg (_("running: %s \"%s\" \"%s\" \"%s\" \"%s\"\n"), | |
373 | cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]); | |
374 | fflush (stdout); | |
375 | } | |
376 | ||
377 | pid = fork (); | |
378 | if (pid == -1) | |
379 | return FALSE; | |
380 | if (pid == 0) | |
381 | { | |
382 | execvp (cmd[0], (char *const *) cmd); | |
42ba7415 | 383 | if (strcmp ("embedspu", EMBEDSPU) != 0) |
628e8859 AM |
384 | { |
385 | cmd[0] = "embedspu"; | |
386 | execvp (cmd[0], (char *const *) cmd); | |
387 | } | |
dc27aea4 AM |
388 | perror (cmd[0]); |
389 | _exit (127); | |
390 | } | |
391 | #ifdef HAVE_WAITPID | |
392 | #define WAITFOR(PID, STAT) waitpid (PID, STAT, 0) | |
393 | #else | |
394 | #define WAITFOR(PID, STAT) wait (STAT) | |
395 | #endif | |
396 | if (WAITFOR (pid, &status) != pid | |
397 | || !WIFEXITED (status) | |
398 | || WEXITSTATUS (status) != 0) | |
399 | return FALSE; | |
400 | #undef WAITFOR | |
401 | ||
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) | |
405 | return FALSE; | |
406 | ||
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; | |
418 | ||
419 | /* Ensure bfd sections are excluded from the output. */ | |
420 | bfd_section_list_clear (entry->the_bfd); | |
421 | entry->loaded = TRUE; | |
422 | return TRUE; | |
423 | } | |
424 | ||
425 | EOF | |
426 | fi | |
427 | ||
e9f53129 AM |
428 | # Define some shell vars to insert bits of code into the standard elf |
429 | # parse_args and list_options functions. | |
430 | # | |
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) | |
49fa1e15 AM |
437 | #define OPTION_SPU_STACK_ANALYSIS (OPTION_SPU_LOCAL_STORE + 1) |
438 | #define OPTION_SPU_STACK_SYMS (OPTION_SPU_STACK_ANALYSIS + 1) | |
e9f53129 AM |
439 | ' |
440 | ||
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 }, | |
49fa1e15 AM |
447 | { "stack-analysis", no_argument, NULL, OPTION_SPU_STACK_ANALYSIS }, |
448 | { "emit-stack-syms", no_argument, NULL, OPTION_SPU_STACK_SYMS }, | |
e9f53129 AM |
449 | ' |
450 | ||
451 | PARSE_AND_LIST_OPTIONS=' | |
452 | fprintf (file, _("\ | |
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\ | |
49fa1e15 AM |
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" | |
e9f53129 AM |
460 | )); |
461 | ' | |
462 | ||
463 | PARSE_AND_LIST_ARGS_CASES=' | |
464 | case OPTION_SPU_PLUGIN: | |
465 | spu_elf_plugin (1); | |
466 | break; | |
467 | ||
468 | case OPTION_SPU_NO_OVERLAYS: | |
469 | no_overlays = 1; | |
470 | break; | |
471 | ||
472 | case OPTION_SPU_STUB_SYMS: | |
473 | emit_stub_syms = 1; | |
474 | break; | |
475 | ||
476 | case OPTION_SPU_NON_OVERLAY_STUBS: | |
477 | non_overlay_stubs = 1; | |
478 | break; | |
479 | ||
480 | case OPTION_SPU_LOCAL_STORE: | |
481 | { | |
482 | char *end; | |
483 | local_store_lo = strtoul (optarg, &end, 0); | |
484 | if (*end == '\'':'\'') | |
485 | { | |
486 | local_store_hi = strtoul (end + 1, &end, 0); | |
487 | if (*end == 0) | |
488 | break; | |
489 | } | |
490 | einfo (_("%P%F: invalid --local-store address range `%s'\''\n"), optarg); | |
491 | } | |
492 | break; | |
49fa1e15 AM |
493 | |
494 | case OPTION_SPU_STACK_ANALYSIS: | |
495 | stack_analysis = 1; | |
496 | break; | |
497 | ||
498 | case OPTION_SPU_STACK_SYMS: | |
499 | emit_stack_syms = 1; | |
500 | break; | |
e9f53129 AM |
501 | ' |
502 | ||
503 | LDEMUL_AFTER_OPEN=spu_after_open | |
504 | LDEMUL_BEFORE_ALLOCATION=spu_before_allocation | |
505 | LDEMUL_FINISH=gld${EMULATION_NAME}_finish |