Skip to content

Instantly share code, notes, and snippets.

@ilyakurdyukov
Created July 10, 2024 04:31
Show Gist options
  • Save ilyakurdyukov/fb73d7134a7c7ba5478bdc21e0b3140f to your computer and use it in GitHub Desktop.
Save ilyakurdyukov/fb73d7134a7c7ba5478bdc21e0b3140f to your computer and use it in GitHub Desktop.
Example of a wrapper for Clang.
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef EXE_PATH
#define EXE_PATH "/usr/bin/clang"
#endif
int main(int argc, char **argv, char **envp) {
char **newargv = argv;
const char *newname = EXE_PATH;
int i, j, first = 0, last = 0;
// skip processing if called with "-cc1" option
if (argc < 2 || !strcmp(argv[1], "-cc1")) goto skip;
newargv = malloc((argc + 3) * sizeof(*newargv));
if (!newargv) {
fprintf(stderr, "!!! malloc failed\n");
return EXIT_FAILURE;
}
for (i = 1; i < argc; i++) {
const char *s = argv[i];
size_t len = strlen(s);
if (len <= 2 || strcmp(s + len - 2, ".a")) continue;
if (!first) first = i;
last = i;
}
newargv[0] = (char*)newname; // argv[0];
for (i = j = 1; i < argc; i++) {
if (i == first) newargv[j++] = "-Wl,--start-group";
newargv[j++] = (char*)argv[i];
if (i == last) newargv[j++] = "-Wl,--end-group";
}
newargv[j] = NULL;
skip:
execve(newname, newargv, envp);
perror(newname);
return EXIT_FAILURE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment