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 | # | |
92b93329 | 25 | fragment <<EOF |
e9f53129 AM |
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 | ||
92b93329 | 53 | fragment <<EOF |
671ace2f | 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 | ||
e9f53129 AM |
61 | |
62 | static int | |
63 | is_spu_target (void) | |
64 | { | |
65 | extern const bfd_target bfd_elf32_spu_vec; | |
66 | ||
67 | return link_info.hash->creator == &bfd_elf32_spu_vec; | |
68 | } | |
69 | ||
70 | /* Create our note section. */ | |
71 | ||
72 | static void | |
73 | spu_after_open (void) | |
74 | { | |
75 | if (is_spu_target () | |
76 | && !link_info.relocatable | |
77 | && link_info.input_bfds != NULL | |
49fa1e15 AM |
78 | && !spu_elf_create_sections (output_bfd, &link_info, |
79 | stack_analysis, emit_stack_syms)) | |
e9f53129 AM |
80 | einfo ("%X%P: can not create note section: %E\n"); |
81 | ||
82 | gld${EMULATION_NAME}_after_open (); | |
83 | } | |
84 | ||
47f6dab9 AM |
85 | /* If O is NULL, add section S at the end of output section OUTPUT_NAME. |
86 | If O is not NULL, add section S at the beginning of output section O. | |
e9f53129 AM |
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 | |
47f6dab9 | 97 | spu_place_special_section (asection *s, asection *o, const char *output_name) |
e9f53129 AM |
98 | { |
99 | lang_output_section_statement_type *os; | |
100 | ||
47f6dab9 | 101 | os = lang_output_section_find (o != NULL ? o->name : output_name); |
e9f53129 AM |
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 | } | |
47f6dab9 AM |
109 | else if (o != NULL && os->children.head != NULL) |
110 | { | |
111 | lang_statement_list_type add; | |
112 | ||
113 | lang_list_init (&add); | |
114 | lang_add_section (&add, s, os); | |
115 | *add.tail = os->children.head; | |
116 | os->children.head = add.head; | |
117 | } | |
e9f53129 AM |
118 | else |
119 | lang_add_section (&os->children, s, os); | |
120 | ||
121 | s->output_section->size += s->size; | |
122 | } | |
123 | ||
124 | /* Load built-in overlay manager, and tweak overlay section alignment. */ | |
125 | ||
126 | static void | |
127 | spu_elf_load_ovl_mgr (void) | |
128 | { | |
129 | lang_output_section_statement_type *os; | |
130 | struct elf_link_hash_entry *h; | |
131 | ||
132 | h = elf_link_hash_lookup (elf_hash_table (&link_info), | |
133 | "__ovly_load", FALSE, FALSE, FALSE); | |
134 | ||
135 | if (h != NULL | |
136 | && (h->root.type == bfd_link_hash_defined | |
137 | || h->root.type == bfd_link_hash_defweak) | |
138 | && h->def_regular) | |
139 | { | |
140 | /* User supplied __ovly_load. */ | |
141 | } | |
45d3b878 AM |
142 | else if (ovl_mgr_stream.start == ovl_mgr_stream.end) |
143 | einfo ("%F%P: no built-in overlay manager\n"); | |
e9f53129 AM |
144 | else |
145 | { | |
146 | lang_input_statement_type *ovl_is; | |
147 | ||
148 | ovl_is = lang_add_input_file ("builtin ovl_mgr", | |
149 | lang_input_file_is_file_enum, | |
150 | NULL); | |
151 | ||
152 | if (!spu_elf_open_builtin_lib (&ovl_is->the_bfd, &ovl_mgr_stream)) | |
153 | einfo ("%X%P: can not open built-in overlay manager: %E\n"); | |
154 | else | |
155 | { | |
156 | asection *in; | |
157 | ||
158 | if (!load_symbols (ovl_is, NULL)) | |
159 | einfo ("%X%P: can not load built-in overlay manager: %E\n"); | |
160 | ||
161 | /* Map overlay manager sections to output sections. */ | |
162 | for (in = ovl_is->the_bfd->sections; in != NULL; in = in->next) | |
163 | if ((in->flags & (SEC_ALLOC | SEC_LOAD)) | |
164 | == (SEC_ALLOC | SEC_LOAD)) | |
47f6dab9 | 165 | spu_place_special_section (in, NULL, ".text"); |
e9f53129 AM |
166 | } |
167 | } | |
168 | ||
169 | /* Ensure alignment of overlay sections is sufficient. */ | |
170 | for (os = &lang_output_section_statement.head->output_section_statement; | |
171 | os != NULL; | |
172 | os = os->next) | |
173 | if (os->bfd_section != NULL | |
174 | && spu_elf_section_data (os->bfd_section) != NULL | |
47f6dab9 | 175 | && spu_elf_section_data (os->bfd_section)->u.o.ovl_index != 0) |
e9f53129 AM |
176 | { |
177 | if (os->bfd_section->alignment_power < 4) | |
178 | os->bfd_section->alignment_power = 4; | |
179 | ||
180 | /* Also ensure size rounds up. */ | |
181 | os->block_value = 16; | |
182 | } | |
183 | } | |
184 | ||
185 | /* Go find if we need to do anything special for overlays. */ | |
186 | ||
187 | static void | |
188 | spu_before_allocation (void) | |
189 | { | |
190 | if (is_spu_target () | |
191 | && !link_info.relocatable | |
192 | && !no_overlays) | |
193 | { | |
194 | /* Size the sections. This is premature, but we need to know the | |
195 | rough layout so that overlays can be found. */ | |
196 | expld.phase = lang_mark_phase_enum; | |
197 | expld.dataseg.phase = exp_dataseg_none; | |
198 | one_lang_size_sections_pass (NULL, TRUE); | |
199 | ||
200 | /* Find overlays by inspecting section vmas. */ | |
201 | if (spu_elf_find_overlays (output_bfd, &link_info)) | |
202 | { | |
47f6dab9 | 203 | int ret; |
e9f53129 | 204 | |
47f6dab9 AM |
205 | ret = spu_elf_size_stubs (output_bfd, &link_info, |
206 | spu_place_special_section, | |
207 | non_overlay_stubs); | |
208 | if (ret == 0) | |
e9f53129 | 209 | einfo ("%X%P: can not size overlay stubs: %E\n"); |
47f6dab9 AM |
210 | else if (ret == 2) |
211 | spu_elf_load_ovl_mgr (); | |
e9f53129 AM |
212 | } |
213 | ||
214 | /* We must not cache anything from the preliminary sizing. */ | |
215 | lang_reset_memory_regions (); | |
216 | } | |
217 | ||
218 | gld${EMULATION_NAME}_before_allocation (); | |
219 | } | |
220 | ||
221 | /* Final emulation specific call. */ | |
222 | ||
223 | static void | |
224 | gld${EMULATION_NAME}_finish (void) | |
225 | { | |
226 | int need_laying_out; | |
227 | ||
228 | need_laying_out = bfd_elf_discard_info (output_bfd, &link_info); | |
229 | ||
230 | gld${EMULATION_NAME}_map_segments (need_laying_out); | |
231 | ||
232 | if (is_spu_target () && local_store_lo < local_store_hi) | |
233 | { | |
234 | asection *s; | |
235 | ||
236 | s = spu_elf_check_vma (output_bfd, local_store_lo, local_store_hi); | |
237 | if (s != NULL) | |
238 | einfo ("%X%P: %A exceeds local store range\n", s); | |
239 | } | |
240 | ||
47f6dab9 AM |
241 | if (!spu_elf_build_stubs (&link_info, |
242 | emit_stub_syms || link_info.emitrelocations)) | |
e9f53129 AM |
243 | einfo ("%X%P: can not build overlay stubs: %E\n"); |
244 | ||
245 | finish_default (); | |
246 | } | |
247 | ||
248 | EOF | |
249 | ||
dc27aea4 | 250 | if grep -q 'ld_elf.*ppc.*_emulation' ldemul-list.h; then |
92b93329 | 251 | fragment <<EOF |
dc27aea4 AM |
252 | #include "filenames.h" |
253 | #include <fcntl.h> | |
254 | #include <sys/wait.h> | |
255 | ||
256 | struct tflist { | |
257 | struct tflist *next; | |
258 | char name[9]; | |
259 | }; | |
260 | ||
261 | static struct tflist *tmp_file_list; | |
262 | ||
263 | static void clean_tmp (void) | |
264 | { | |
265 | for (; tmp_file_list != NULL; tmp_file_list = tmp_file_list->next) | |
266 | unlink (tmp_file_list->name); | |
267 | } | |
268 | ||
3ba20bde AM |
269 | static const char * |
270 | base_name (const char *path) | |
271 | { | |
272 | const char *file = strrchr (path, '/'); | |
273 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
274 | { | |
0fd1c9f1 | 275 | char *bslash = strrchr (path, '\\\\'); |
3ba20bde AM |
276 | |
277 | if (file == NULL || (bslash != NULL && bslash > file)) | |
278 | file = bslash; | |
279 | if (file == NULL | |
280 | && path[0] != '\0' | |
281 | && path[1] == ':') | |
282 | file = path + 1; | |
283 | } | |
284 | #endif | |
285 | if (file == NULL) | |
286 | file = path; | |
287 | else | |
288 | ++file; | |
289 | return file; | |
290 | } | |
291 | ||
dc27aea4 AM |
292 | /* This function is called when building a ppc32 or ppc64 executable |
293 | to handle embedded spu images. */ | |
294 | extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *); | |
295 | ||
296 | bfd_boolean | |
297 | embedded_spu_file (lang_input_statement_type *entry, const char *flags) | |
298 | { | |
299 | const char *cmd[6]; | |
300 | const char *sym; | |
301 | char *handle, *p; | |
302 | struct tflist *tf; | |
303 | char *oname; | |
304 | int fd; | |
305 | pid_t pid; | |
306 | int status; | |
307 | union lang_statement_union **old_stat_tail; | |
308 | union lang_statement_union **old_file_tail; | |
309 | union lang_statement_union *new_ent; | |
3ba20bde | 310 | lang_input_statement_type *search; |
dc27aea4 AM |
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. */ | |
3ba20bde | 319 | sym = base_name (entry->the_bfd->filename); |
dc27aea4 AM |
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 | ||
3ba20bde AM |
346 | for (search = (lang_input_statement_type *) input_file_chain.head; |
347 | search != NULL; | |
348 | search = (lang_input_statement_type *) search->next_real_file) | |
80c4ed32 AM |
349 | if (search->filename != NULL) |
350 | { | |
351 | const char *infile = base_name (search->filename); | |
352 | ||
353 | if (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 | } | |
3ba20bde | 362 | |
dc27aea4 AM |
363 | /* Use fork() and exec() rather than system() so that we don't |
364 | need to worry about quoting args. */ | |
42ba7415 | 365 | cmd[0] = EMBEDSPU; |
dc27aea4 AM |
366 | cmd[1] = flags; |
367 | cmd[2] = handle; | |
368 | cmd[3] = entry->the_bfd->filename; | |
369 | cmd[4] = oname; | |
370 | cmd[5] = NULL; | |
371 | if (trace_file_tries) | |
372 | { | |
373 | info_msg (_("running: %s \"%s\" \"%s\" \"%s\" \"%s\"\n"), | |
374 | cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]); | |
375 | fflush (stdout); | |
376 | } | |
377 | ||
378 | pid = fork (); | |
379 | if (pid == -1) | |
380 | return FALSE; | |
381 | if (pid == 0) | |
382 | { | |
383 | execvp (cmd[0], (char *const *) cmd); | |
42ba7415 | 384 | if (strcmp ("embedspu", EMBEDSPU) != 0) |
628e8859 AM |
385 | { |
386 | cmd[0] = "embedspu"; | |
387 | execvp (cmd[0], (char *const *) cmd); | |
388 | } | |
dc27aea4 AM |
389 | perror (cmd[0]); |
390 | _exit (127); | |
391 | } | |
392 | #ifdef HAVE_WAITPID | |
393 | #define WAITFOR(PID, STAT) waitpid (PID, STAT, 0) | |
394 | #else | |
395 | #define WAITFOR(PID, STAT) wait (STAT) | |
396 | #endif | |
397 | if (WAITFOR (pid, &status) != pid | |
398 | || !WIFEXITED (status) | |
399 | || WEXITSTATUS (status) != 0) | |
400 | return FALSE; | |
401 | #undef WAITFOR | |
402 | ||
403 | old_stat_tail = stat_ptr->tail; | |
404 | old_file_tail = input_file_chain.tail; | |
405 | if (lang_add_input_file (oname, lang_input_file_is_file_enum, NULL) == NULL) | |
406 | return FALSE; | |
407 | ||
408 | /* lang_add_input_file put the new list entry at the end of the statement | |
409 | and input file lists. Move it to just after the current entry. */ | |
410 | new_ent = *old_stat_tail; | |
411 | *old_stat_tail = NULL; | |
412 | stat_ptr->tail = old_stat_tail; | |
413 | *old_file_tail = NULL; | |
414 | input_file_chain.tail = old_file_tail; | |
415 | new_ent->header.next = entry->header.next; | |
416 | entry->header.next = new_ent; | |
417 | new_ent->input_statement.next_real_file = entry->next_real_file; | |
418 | entry->next_real_file = new_ent; | |
419 | ||
420 | /* Ensure bfd sections are excluded from the output. */ | |
421 | bfd_section_list_clear (entry->the_bfd); | |
422 | entry->loaded = TRUE; | |
423 | return TRUE; | |
424 | } | |
425 | ||
426 | EOF | |
427 | fi | |
428 | ||
e9f53129 AM |
429 | # Define some shell vars to insert bits of code into the standard elf |
430 | # parse_args and list_options functions. | |
431 | # | |
432 | PARSE_AND_LIST_PROLOGUE=' | |
433 | #define OPTION_SPU_PLUGIN 301 | |
434 | #define OPTION_SPU_NO_OVERLAYS (OPTION_SPU_PLUGIN + 1) | |
435 | #define OPTION_SPU_STUB_SYMS (OPTION_SPU_NO_OVERLAYS + 1) | |
436 | #define OPTION_SPU_NON_OVERLAY_STUBS (OPTION_SPU_STUB_SYMS + 1) | |
437 | #define OPTION_SPU_LOCAL_STORE (OPTION_SPU_NON_OVERLAY_STUBS + 1) | |
49fa1e15 AM |
438 | #define OPTION_SPU_STACK_ANALYSIS (OPTION_SPU_LOCAL_STORE + 1) |
439 | #define OPTION_SPU_STACK_SYMS (OPTION_SPU_STACK_ANALYSIS + 1) | |
e9f53129 AM |
440 | ' |
441 | ||
442 | PARSE_AND_LIST_LONGOPTS=' | |
443 | { "plugin", no_argument, NULL, OPTION_SPU_PLUGIN }, | |
444 | { "no-overlays", no_argument, NULL, OPTION_SPU_NO_OVERLAYS }, | |
445 | { "emit-stub-syms", no_argument, NULL, OPTION_SPU_STUB_SYMS }, | |
446 | { "extra-overlay-stubs", no_argument, NULL, OPTION_SPU_NON_OVERLAY_STUBS }, | |
447 | { "local-store", required_argument, NULL, OPTION_SPU_LOCAL_STORE }, | |
49fa1e15 AM |
448 | { "stack-analysis", no_argument, NULL, OPTION_SPU_STACK_ANALYSIS }, |
449 | { "emit-stack-syms", no_argument, NULL, OPTION_SPU_STACK_SYMS }, | |
e9f53129 AM |
450 | ' |
451 | ||
452 | PARSE_AND_LIST_OPTIONS=' | |
453 | fprintf (file, _("\ | |
442996ee AM |
454 | --plugin Make SPU plugin.\n\ |
455 | --no-overlays No overlay handling.\n\ | |
456 | --emit-stub-syms Add symbols on overlay call stubs.\n\ | |
457 | --extra-overlay-stubs Add stubs on all calls out of overlay regions.\n\ | |
458 | --local-store=lo:hi Valid address range.\n\ | |
459 | --stack-analysis Estimate maximum stack requirement.\n\ | |
460 | --emit-stack-syms Add sym giving stack needed for each func.\n" | |
e9f53129 AM |
461 | )); |
462 | ' | |
463 | ||
464 | PARSE_AND_LIST_ARGS_CASES=' | |
465 | case OPTION_SPU_PLUGIN: | |
466 | spu_elf_plugin (1); | |
467 | break; | |
468 | ||
469 | case OPTION_SPU_NO_OVERLAYS: | |
470 | no_overlays = 1; | |
471 | break; | |
472 | ||
473 | case OPTION_SPU_STUB_SYMS: | |
474 | emit_stub_syms = 1; | |
475 | break; | |
476 | ||
477 | case OPTION_SPU_NON_OVERLAY_STUBS: | |
478 | non_overlay_stubs = 1; | |
479 | break; | |
480 | ||
481 | case OPTION_SPU_LOCAL_STORE: | |
482 | { | |
483 | char *end; | |
484 | local_store_lo = strtoul (optarg, &end, 0); | |
485 | if (*end == '\'':'\'') | |
486 | { | |
487 | local_store_hi = strtoul (end + 1, &end, 0); | |
488 | if (*end == 0) | |
489 | break; | |
490 | } | |
491 | einfo (_("%P%F: invalid --local-store address range `%s'\''\n"), optarg); | |
492 | } | |
493 | break; | |
49fa1e15 AM |
494 | |
495 | case OPTION_SPU_STACK_ANALYSIS: | |
496 | stack_analysis = 1; | |
497 | break; | |
498 | ||
499 | case OPTION_SPU_STACK_SYMS: | |
500 | emit_stack_syms = 1; | |
501 | break; | |
e9f53129 AM |
502 | ' |
503 | ||
504 | LDEMUL_AFTER_OPEN=spu_after_open | |
505 | LDEMUL_BEFORE_ALLOCATION=spu_before_allocation | |
506 | LDEMUL_FINISH=gld${EMULATION_NAME}_finish |