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