Auto-generate stage2.lds so that the linker script matches the local
linker.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@2122 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/Makefile.am b/coregrind/Makefile.am
index 4105775..473297f 100644
--- a/coregrind/Makefile.am
+++ b/coregrind/Makefile.am
@@ -1,5 +1,5 @@
 
-SUBDIRS = demangle . docs x86
+SUBDIRS = x86 demangle . docs
 
 add_includes = -I$(srcdir)/demangle -I$(top_srcdir)/include -I$(srcdir)/x86
 
diff --git a/coregrind/stage1.c b/coregrind/stage1.c
index 4d6d5db..638bf89 100644
--- a/coregrind/stage1.c
+++ b/coregrind/stage1.c
@@ -135,9 +135,7 @@
    info.setbrk = 1;		/* ask do_exec to move the brk-base */
    info.argv = NULL;
 
-   strcpy(buf, valgrind_lib);
-   strcat(buf, "/");
-   strcat(buf, stage2);
+   snprintf(buf, sizeof(buf), "%s/%s", valgrind_lib, stage2);
 
    err = do_exec(buf, &info);
 
diff --git a/coregrind/stage2.c b/coregrind/stage2.c
index c105bd8..113103f 100644
--- a/coregrind/stage2.c
+++ b/coregrind/stage2.c
@@ -65,7 +65,7 @@
 	 break;
 
       case AT_UME_EXECFD:
-	 kp.execfd = auxv->a_val;
+	 kp.vgexecfd = auxv->a_val;
 	 found |= 2;
 	 break;
       }
@@ -593,7 +593,7 @@
 
    if (!scan_auxv()) {
       fprintf(stderr, "stage2 must be launched by stage1\n");
-      exit(1);
+      exit(127);
    }
 
    if (0) {
@@ -867,7 +867,7 @@
 
       fprintf(stderr, "Aborting: couldn't initialize valgrind\n");
       list_tools();
-      exit(1);
+      exit(127);
    }
    
    /* Work out overall shape of the address space. This tries to give
@@ -900,11 +900,13 @@
    info.setbrk = 0;
    info.argv = cl_argv;
 
+   kp.clexecfd = open(exec, O_RDONLY);
+
    {
       int ret = do_exec(exec, &info);
       if (ret != 0) {
 	 fprintf(stderr, "do_exec(%s) failed: %s\n", exec, strerror(ret));
-	 exit(1);
+	 exit(127);
       }
    }
 
diff --git a/coregrind/ume.c b/coregrind/ume.c
index bf8fb15..192bdbe 100644
--- a/coregrind/ume.c
+++ b/coregrind/ume.c
@@ -608,6 +608,11 @@
       gid_t groups[32];
       int ngrp = getgroups(32, groups);
 
+      if (st.st_mode & (S_ISUID | S_ISGID)) {
+	 fprintf(stderr, "Can't execute suid/sgid executable %s\n", exe);
+	 return EACCES;
+      }
+
       if (uid == st.st_uid && !(st.st_mode & S_IXUSR))
 	 return EACCES;
 
diff --git a/coregrind/vg_errcontext.c b/coregrind/vg_errcontext.c
index 89baa24..9de782e 100644
--- a/coregrind/vg_errcontext.c
+++ b/coregrind/vg_errcontext.c
@@ -265,6 +265,8 @@
          m_esp = tst->m_esp;
          m_ebp = tst->m_ebp;
       }
+      VG_(printf)("starting gdb with eip=%p esp=%p ebp=%p\n",
+		  m_eip, m_esp, m_ebp);
       VG_(swizzle_esp_then_start_GDB)( m_eip, m_esp, m_ebp );
    }
    /* Or maybe we want to generate the error's suppression? */
diff --git a/coregrind/vg_include.h b/coregrind/vg_include.h
index 1d8ab02..9c72831 100644
--- a/coregrind/vg_include.h
+++ b/coregrind/vg_include.h
@@ -1335,7 +1335,8 @@
    Char **argv;
    const Char *libdir;		/* library directory                    */
 
-   Int  execfd;			/* fd of our own (stage1) executable    */
+   Int  vgexecfd;		/* fd of our own (stage1) executable    */
+   Int  clexecfd;		/* fd of the client executable          */
 
    Addr client_base;		/* start of client address space	*/
    Addr	client_end;		/* end of client address space		*/
