Commit | Line | Data |
---|---|---|
e9f53129 | 1 | # This shell script emits a C file. -*- C -*- |
671ace2f | 2 | # Copyright 2006, 2007 Free Software Foundation, Inc. |
e9f53129 AM |
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 | ||
49fa1e15 AM |
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 | ||
e9f53129 AM |
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 | ||
671ace2f AM |
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 | }; | |
e9f53129 AM |
54 | |
55 | static const struct _ovl_stream ovl_mgr_stream = { | |
671ace2f AM |
56 | ovl_mgr, |
57 | ovl_mgr + sizeof (ovl_mgr) | |
e9f53129 AM |
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 | |
49fa1e15 AM |
79 | && !spu_elf_create_sections (output_bfd, &link_info, |
80 | stack_analysis, emit_stack_syms)) | |
e9f53129 AM |
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 | } | |
45d3b878 AM |
133 | else if (ovl_mgr_stream.start == ovl_mgr_stream.end) |
134 | einfo ("%F%P: no built-in overlay manager\n"); | |
e9f53129 AM |
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, | |
49fa1e15 | 197 | stack_analysis, &stub, &ovtab, &toe)) |
e9f53129 AM |
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 | ||
dc27aea4 AM |
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 | /* This function is called when building a ppc32 or ppc64 executable | |
268 | to handle embedded spu images. */ | |
269 | extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *); | |
270 | ||
271 | bfd_boolean | |
272 | embedded_spu_file (lang_input_statement_type *entry, const char *flags) | |
273 | { | |
274 | const char *cmd[6]; | |
275 | const char *sym; | |
276 | char *handle, *p; | |
277 | struct tflist *tf; | |
278 | char *oname; | |
279 | int fd; | |
280 | pid_t pid; | |
281 | int status; | |
282 | union lang_statement_union **old_stat_tail; | |
283 | union lang_statement_union **old_file_tail; | |
284 | union lang_statement_union *new_ent; | |
285 | ||
286 | if (entry->the_bfd->format != bfd_object | |
287 | || strcmp (entry->the_bfd->xvec->name, "elf32-spu") != 0 | |
288 | || (entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_EXEC | |
289 | && entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_DYN)) | |
290 | return FALSE; | |
291 | ||
292 | /* Use the filename as the symbol marking the program handle struct. */ | |
293 | sym = strrchr (entry->the_bfd->filename, '/'); | |
294 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
295 | { | |
296 | char *bslash = strrchr (entry->the_bfd->filename, '\\'); | |
297 | ||
298 | if (sym == NULL || (bslash != NULL && bslash > sym)) | |
299 | sym = bslash; | |
300 | if (sym == NULL | |
301 | && entry->the_bfd->filename[0] != '\0' | |
302 | && entry->the_bfd->filename[1] == ':') | |
303 | sym = entry->the_bfd->filename + 1; | |
304 | } | |
305 | #endif | |
306 | if (sym == NULL) | |
307 | sym = entry->the_bfd->filename; | |
308 | else | |
309 | ++sym; | |
310 | ||
311 | handle = xstrdup (sym); | |
312 | for (p = handle; *p; ++p) | |
313 | if (!(ISALNUM (*p) || *p == '$' || *p == '.')) | |
314 | *p = '_'; | |
315 | ||
316 | if (tmp_file_list == NULL) | |
317 | atexit (clean_tmp); | |
318 | tf = xmalloc (sizeof (*tf)); | |
319 | tf->next = tmp_file_list; | |
320 | tmp_file_list = tf; | |
321 | oname = tf->name; | |
322 | memcpy (tf->name, "ldXXXXXX", sizeof (tf->name)); | |
323 | ||
324 | #ifdef HAVE_MKSTEMP | |
325 | fd = mkstemp (oname); | |
326 | #else | |
327 | oname = mktemp (oname); | |
328 | if (oname == NULL) | |
329 | return FALSE; | |
330 | fd = open (oname, O_RDWR | O_CREAT | O_EXCL, 0600); | |
331 | #endif | |
332 | if (fd == -1) | |
333 | return FALSE; | |
334 | close (fd); | |
335 | ||
336 | /* Use fork() and exec() rather than system() so that we don't | |
337 | need to worry about quoting args. */ | |
338 | cmd[0] = "embedspu"; | |
339 | cmd[1] = flags; | |
340 | cmd[2] = handle; | |
341 | cmd[3] = entry->the_bfd->filename; | |
342 | cmd[4] = oname; | |
343 | cmd[5] = NULL; | |
344 | if (trace_file_tries) | |
345 | { | |
346 | info_msg (_("running: %s \"%s\" \"%s\" \"%s\" \"%s\"\n"), | |
347 | cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]); | |
348 | fflush (stdout); | |
349 | } | |
350 | ||
351 | pid = fork (); | |
352 | if (pid == -1) | |
353 | return FALSE; | |
354 | if (pid == 0) | |
355 | { | |
356 | execvp (cmd[0], (char *const *) cmd); | |
357 | perror (cmd[0]); | |
358 | _exit (127); | |
359 | } | |
360 | #ifdef HAVE_WAITPID | |
361 | #define WAITFOR(PID, STAT) waitpid (PID, STAT, 0) | |
362 | #else | |
363 | #define WAITFOR(PID, STAT) wait (STAT) | |
364 | #endif | |
365 | if (WAITFOR (pid, &status) != pid | |
366 | || !WIFEXITED (status) | |
367 | || WEXITSTATUS (status) != 0) | |
368 | return FALSE; | |
369 | #undef WAITFOR | |
370 | ||
371 | old_stat_tail = stat_ptr->tail; | |
372 | old_file_tail = input_file_chain.tail; | |
373 | if (lang_add_input_file (oname, lang_input_file_is_file_enum, NULL) == NULL) | |
374 | return FALSE; | |
375 | ||
376 | /* lang_add_input_file put the new list entry at the end of the statement | |
377 | and input file lists. Move it to just after the current entry. */ | |
378 | new_ent = *old_stat_tail; | |
379 | *old_stat_tail = NULL; | |
380 | stat_ptr->tail = old_stat_tail; | |
381 | *old_file_tail = NULL; | |
382 | input_file_chain.tail = old_file_tail; | |
383 | new_ent->header.next = entry->header.next; | |
384 | entry->header.next = new_ent; | |
385 | new_ent->input_statement.next_real_file = entry->next_real_file; | |
386 | entry->next_real_file = new_ent; | |
387 | ||
388 | /* Ensure bfd sections are excluded from the output. */ | |
389 | bfd_section_list_clear (entry->the_bfd); | |
390 | entry->loaded = TRUE; | |
391 | return TRUE; | |
392 | } | |
393 | ||
394 | EOF | |
395 | fi | |
396 | ||
e9f53129 AM |
397 | # Define some shell vars to insert bits of code into the standard elf |
398 | # parse_args and list_options functions. | |
399 | # | |
400 | PARSE_AND_LIST_PROLOGUE=' | |
401 | #define OPTION_SPU_PLUGIN 301 | |
402 | #define OPTION_SPU_NO_OVERLAYS (OPTION_SPU_PLUGIN + 1) | |
403 | #define OPTION_SPU_STUB_SYMS (OPTION_SPU_NO_OVERLAYS + 1) | |
404 | #define OPTION_SPU_NON_OVERLAY_STUBS (OPTION_SPU_STUB_SYMS + 1) | |
405 | #define OPTION_SPU_LOCAL_STORE (OPTION_SPU_NON_OVERLAY_STUBS + 1) | |
49fa1e15 AM |
406 | #define OPTION_SPU_STACK_ANALYSIS (OPTION_SPU_LOCAL_STORE + 1) |
407 | #define OPTION_SPU_STACK_SYMS (OPTION_SPU_STACK_ANALYSIS + 1) | |
e9f53129 AM |
408 | ' |
409 | ||
410 | PARSE_AND_LIST_LONGOPTS=' | |
411 | { "plugin", no_argument, NULL, OPTION_SPU_PLUGIN }, | |
412 | { "no-overlays", no_argument, NULL, OPTION_SPU_NO_OVERLAYS }, | |
413 | { "emit-stub-syms", no_argument, NULL, OPTION_SPU_STUB_SYMS }, | |
414 | { "extra-overlay-stubs", no_argument, NULL, OPTION_SPU_NON_OVERLAY_STUBS }, | |
415 | { "local-store", required_argument, NULL, OPTION_SPU_LOCAL_STORE }, | |
49fa1e15 AM |
416 | { "stack-analysis", no_argument, NULL, OPTION_SPU_STACK_ANALYSIS }, |
417 | { "emit-stack-syms", no_argument, NULL, OPTION_SPU_STACK_SYMS }, | |
e9f53129 AM |
418 | ' |
419 | ||
420 | PARSE_AND_LIST_OPTIONS=' | |
421 | fprintf (file, _("\ | |
422 | --plugin Make SPU plugin.\n\ | |
423 | --no-overlays No overlay handling.\n\ | |
424 | --emit-stub-syms Add symbols on overlay call stubs.\n\ | |
425 | --extra-overlay-stubs Add stubs on all calls out of overlay regions.\n\ | |
49fa1e15 AM |
426 | --local-store=lo:hi Valid address range.\n\ |
427 | --stack-analysis Estimate maximum stack requirement.\n\ | |
428 | --emit-stack-syms Add __stack_func giving stack needed for each func.\n" | |
e9f53129 AM |
429 | )); |
430 | ' | |
431 | ||
432 | PARSE_AND_LIST_ARGS_CASES=' | |
433 | case OPTION_SPU_PLUGIN: | |
434 | spu_elf_plugin (1); | |
435 | break; | |
436 | ||
437 | case OPTION_SPU_NO_OVERLAYS: | |
438 | no_overlays = 1; | |
439 | break; | |
440 | ||
441 | case OPTION_SPU_STUB_SYMS: | |
442 | emit_stub_syms = 1; | |
443 | break; | |
444 | ||
445 | case OPTION_SPU_NON_OVERLAY_STUBS: | |
446 | non_overlay_stubs = 1; | |
447 | break; | |
448 | ||
449 | case OPTION_SPU_LOCAL_STORE: | |
450 | { | |
451 | char *end; | |
452 | local_store_lo = strtoul (optarg, &end, 0); | |
453 | if (*end == '\'':'\'') | |
454 | { | |
455 | local_store_hi = strtoul (end + 1, &end, 0); | |
456 | if (*end == 0) | |
457 | break; | |
458 | } | |
459 | einfo (_("%P%F: invalid --local-store address range `%s'\''\n"), optarg); | |
460 | } | |
461 | break; | |
49fa1e15 AM |
462 | |
463 | case OPTION_SPU_STACK_ANALYSIS: | |
464 | stack_analysis = 1; | |
465 | break; | |
466 | ||
467 | case OPTION_SPU_STACK_SYMS: | |
468 | emit_stack_syms = 1; | |
469 | break; | |
e9f53129 AM |
470 | ' |
471 | ||
472 | LDEMUL_AFTER_OPEN=spu_after_open | |
473 | LDEMUL_BEFORE_ALLOCATION=spu_before_allocation | |
474 | LDEMUL_FINISH=gld${EMULATION_NAME}_finish |