From 5b076e4c227cc3d15f823244d728b21acd30aaa4 Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Wed, 19 Jun 2024 15:49:11 +0200 Subject: stage-0/gcc: separate gcc.real from wrapping and use compiled wrapper --- etc/patches/wrapper.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 etc/patches/wrapper.c (limited to 'etc/patches') diff --git a/etc/patches/wrapper.c b/etc/patches/wrapper.c new file mode 100644 index 0000000..ca4f4b1 --- /dev/null +++ b/etc/patches/wrapper.c @@ -0,0 +1,93 @@ +#include +#include +#include +#include + +int main(int argc, char** argv) { +#include "toolname.h" +#include "link_args.h" + char *s, *path, *newpath, *prgdir, *cwd, **newargv, *tool; + size_t size, s_s, s_cwd, s_prgdir, s_path, s_toolname; + int i; + + for (link_args_c =0; link_args[link_args_c] != NULL; link_args_c++) {} + + if ((prgdir=strdup(argv[0])) == NULL) { + return 66; + } + s = strrchr(prgdir, '/'); + if (s == NULL) { + prgdir = getenv("CC_REAL_DIRECTORY"); + if (prgdir == NULL) { + prgdir = "."; + } + } else { + *s = '\0'; + } + + if (*prgdir != '/') { + /* called by non-absolute path, need to prefix with cwd */ + for (size = 1024;; size *= 2) { + if ((cwd=(char*)malloc(size)) == NULL) { + return 66; + } + if (getcwd(cwd, size) != NULL) { + break; + } + if (errno != ERANGE) { + return 66; + } + } + s = prgdir; + s_s = strlen(s); + s_cwd = strlen(cwd); + + if ((prgdir=(char*)malloc(s_cwd + 1 + s_s + 1))==NULL) { + return 66; + } + strcpy(prgdir, cwd); + strcpy(&prgdir[s_cwd], "/"); + strcpy(&prgdir[s_cwd+1], s); + } + + /* prgdir is now our best guess for the directory argv[0] is located in */ + path = getenv("PATH"); + if (path == NULL) { + path = prgdir; + } else { + s_path = strlen(path); + s_prgdir = strlen(prgdir); + if ((newpath=(char*)malloc(s_prgdir + 1 + s_path + 1)) == NULL) { + return 66; + } + strcpy(newpath, prgdir); + strcpy(&newpath[s_prgdir], ":"); + strcpy(&newpath[s_prgdir+1], path); + path = newpath; + } + setenv("PATH", path, 1); + + if ((newargv=(char**)malloc((argc + link_args_c + 1)*sizeof(char*)))==NULL) { + return 66; + } + for (i=0; i