@@ -1384,7 +1385,10 @@
 extern Addr VG_(valgrind_end);
 
 /* stage1 executable file descriptor */
-extern Int  VG_(execfd);
+extern Int  VG_(vgexecfd);
+
+/* client executable file descriptor */
+extern Int  VG_(clexecfd);
 
 /* Path to all our library/aux files */
 extern const Char *VG_(libdir);
diff --git a/coregrind/vg_intercept.c b/coregrind/vg_intercept.c
index bf29bd7..65ea911 100644
--- a/coregrind/vg_intercept.c
+++ b/coregrind/vg_intercept.c
@@ -62,7 +62,7 @@
    message instead of "raise" */
 int gsignal(int sig)
 {
-   raise(sig);
+   return raise(sig);
 }
 
 /* ---------------------------------------------------------------------
diff --git a/coregrind/vg_main.c b/coregrind/vg_main.c
index 1585cd0..b409eea 100644
--- a/coregrind/vg_main.c
+++ b/coregrind/vg_main.c
@@ -135,7 +135,10 @@
 Addr VG_(valgrind_end);
 
 /* stage1 (main) executable */
-Int  VG_(execfd) = -1;
+Int  VG_(vgexecfd) = -1;
+
+/* client executable */
+Int  VG_(clexecfd) = -1;
 
 /* Path to library directory */
 const Char *VG_(libdir) = VG_LIBDIR;
@@ -1389,8 +1392,10 @@
 
    vg_assert(VG_(clstk_end) == VG_(client_end));
 
