summaryrefslogtreecommitdiff
path: root/etc/patches
diff options
context:
space:
mode:
authorKlaus Aehlig <klaus.aehlig@huawei.com>2024-06-19 15:49:11 +0200
committerKlaus Aehlig <klaus.aehlig@huawei.com>2024-06-28 16:23:54 +0200
commit5b076e4c227cc3d15f823244d728b21acd30aaa4 (patch)
tree3de8c6a050601772b19df84b1324d0b2012ee5e1 /etc/patches
parent103285fee740ce18d908f2a0df0624be9ce86aba (diff)
downloadbootstrappable-toolchain-5b076e4c227cc3d15f823244d728b21acd30aaa4.tar.gz
stage-0/gcc: separate gcc.real from wrapping and use compiled wrapper
Diffstat (limited to 'etc/patches')
-rw-r--r--etc/patches/wrapper.c93
1 files changed, 93 insertions, 0 deletions
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 <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+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<argc; i++) {
+ newargv[i] = argv[i];
+ }
+ for (i=0; i < link_args_c; i++) {
+ newargv[argc+i] = link_args[i];
+ }
+ newargv[argc+link_args_c] = NULL;
+
+ s_prgdir = strlen(prgdir);
+ s_toolname = strlen(toolname);
+ if ((tool=(char*)malloc(s_prgdir+1+s_toolname+1))==NULL) {
+ return 66;
+ }
+ strcpy(tool, prgdir);
+ strcpy(&tool[s_prgdir], "/");
+ strcpy(&tool[s_prgdir+1], toolname);
+
+ execv(tool, newargv);
+
+ return 65; /* exec failed */
+}