From 5ee0d121982c45913ba81fef90cca399e2fb99ce Mon Sep 17 00:00:00 2001 From: midipix Date: Wed, 6 Apr 2016 00:50:13 -0400 Subject: link mode: executable wrapper: initial implementation. --- src/logic/slbt_exec_ctx.c | 3 +++ src/logic/slbt_exec_link.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) (limited to 'src') diff --git a/src/logic/slbt_exec_ctx.c b/src/logic/slbt_exec_ctx.c index c9cefdc..8b20f95 100644 --- a/src/logic/slbt_exec_ctx.c +++ b/src/logic/slbt_exec_ctx.c @@ -320,6 +320,9 @@ static int slbt_free_exec_ctx_impl( struct slbt_exec_ctx_impl * ictx, int status) { + if (ictx->ctx.fwrapper) + fclose(ictx->ctx.fwrapper); + free(ictx->args); free(ictx->shadow); free (ictx); diff --git a/src/logic/slbt_exec_link.c b/src/logic/slbt_exec_link.c index 4950bcd..ce9d5b4 100644 --- a/src/logic/slbt_exec_link.c +++ b/src/logic/slbt_exec_link.c @@ -193,6 +193,16 @@ static int slbt_exec_link_adjust_argument_vector( mark = strrchr(*carg,'/'); *mark = '\0'; + if (ectx->fwrapper) { + *slash = '\0'; + + if (fprintf(ectx->fwrapper, + "DL_PATH=\"$DL_PATH$COLON%s\"\n" + "COLON=':'\n\n", + arg) < 0) + return -1; + } + *aarg++ = *carg++; *aarg++ = ++mark; @@ -377,7 +387,9 @@ static int slbt_exec_link_create_executable( const char * exefilename) { char ** parg; + char cwd [PATH_MAX]; char output [PATH_MAX]; + char wrapper[PATH_MAX]; /* initial state */ slbt_reset_arguments(ectx); @@ -400,6 +412,30 @@ static int slbt_exec_link_create_executable( if (dctx->cctx->drvflags & SLBT_DRIVER_NO_UNDEFINED) *ectx->noundef = "-Wl,--no-undefined"; + /* executable wrapper: header */ + if ((size_t)snprintf(wrapper,sizeof(wrapper),"%s.wrapper.tmp", + dctx->cctx->output) + >= sizeof(wrapper)) + return -1; + + if (!(ectx->fwrapper = fopen(wrapper,"w"))) + return -1; + + if (fprintf(ectx->fwrapper, + "#!/bin/sh\n" + "# slibtool (pre-alpha): generated executable wrapper\n\n" + "if [ -z \"$%s\" ]; then\n" + "\tDL_PATH=\n" + "\tCOLON=\n" + "\tLCOLON=\n" + "else\n" + "\tDL_PATH=\n" + "\tCOLON=\n" + "\tLCOLON=':'\n" + "fi\n\n", + dctx->cctx->settings.ldpathenv) < 0) + return -1; + /* output */ if ((size_t)snprintf(output,sizeof(output),"%s", exefilename) @@ -418,6 +454,20 @@ static int slbt_exec_link_create_executable( ectx->argv = ectx->altv; ectx->program = ectx->altv[0]; + + /* executable wrapper: footer */ + if (!getcwd(cwd,sizeof(cwd))) + return -1; + + if (fprintf(ectx->fwrapper, + "DL_PATH=\"$DL_PATH$LCOLON$%s\"\n\n" + "export %s=$DL_PATH\n\n" + "exec %s/%s \"$@\"\n", + dctx->cctx->settings.ldpathenv, + dctx->cctx->settings.ldpathenv, + cwd,exefilename) < 0) + return -1; + /* step output */ if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT)) if (slbt_output_link(dctx,ectx)) @@ -427,6 +477,16 @@ static int slbt_exec_link_create_executable( if ((slbt_spawn(ectx,true) < 0) || ectx->exitcode) return -1; + /* executable wrapper: finalize */ + fclose(ectx->fwrapper); + ectx->fwrapper = 0; + + if (rename(wrapper,dctx->cctx->output)) + return -1; + + if (chmod(dctx->cctx->output,0755)) + return -1; + return 0; } -- cgit v1.2.3