-   if (kp->execfd != -1)
-      VG_(execfd) = VG_(safe_fd)(kp->execfd);
+   if (kp->vgexecfd != -1)
+      VG_(vgexecfd) = VG_(safe_fd)(kp->vgexecfd);
+   if (kp->clexecfd != -1)
+      VG_(clexecfd) = VG_(safe_fd)(kp->clexecfd);
 
    if (0) {
       if (VG_(have_ssestate))
@@ -1709,13 +1714,13 @@
    service after an error has been shown, so she can poke around and
    look at parameters, memory, etc.  You can't meaningfully get GDB to
    continue the program, though; to continue, quit GDB.  */
-extern void VG_(start_GDB_whilst_on_client_stack) ( void )
+void VG_(start_GDB_whilst_on_client_stack) ( void )
 {
    Int   res;
    UChar buf[100];
 
-   VG_(sprintf)(buf, "%s -nw /proc/%d/exe %d",
-                VG_(clo_GDB_path), VG_(getpid)(), VG_(getpid)());
+   VG_(sprintf)(buf, "%s -nw /proc/%d/fd/%d %d",
+                VG_(clo_GDB_path), VG_(getpid)(), VG_(clexecfd), VG_(getpid)());
    VG_(message)(Vg_UserMsg, "starting GDB with cmd: %s", buf);
    res = VG_(system)(buf);
    if (res == 0) {      
diff --git a/coregrind/vg_syscalls.c b/coregrind/vg_syscalls.c
index 91f84f7..e4eeb2a 100644
--- a/coregrind/vg_syscalls.c
+++ b/coregrind/vg_syscalls.c
@@ -1716,7 +1716,7 @@
       exec = VG_(arena_malloc)(VG_AR_CORE, VG_(strlen)(exec) + 7 /* --exec= */ + 1 /* \0 */);
 
       VG_(sprintf)(exec, "--exec=%s", (Char *)arg1);
-      VG_(sprintf)(exename, "/proc/self/fd/%d", VG_(execfd));
+      VG_(sprintf)(exename, "/proc/self/fd/%d", VG_(vgexecfd));
 
       optlen += VG_(strlen)(exec)+1;
 
diff --git a/coregrind/x86/Makefile.am b/coregrind/x86/Makefile.am
index 1ef4d9c..8fb5c59 100644
--- a/coregrind/x86/Makefile.am
+++ b/coregrind/x86/Makefile.am
@@ -3,9 +3,18 @@
 
 EXTRA_DIST =		\
 	Make.inc	\
-	stage2.lds	\
 	ume_archdefs.c	\
 	ume_archdefs.h	\
 	ume_entry.S	\
 	ume_go.c
 
+BUILT_SOURCES = stage2.lds
+CLEANFILES = stage2.lds
+
+# Extract ld's default linker script and hack it to our needs
+stage2.lds: Makefile
+	ld --verbose | sed \
+		-e '1,/^=====\+$$/d' \
+		-e '/^=====\+$$/d' \
+		-e 's/ENTRY(_start)/ENTRY(_ume_entry)/' \
+		-e 's/0x08048000/kickstart_base/' > $@ || rm -f $@
diff --git a/coregrind/x86/stage2.lds b/coregrind/x86/stage2.lds
deleted file mode 100644
index 7153162..0000000
--- a/coregrind/x86/stage2.lds
+++ /dev/null
@@ -1,191 +0,0 @@
-/* Linker script for kickstart - slightly modified from the output of ld --verbose */
-OUTPUT_FORMAT("elf32-i386", "elf32-i386",
-	      "elf32-i386")
-OUTPUT_ARCH(i386)
-ENTRY(_ume_entry)
-SEARCH_DIR("/usr/i386-redhat-linux/lib"); SEARCH_DIR("/usr/lib"); SEARCH_DIR("/usr/local/lib"); SEARCH_DIR("/lib");
-/* Do we need any of these for elf?
-   __DYNAMIC = 0;    */
-
-SECTIONS
-{
-  /* Read-only sections, merged into text segment: */
-  . = kickstart_base + SIZEOF_HEADERS;
-  .interp         : { *(.interp) }
-  .hash           : { *(.hash) }
-  .dynsym         : { *(.dynsym) }
-  .dynstr         : { *(.dynstr) }
-  .gnu.version    : { *(.gnu.version) }
-  .gnu.version_d  : { *(.gnu.version_d) }
-  .gnu.version_r  : { *(.gnu.version_r) }
-  .rel.dyn        :
-    {
-      *(.rel.init)
-      *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)
-      *(.rel.fini)
-      *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)
-      *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)
-      *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)
-      *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)
-      *(.rel.ctors)
-      *(.rel.dtors)
-      *(.rel.got)
-      *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)
-    }
-  .rela.dyn       :
-    {
-      *(.rela.init)
-      *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)
-      *(.rela.fini)
-      *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)
-      *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)
-      *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)
-      *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)
-      *(.rela.ctors)
-      *(.rela.dtors)
-      *(.rela.got)
-      *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)
-    }
-  .rel.plt        : { *(.rel.plt) }
-  .rela.plt       : { *(.rela.plt) }
-  .init           :
-  {
-    KEEP (*(.init))
-  } =0x90909090
-  .plt            : { *(.plt) }
-  .text           :
-  {
-    *(.text .stub .text.* .gnu.linkonce.t.*)
-    /* .gnu.warning sections are handled specially by elf32.em.  */
-    *(.gnu.warning)
-  } =0x90909090
-  .fini           :
-  {
-    KEEP (*(.fini))
-  } =0x90909090
-  PROVIDE (__etext = .);
-  PROVIDE (_etext = .);
-  PROVIDE (etext = .);
-  .rodata         : { *(.rodata .rodata.* .gnu.linkonce.r.*) }
-  .rodata1        : { *(.rodata1) }
-  .eh_frame_hdr : { *(.eh_frame_hdr) }
-  .eh_frame       : ONLY_IF_RO { KEEP (*(.eh_frame)) }
-  .gcc_except_table   : ONLY_IF_RO { *(.gcc_except_table) }
-  /* Adjust the address for the data segment.  We want to adjust up to
-     the same address within the page on the next page up.  */
-  . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1)); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000);
-  /* For backward-compatibility with tools that don't support the
-     *_array_* sections below, our glibc's crt files contain weak
-     definitions of symbols that they reference.  We don't want to use
-     them, though, unless they're strictly necessary, because they'd
-     bring us empty sections, unlike PROVIDE below, so we drop the
-     sections from the crt files here.  */
-  /DISCARD/ : {
-      */crti.o(.init_array .fini_array .preinit_array)
-      */crtn.o(.init_array .fini_array .preinit_array)
-  }
-  /* Ensure the __preinit_array_start label is properly aligned.  We
-     could instead move the label definition inside the section, but
-     the linker would then create the section even if it turns out to
-     be empty, which isn't pretty.  */
-  . = ALIGN(32 / 8);
-  PROVIDE (__preinit_array_start = .);
-  .preinit_array     : { *(.preinit_array) }
-  PROVIDE (__preinit_array_end = .);
-  PROVIDE (__init_array_start = .);
-  .init_array     : { *(.init_array) }
-  PROVIDE (__init_array_end = .);
-  PROVIDE (__fini_array_start = .);
-  .fini_array     : { *(.fini_array) }
-  PROVIDE (__fini_array_end = .);
-  .data           :
-  {
-    *(.data .data.* .gnu.linkonce.d.*)
-    SORT(CONSTRUCTORS)
-  }
-  .data1          : { *(.data1) }
-  .tdata	  : { *(.tdata .tdata.* .gnu.linkonce.td.*) }
-  .tbss		  : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }
-  .eh_frame       : ONLY_IF_RW { KEEP (*(.eh_frame)) }
-  .gcc_except_table   : ONLY_IF_RW { *(.gcc_except_table) }
-  .dynamic        : { *(.dynamic) }
-  .ctors          :
-  {
-    /* gcc uses crtbegin.o to find the start of
-       the constructors, so we make sure it is
-       first.  Because this is a wildcard, it
-       doesn't matter if the user does not
-       actually link against crtbegin.o; the
-       linker won't look for a file to match a
-       wildcard.  The wildcard also means that it
-       doesn't matter which directory crtbegin.o
-       is in.  */
-    KEEP (*crtbegin.o(.ctors))
-    /* We don't want to include the .ctor section from
-       from the crtend.o file until after the sorted ctors.
-       The .ctor section from the crtend file contains the
-       end of ctors marker and it must be last */
-    KEEP (*(EXCLUDE_FILE (*crtend.o ) .ctors))
-    KEEP (*(SORT(.ctors.*)))
-    KEEP (*(.ctors))
-  }
-  .dtors          :
-  {
-    KEEP (*crtbegin.o(.dtors))
-    KEEP (*(EXCLUDE_FILE (*crtend.o ) .dtors))
-    KEEP (*(SORT(.dtors.*)))
-    KEEP (*(.dtors))
-  }
-  .jcr            : { KEEP (*(.jcr)) }
-  .got            : { *(.got.plt) *(.got) }
-  _edata = .;
-  PROVIDE (edata = .);
-  __bss_start = .;
-  .bss            :
-  {
-   *(.dynbss)
-   *(.bss .bss.* .gnu.linkonce.b.*)
-   *(COMMON)
-   /* Align here to ensure that the .bss section occupies space up to
-      _end.  Align after .bss to ensure correct alignment even if the
-      .bss section disappears because there are no input sections.  */
-   . = ALIGN(32 / 8);
-  }
-  . = ALIGN(32 / 8);
-  _end = .;
-  PROVIDE (end = .);
-  . = DATA_SEGMENT_END (.);
-  /* Stabs debugging sections.  */
-  .stab          0 : { *(.stab) }
-  .stabstr       0 : { *(.stabstr) }
-  .stab.excl     0 : { *(.stab.excl) }
-  .stab.exclstr  0 : { *(.stab.exclstr) }
-  .stab.index    0 : { *(.stab.index) }
-  .stab.indexstr 0 : { *(.stab.indexstr) }
-  .comment       0 : { *(.comment) }
-  /* DWARF debug sections.
-     Symbols in the DWARF debugging sections are relative to the beginning
-     of the section so we begin them at 0.  */
-  /* DWARF 1 */
-  .debug          0 : { *(.debug) }
-  .line           0 : { *(.line) }
-  /* GNU DWARF 1 extensions */
-  .debug_srcinfo  0 : { *(.debug_srcinfo) }
-  .debug_sfnames  0 : { *(.debug_sfnames) }
-  /* DWARF 1.1 and DWARF 2 */
-  .debug_aranges  0 : { *(.debug_aranges) }
-  .debug_pubnames 0 : { *(.debug_pubnames) }
-  /* DWARF 2 */
-  .debug_info     0 : { *(.debug_info .gnu.linkonce.wi.*) }
-  .debug_abbrev   0 : { *(.debug_abbrev) }
-  .debug_line     0 : { *(.debug_line) }
-  .debug_frame    0 : { *(.debug_frame) }
-  .debug_str      0 : { *(.debug_str) }
-  .debug_loc      0 : { *(.debug_loc) }
-  .debug_macinfo  0 : { *(.debug_macinfo) }
-  /* SGI/MIPS DWARF 2 extensions */
-  .debug_weaknames 0 : { *(.debug_weaknames) }
-  .debug_funcnames 0 : { *(.debug_funcnames) }
-  .debug_typenames 0 : { *(.debug_typenames) }
-  .debug_varnames  0 : { *(.debug_varnames) }
-}