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