*** empty log message ***
[deliverable/binutils-gdb.git] / ld / emultempl / spuelf.em
CommitLineData
e9f53129 1# This shell script emits a C file. -*- C -*-
ff7a0acf 2# Copyright 2006, 2007, 2008 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 25fragment <<EOF
e9f53129
AM
26#include "ldctor.h"
27#include "elf32-spu.h"
28
29/* Non-zero if no overlay processing should be done. */
30static int no_overlays = 0;
31
32/* Non-zero if we want stubs on all calls out of overlay regions. */
33static int non_overlay_stubs = 0;
34
35/* Whether to emit symbols for stubs. */
36static int emit_stub_syms = 0;
37
49fa1e15
AM
38/* Non-zero to perform stack space analysis. */
39static int stack_analysis = 0;
40
41/* Whether to emit symbols with stack requirements for each function. */
42static int emit_stack_syms = 0;
43
e9f53129
AM
44/* Range of valid addresses for loadable sections. */
45static bfd_vma local_store_lo = 0;
46static bfd_vma local_store_hi = 0x3ffff;
47
671ace2f
AM
48static const char ovl_mgr[] = {
49EOF
50
51../binutils/bin2c < ${srcdir}/emultempl/spu_ovl.o >> e${EMULATION_NAME}.c
52
92b93329 53fragment <<EOF
671ace2f 54};
e9f53129
AM
55
56static 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
62static int
63is_spu_target (void)
64{
65 extern const bfd_target bfd_elf32_spu_vec;
66
f13a99db 67 return link_info.output_bfd->xvec == &bfd_elf32_spu_vec;
e9f53129
AM
68}
69
70/* Create our note section. */
71
72static void
73spu_after_open (void)
74{
75 if (is_spu_target ()
76 && !link_info.relocatable
77 && link_info.input_bfds != NULL
c65be8d7 78 && !spu_elf_create_sections (&link_info,
49fa1e15 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
96static void
47f6dab9 97spu_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
126static void
127spu_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
187static void
188spu_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. */
c65be8d7 201 if (spu_elf_find_overlays (&link_info))
e9f53129 202 {
47f6dab9 203 int ret;
e9f53129 204
c65be8d7 205 ret = spu_elf_size_stubs (&link_info,
47f6dab9
AM
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
223static void
224gld${EMULATION_NAME}_finish (void)
225{
226 int need_laying_out;
227
f13a99db 228 need_laying_out = bfd_elf_discard_info (link_info.output_bfd, &link_info);
e9f53129
AM
229
230 gld${EMULATION_NAME}_map_segments (need_laying_out);
231
771a9c5f 232 if (is_spu_target ())
e9f53129 233 {
771a9c5f
AM
234 if (local_store_lo < local_store_hi)
235 {
236 asection *s;
237
c65be8d7 238 s = spu_elf_check_vma (&link_info, local_store_lo, local_store_hi);
771a9c5f
AM
239 if (s != NULL)
240 einfo ("%X%P: %A exceeds local store range\n", s);
241 }
e9f53129 242
771a9c5f
AM
243 if (!spu_elf_build_stubs (&link_info,
244 emit_stub_syms || link_info.emitrelocations))
245 einfo ("%X%P: can not build overlay stubs: %E\n");
e9f53129
AM
246 }
247
e9f53129
AM
248 finish_default ();
249}
250
251EOF
252
dc27aea4 253if grep -q 'ld_elf.*ppc.*_emulation' ldemul-list.h; then
92b93329 254 fragment <<EOF
dc27aea4
AM
255#include "filenames.h"
256#include <fcntl.h>
257#include <sys/wait.h>
258
259struct tflist {
260 struct tflist *next;
261 char name[9];
262};
263
264static struct tflist *tmp_file_list;
265
266static void clean_tmp (void)
267{
268 for (; tmp_file_list != NULL; tmp_file_list = tmp_file_list->next)
269 unlink (tmp_file_list->name);
270}
271
3ba20bde
AM
272static const char *
273base_name (const char *path)
274{
275 const char *file = strrchr (path, '/');
276#ifdef HAVE_DOS_BASED_FILE_SYSTEM
277 {
0fd1c9f1 278 char *bslash = strrchr (path, '\\\\');
3ba20bde
AM
279
280 if (file == NULL || (bslash != NULL && bslash > file))
281 file = bslash;
282 if (file == NULL
283 && path[0] != '\0'
284 && path[1] == ':')
285 file = path + 1;
286 }
287#endif
288 if (file == NULL)
289 file = path;
290 else
291 ++file;
292 return file;
293}
294
dc27aea4
AM
295/* This function is called when building a ppc32 or ppc64 executable
296 to handle embedded spu images. */
297extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
298
299bfd_boolean
300embedded_spu_file (lang_input_statement_type *entry, const char *flags)
301{
302 const char *cmd[6];
303 const char *sym;
304 char *handle, *p;
305 struct tflist *tf;
306 char *oname;
307 int fd;
308 pid_t pid;
309 int status;
310 union lang_statement_union **old_stat_tail;
311 union lang_statement_union **old_file_tail;
312 union lang_statement_union *new_ent;
3ba20bde 313 lang_input_statement_type *search;
dc27aea4
AM
314
315 if (entry->the_bfd->format != bfd_object
316 || strcmp (entry->the_bfd->xvec->name, "elf32-spu") != 0
317 || (entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_EXEC
318 && entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_DYN))
319 return FALSE;
320
321 /* Use the filename as the symbol marking the program handle struct. */
3ba20bde 322 sym = base_name (entry->the_bfd->filename);
dc27aea4
AM
323
324 handle = xstrdup (sym);
325 for (p = handle; *p; ++p)
326 if (!(ISALNUM (*p) || *p == '$' || *p == '.'))
327 *p = '_';
328
329 if (tmp_file_list == NULL)
330 atexit (clean_tmp);
331 tf = xmalloc (sizeof (*tf));
332 tf->next = tmp_file_list;
333 tmp_file_list = tf;
334 oname = tf->name;
335 memcpy (tf->name, "ldXXXXXX", sizeof (tf->name));
336
337#ifdef HAVE_MKSTEMP
338 fd = mkstemp (oname);
339#else
340 oname = mktemp (oname);
341 if (oname == NULL)
342 return FALSE;
343 fd = open (oname, O_RDWR | O_CREAT | O_EXCL, 0600);
344#endif
345 if (fd == -1)
346 return FALSE;
347 close (fd);
348
3ba20bde
AM
349 for (search = (lang_input_statement_type *) input_file_chain.head;
350 search != NULL;
351 search = (lang_input_statement_type *) search->next_real_file)
80c4ed32
AM
352 if (search->filename != NULL)
353 {
354 const char *infile = base_name (search->filename);
355
356 if (strncmp (infile, "crtbegin", 8) == 0)
357 {
358 if (infile[8] == 'S')
ff7a0acf 359 flags = concat (flags, " -fPIC", (const char *) NULL);
80c4ed32 360 else if (infile[8] == 'T')
ff7a0acf 361 flags = concat (flags, " -fpie", (const char *) NULL);
80c4ed32
AM
362 break;
363 }
364 }
3ba20bde 365
dc27aea4
AM
366 /* Use fork() and exec() rather than system() so that we don't
367 need to worry about quoting args. */
42ba7415 368 cmd[0] = EMBEDSPU;
dc27aea4
AM
369 cmd[1] = flags;
370 cmd[2] = handle;
371 cmd[3] = entry->the_bfd->filename;
372 cmd[4] = oname;
373 cmd[5] = NULL;
374 if (trace_file_tries)
375 {
376 info_msg (_("running: %s \"%s\" \"%s\" \"%s\" \"%s\"\n"),
377 cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]);
378 fflush (stdout);
379 }
380
381 pid = fork ();
382 if (pid == -1)
383 return FALSE;
384 if (pid == 0)
385 {
386 execvp (cmd[0], (char *const *) cmd);
42ba7415 387 if (strcmp ("embedspu", EMBEDSPU) != 0)
628e8859
AM
388 {
389 cmd[0] = "embedspu";
390 execvp (cmd[0], (char *const *) cmd);
391 }
dc27aea4
AM
392 perror (cmd[0]);
393 _exit (127);
394 }
395#ifdef HAVE_WAITPID
396#define WAITFOR(PID, STAT) waitpid (PID, STAT, 0)
397#else
398#define WAITFOR(PID, STAT) wait (STAT)
399#endif
400 if (WAITFOR (pid, &status) != pid
401 || !WIFEXITED (status)
402 || WEXITSTATUS (status) != 0)
403 return FALSE;
404#undef WAITFOR
405
406 old_stat_tail = stat_ptr->tail;
407 old_file_tail = input_file_chain.tail;
408 if (lang_add_input_file (oname, lang_input_file_is_file_enum, NULL) == NULL)
409 return FALSE;
410
411 /* lang_add_input_file put the new list entry at the end of the statement
412 and input file lists. Move it to just after the current entry. */
413 new_ent = *old_stat_tail;
414 *old_stat_tail = NULL;
415 stat_ptr->tail = old_stat_tail;
416 *old_file_tail = NULL;
417 input_file_chain.tail = old_file_tail;
418 new_ent->header.next = entry->header.next;
419 entry->header.next = new_ent;
420 new_ent->input_statement.next_real_file = entry->next_real_file;
421 entry->next_real_file = new_ent;
422
423 /* Ensure bfd sections are excluded from the output. */
424 bfd_section_list_clear (entry->the_bfd);
425 entry->loaded = TRUE;
426 return TRUE;
427}
428
429EOF
430fi
431
e9f53129
AM
432# Define some shell vars to insert bits of code into the standard elf
433# parse_args and list_options functions.
434#
435PARSE_AND_LIST_PROLOGUE='
436#define OPTION_SPU_PLUGIN 301
437#define OPTION_SPU_NO_OVERLAYS (OPTION_SPU_PLUGIN + 1)
438#define OPTION_SPU_STUB_SYMS (OPTION_SPU_NO_OVERLAYS + 1)
439#define OPTION_SPU_NON_OVERLAY_STUBS (OPTION_SPU_STUB_SYMS + 1)
440#define OPTION_SPU_LOCAL_STORE (OPTION_SPU_NON_OVERLAY_STUBS + 1)
49fa1e15
AM
441#define OPTION_SPU_STACK_ANALYSIS (OPTION_SPU_LOCAL_STORE + 1)
442#define OPTION_SPU_STACK_SYMS (OPTION_SPU_STACK_ANALYSIS + 1)
e9f53129
AM
443'
444
445PARSE_AND_LIST_LONGOPTS='
446 { "plugin", no_argument, NULL, OPTION_SPU_PLUGIN },
447 { "no-overlays", no_argument, NULL, OPTION_SPU_NO_OVERLAYS },
448 { "emit-stub-syms", no_argument, NULL, OPTION_SPU_STUB_SYMS },
449 { "extra-overlay-stubs", no_argument, NULL, OPTION_SPU_NON_OVERLAY_STUBS },
450 { "local-store", required_argument, NULL, OPTION_SPU_LOCAL_STORE },
49fa1e15
AM
451 { "stack-analysis", no_argument, NULL, OPTION_SPU_STACK_ANALYSIS },
452 { "emit-stack-syms", no_argument, NULL, OPTION_SPU_STACK_SYMS },
e9f53129
AM
453'
454
455PARSE_AND_LIST_OPTIONS='
456 fprintf (file, _("\
442996ee
AM
457 --plugin Make SPU plugin.\n\
458 --no-overlays No overlay handling.\n\
459 --emit-stub-syms Add symbols on overlay call stubs.\n\
460 --extra-overlay-stubs Add stubs on all calls out of overlay regions.\n\
461 --local-store=lo:hi Valid address range.\n\
462 --stack-analysis Estimate maximum stack requirement.\n\
463 --emit-stack-syms Add sym giving stack needed for each func.\n"
e9f53129
AM
464 ));
465'
466
467PARSE_AND_LIST_ARGS_CASES='
468 case OPTION_SPU_PLUGIN:
469 spu_elf_plugin (1);
470 break;
471
472 case OPTION_SPU_NO_OVERLAYS:
473 no_overlays = 1;
474 break;
475
476 case OPTION_SPU_STUB_SYMS:
477 emit_stub_syms = 1;
478 break;
479
480 case OPTION_SPU_NON_OVERLAY_STUBS:
481 non_overlay_stubs = 1;
482 break;
483
484 case OPTION_SPU_LOCAL_STORE:
485 {
486 char *end;
487 local_store_lo = strtoul (optarg, &end, 0);
488 if (*end == '\'':'\'')
489 {
490 local_store_hi = strtoul (end + 1, &end, 0);
491 if (*end == 0)
492 break;
493 }
494 einfo (_("%P%F: invalid --local-store address range `%s'\''\n"), optarg);
495 }
496 break;
49fa1e15
AM
497
498 case OPTION_SPU_STACK_ANALYSIS:
499 stack_analysis = 1;
500 break;
501
502 case OPTION_SPU_STACK_SYMS:
503 emit_stack_syms = 1;
504 break;
e9f53129
AM
505'
506
507LDEMUL_AFTER_OPEN=spu_after_open
508LDEMUL_BEFORE_ALLOCATION=spu_before_allocation
509LDEMUL_FINISH=gld${EMULATION_NAME}_finish
This page took 0.17698 seconds and 4 git commands to generate.