1# This shell script emits a C file. -*- C -*- 2# Copyright 2006, 2007 Free Software Foundation, Inc. 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# 24cat >>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. */ 29static int no_overlays = 0; 30 31/* Non-zero if we want stubs on all calls out of overlay regions. */ 32static int non_overlay_stubs = 0; 33 34/* Whether to emit symbols for stubs. */ 35static int emit_stub_syms = 0; 36 37/* Non-zero to perform stack space analysis. */ 38static int stack_analysis = 0; 39 40/* Whether to emit symbols with stack requirements for each function. */ 41static int emit_stack_syms = 0; 42 43/* Range of valid addresses for loadable sections. */ 44static bfd_vma local_store_lo = 0; 45static bfd_vma local_store_hi = 0x3ffff; 46 47static const char ovl_mgr[] = { 48EOF 49 50../binutils/bin2c < ${srcdir}/emultempl/spu_ovl.o >> e${EMULATION_NAME}.c 51 52cat >>e${EMULATION_NAME}.c <<EOF 53}; 54 55static const struct _ovl_stream ovl_mgr_stream = { 56 ovl_mgr, 57 ovl_mgr + sizeof (ovl_mgr) 58}; 59 60static asection *toe = NULL; 61 62 63static int 64is_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 73static void 74spu_after_open (void) 75{ 76 if (is_spu_target () 77 && !link_info.relocatable 78 && link_info.input_bfds != NULL 79 && !spu_elf_create_sections (output_bfd, &link_info, 80 stack_analysis, emit_stack_syms)) 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 96static void 97spu_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 117static void 118spu_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 } 133 else if (ovl_mgr_stream.start == ovl_mgr_stream.end) 134 einfo ("%F%P: no built-in overlay manager\n"); 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 178static void 179spu_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, 197 stack_analysis, &stub, &ovtab, &toe)) 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 219static void 220gld${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 246EOF 247 248if 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 254struct tflist { 255 struct tflist *next; 256 char name[9]; 257}; 258 259static struct tflist *tmp_file_list; 260 261static 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 267static const char * 268base_name (const char *path) 269{ 270 const char *file = strrchr (path, '/'); 271#ifdef HAVE_DOS_BASED_FILE_SYSTEM 272 { 273 char *bslash = strrchr (path, '\\\\'); 274 275 if (file == NULL || (bslash != NULL && bslash > file)) 276 file = bslash; 277 if (file == NULL 278 && path[0] != '\0' 279 && path[1] == ':') 280 file = path + 1; 281 } 282#endif 283 if (file == NULL) 284 file = path; 285 else 286 ++file; 287 return file; 288} 289 290/* This function is called when building a ppc32 or ppc64 executable 291 to handle embedded spu images. */ 292extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *); 293 294bfd_boolean 295embedded_spu_file (lang_input_statement_type *entry, const char *flags) 296{ 297 const char *cmd[6]; 298 const char *sym; 299 char *handle, *p; 300 struct tflist *tf; 301 char *oname; 302 int fd; 303 pid_t pid; 304 int status; 305 union lang_statement_union **old_stat_tail; 306 union lang_statement_union **old_file_tail; 307 union lang_statement_union *new_ent; 308 lang_input_statement_type *search; 309 310 if (entry->the_bfd->format != bfd_object 311 || strcmp (entry->the_bfd->xvec->name, "elf32-spu") != 0 312 || (entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_EXEC 313 && entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_DYN)) 314 return FALSE; 315 316 /* Use the filename as the symbol marking the program handle struct. */ 317 sym = base_name (entry->the_bfd->filename); 318 319 handle = xstrdup (sym); 320 for (p = handle; *p; ++p) 321 if (!(ISALNUM (*p) || *p == '$' || *p == '.')) 322 *p = '_'; 323 324 if (tmp_file_list == NULL) 325 atexit (clean_tmp); 326 tf = xmalloc (sizeof (*tf)); 327 tf->next = tmp_file_list; 328 tmp_file_list = tf; 329 oname = tf->name; 330 memcpy (tf->name, "ldXXXXXX", sizeof (tf->name)); 331 332#ifdef HAVE_MKSTEMP 333 fd = mkstemp (oname); 334#else 335 oname = mktemp (oname); 336 if (oname == NULL) 337 return FALSE; 338 fd = open (oname, O_RDWR | O_CREAT | O_EXCL, 0600); 339#endif 340 if (fd == -1) 341 return FALSE; 342 close (fd); 343 344 for (search = (lang_input_statement_type *) input_file_chain.head; 345 search != NULL; 346 search = (lang_input_statement_type *) search->next_real_file) 347 { 348 const char *infile = base_name (search->filename); 349 350 if (infile != NULL 351 && strncmp (infile, "crtbegin", 8) == 0) 352 { 353 if (infile[8] == 'S') 354 flags = concat (flags, " -fPIC", NULL); 355 else if (infile[8] == 'T') 356 flags = concat (flags, " -fpie", NULL); 357 break; 358 } 359 } 360 361 /* Use fork() and exec() rather than system() so that we don't 362 need to worry about quoting args. */ 363 cmd[0] = EMBEDSPU; 364 cmd[1] = flags; 365 cmd[2] = handle; 366 cmd[3] = entry->the_bfd->filename; 367 cmd[4] = oname; 368 cmd[5] = NULL; 369 if (trace_file_tries) 370 { 371 info_msg (_("running: %s \"%s\" \"%s\" \"%s\" \"%s\"\n"), 372 cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]); 373 fflush (stdout); 374 } 375 376 pid = fork (); 377 if (pid == -1) 378 return FALSE; 379 if (pid == 0) 380 { 381 execvp (cmd[0], (char *const *) cmd); 382 if (strcmp ("embedspu", EMBEDSPU) != 0) 383 { 384 cmd[0] = "embedspu"; 385 execvp (cmd[0], (char *const *) cmd); 386 } 387 perror (cmd[0]); 388 _exit (127); 389 } 390#ifdef HAVE_WAITPID 391#define WAITFOR(PID, STAT) waitpid (PID, STAT, 0) 392#else 393#define WAITFOR(PID, STAT) wait (STAT) 394#endif 395 if (WAITFOR (pid, &status) != pid 396 || !WIFEXITED (status) 397 || WEXITSTATUS (status) != 0) 398 return FALSE; 399#undef WAITFOR 400 401 old_stat_tail = stat_ptr->tail; 402 old_file_tail = input_file_chain.tail; 403 if (lang_add_input_file (oname, lang_input_file_is_file_enum, NULL) == NULL) 404 return FALSE; 405 406 /* lang_add_input_file put the new list entry at the end of the statement 407 and input file lists. Move it to just after the current entry. */ 408 new_ent = *old_stat_tail; 409 *old_stat_tail = NULL; 410 stat_ptr->tail = old_stat_tail; 411 *old_file_tail = NULL; 412 input_file_chain.tail = old_file_tail; 413 new_ent->header.next = entry->header.next; 414 entry->header.next = new_ent; 415 new_ent->input_statement.next_real_file = entry->next_real_file; 416 entry->next_real_file = new_ent; 417 418 /* Ensure bfd sections are excluded from the output. */ 419 bfd_section_list_clear (entry->the_bfd); 420 entry->loaded = TRUE; 421 return TRUE; 422} 423 424EOF 425fi 426 427# Define some shell vars to insert bits of code into the standard elf 428# parse_args and list_options functions. 429# 430PARSE_AND_LIST_PROLOGUE=' 431#define OPTION_SPU_PLUGIN 301 432#define OPTION_SPU_NO_OVERLAYS (OPTION_SPU_PLUGIN + 1) 433#define OPTION_SPU_STUB_SYMS (OPTION_SPU_NO_OVERLAYS + 1) 434#define OPTION_SPU_NON_OVERLAY_STUBS (OPTION_SPU_STUB_SYMS + 1) 435#define OPTION_SPU_LOCAL_STORE (OPTION_SPU_NON_OVERLAY_STUBS + 1) 436#define OPTION_SPU_STACK_ANALYSIS (OPTION_SPU_LOCAL_STORE + 1) 437#define OPTION_SPU_STACK_SYMS (OPTION_SPU_STACK_ANALYSIS + 1) 438' 439 440PARSE_AND_LIST_LONGOPTS=' 441 { "plugin", no_argument, NULL, OPTION_SPU_PLUGIN }, 442 { "no-overlays", no_argument, NULL, OPTION_SPU_NO_OVERLAYS }, 443 { "emit-stub-syms", no_argument, NULL, OPTION_SPU_STUB_SYMS }, 444 { "extra-overlay-stubs", no_argument, NULL, OPTION_SPU_NON_OVERLAY_STUBS }, 445 { "local-store", required_argument, NULL, OPTION_SPU_LOCAL_STORE }, 446 { "stack-analysis", no_argument, NULL, OPTION_SPU_STACK_ANALYSIS }, 447 { "emit-stack-syms", no_argument, NULL, OPTION_SPU_STACK_SYMS }, 448' 449 450PARSE_AND_LIST_OPTIONS=' 451 fprintf (file, _("\ 452 --plugin Make SPU plugin.\n\ 453 --no-overlays No overlay handling.\n\ 454 --emit-stub-syms Add symbols on overlay call stubs.\n\ 455 --extra-overlay-stubs Add stubs on all calls out of overlay regions.\n\ 456 --local-store=lo:hi Valid address range.\n\ 457 --stack-analysis Estimate maximum stack requirement.\n\ 458 --emit-stack-syms Add __stack_func giving stack needed for each func.\n" 459 )); 460' 461 462PARSE_AND_LIST_ARGS_CASES=' 463 case OPTION_SPU_PLUGIN: 464 spu_elf_plugin (1); 465 break; 466 467 case OPTION_SPU_NO_OVERLAYS: 468 no_overlays = 1; 469 break; 470 471 case OPTION_SPU_STUB_SYMS: 472 emit_stub_syms = 1; 473 break; 474 475 case OPTION_SPU_NON_OVERLAY_STUBS: 476 non_overlay_stubs = 1; 477 break; 478 479 case OPTION_SPU_LOCAL_STORE: 480 { 481 char *end; 482 local_store_lo = strtoul (optarg, &end, 0); 483 if (*end == '\'':'\'') 484 { 485 local_store_hi = strtoul (end + 1, &end, 0); 486 if (*end == 0) 487 break; 488 } 489 einfo (_("%P%F: invalid --local-store address range `%s'\''\n"), optarg); 490 } 491 break; 492 493 case OPTION_SPU_STACK_ANALYSIS: 494 stack_analysis = 1; 495 break; 496 497 case OPTION_SPU_STACK_SYMS: 498 emit_stack_syms = 1; 499 break; 500' 501 502LDEMUL_AFTER_OPEN=spu_after_open 503LDEMUL_BEFORE_ALLOCATION=spu_before_allocation 504LDEMUL_FINISH=gld${EMULATION_NAME}_finish 505