This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

embedding spu executables in ppc shared libs


The new scheme for SPU programs to access PPU symbols without using a
shadow .toe section, ie. by relocating locations in the SPU ELF image
directly, has a problem when building PPU shared libraries (or PIEs)
with embedded SPU programs.  Relocations against the image will be
emitted as dynamic relocations, which is bad in a .rodata section.
(The old .toe scheme doesn't have this problem.)
Fixed as follows.

binutils/
	* embedspu.sh: Emit SPU ELF image to .data.rel.ro.speelf if
	pic or pie and image needs relocs.
ld/
	* emultempl/spuelf.em (base_name): New function, split out from..
	(embedded_spu_file) ..here.  Pass -fPIC or -fpie to embedspu
	invocation if we deduce a shared lib or position independent
	executable build by looking at ctrbegin* linker input files.

Index: binutils/embedspu.sh
===================================================================
RCS file: /cvs/src/src/binutils/embedspu.sh,v
retrieving revision 1.6
diff -u -p -r1.6 embedspu.sh
--- binutils/embedspu.sh	11 May 2007 03:10:11 -0000	1.6
+++ binutils/embedspu.sh	29 May 2007 03:07:30 -0000
@@ -146,11 +146,6 @@ main ()
   # 5. Write a table of _SPUEAR_ symbols.
   ${CC} ${FLAGS} -x assembler-with-cpp -nostartfiles -nostdlib \
 	-Wa,-mbig -Wl,-r -Wl,-x -o ${OUTFILE} - <<EOF
- .section .rodata.speelf,"a",@progbits
- .p2align 7
-__speelf__:
- .incbin "${INFILE}"
-
  .section .data.spetoe,"aw",@progbits
  .p2align 7
 __spetoe__:
@@ -181,6 +176,7 @@ $7 != "'${toe}'" && $7 in sec_off { \
 	print "#else"; \
 	print " .reloc __speelf__+" strtonum ("0x" $2) + sec_off[$7] + 4 ", R_PPC_ADDR32, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)); \
 	print "#endif"; \
+	if (!donedef) { print "#define HAS_RELOCS 1"; donedef = 1; }; \
 } \
 $7 != "'${toe}'" && ! $7 in sec_off { \
 	print "#error Section not found for " $8; \
@@ -206,6 +202,7 @@ $3 ~ /R_SPU_PPU/ { \
 	print "#else"; \
 	print " .reloc __speelf__+" strtonum ("0x" $1) + sec_off[rela[sec]] + (substr($3, 10) == "64" ? 4 : 0)", R_PPC_ADDR32, " $5 "+0x" $7; \
 	print "#endif"; \
+	if (!donedef) { print "#define HAS_RELOCS 1"; donedef = 1; }; \
 } \
 $3 ~ /unrecognized:/ { \
 	print "#ifdef _LP64"; \
@@ -213,8 +210,17 @@ $3 ~ /unrecognized:/ { \
 	print "#else"; \
 	print " .reloc __speelf__+" strtonum ("0x" $1) + sec_off[rela[sec]] + ($4 == "f" ? 4 : 0)", R_PPC_ADDR32, " $6 "+0x" $8; \
 	print "#endif"; \
+	if (!donedef) { print "#define HAS_RELOCS 1"; donedef = 1; }; \
 } \
 '`
+#if defined (HAS_RELOCS) && (defined (__PIC__) || defined (__PIE__))
+ .section .data.rel.ro.speelf,"a",@progbits
+#else
+ .section .rodata.speelf,"a",@progbits
+#endif
+ .p2align 7
+__speelf__:
+ .incbin "${INFILE}"
 
  .section .data,"aw",@progbits
  .globl ${SYMBOL}
Index: ld/emultempl/spuelf.em
===================================================================
RCS file: /cvs/src/src/ld/emultempl/spuelf.em,v
retrieving revision 1.5
diff -u -p -r1.5 spuelf.em
--- ld/emultempl/spuelf.em	30 Apr 2007 14:06:40 -0000	1.5
+++ ld/emultempl/spuelf.em	29 May 2007 03:07:55 -0000
@@ -264,6 +264,29 @@ static void clean_tmp (void)
     unlink (tmp_file_list->name);
 }
 
+static const char *
+base_name (const char *path)
+{
+  const char *file = strrchr (path, '/');
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+  {
+    char *bslash = strrchr (path, '\\');
+
+    if (file == NULL || (bslash != NULL && bslash > file))
+      file = bslash;
+    if (file == NULL
+	&& path[0] != '\0'
+	&& path[1] == ':')
+      file = path + 1;
+  }
+#endif
+  if (file == NULL)
+    file = path;
+  else
+    ++file;
+  return file;
+}
+
 /* This function is called when building a ppc32 or ppc64 executable
    to handle embedded spu images.  */
 extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
@@ -282,6 +305,7 @@ embedded_spu_file (lang_input_statement_
   union lang_statement_union **old_stat_tail;
   union lang_statement_union **old_file_tail;
   union lang_statement_union *new_ent;
+  lang_input_statement_type *search;
 
   if (entry->the_bfd->format != bfd_object
       || strcmp (entry->the_bfd->xvec->name, "elf32-spu") != 0
@@ -290,23 +314,7 @@ embedded_spu_file (lang_input_statement_
     return FALSE;
 
   /* Use the filename as the symbol marking the program handle struct.  */
-  sym = strrchr (entry->the_bfd->filename, '/');
-#ifdef HAVE_DOS_BASED_FILE_SYSTEM
-  {
-    char *bslash = strrchr (entry->the_bfd->filename, '\\');
-
-    if (sym == NULL || (bslash != NULL && bslash > sym))
-      sym = bslash;
-    if (sym == NULL
-	&& entry->the_bfd->filename[0] != '\0'
-	&& entry->the_bfd->filename[1] == ':')
-      sym = entry->the_bfd->filename + 1;
-  }
-#endif
-  if (sym == NULL)
-    sym = entry->the_bfd->filename;
-  else
-    ++sym;
+  sym = base_name (entry->the_bfd->filename);
 
   handle = xstrdup (sym);
   for (p = handle; *p; ++p)
@@ -333,6 +341,23 @@ embedded_spu_file (lang_input_statement_
     return FALSE;
   close (fd);
 
+  for (search = (lang_input_statement_type *) input_file_chain.head;
+       search != NULL;
+       search = (lang_input_statement_type *) search->next_real_file)
+    {
+      const char *infile = base_name (search->filename);
+
+      if (infile != NULL
+	  && strncmp (infile, "crtbegin", 8) == 0)
+	{
+	  if (infile[8] == 'S')
+	    flags = concat (flags, " -fPIC", NULL);
+	  else if (infile[8] == 'T')
+	    flags = concat (flags, " -fpie", NULL);
+	  break;
+	}
+    }
+
   /* Use fork() and exec() rather than system() so that we don't
      need to worry about quoting args.  */
   cmd[0] = "embedspu";

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]