Skip to content

Commit

Permalink
Revise linker options
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhsnn committed Mar 11, 2025
1 parent 502aa8f commit 1d82ab1
Showing 1 changed file with 23 additions and 34 deletions.
57 changes: 23 additions & 34 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static char *opt_MF;
static char *opt_MT;
static char *opt_o;

static StringArray ld_paths;
static StringArray ld_extra_args;
static StringArray sysincl_paths;
static StringArray input_paths;
Expand Down Expand Up @@ -276,16 +277,18 @@ static int parse_args(int argc, char **argv) {
continue;
}

if (!strncmp(argv[i], "-l", 2) || !strncmp(argv[i], "-Wl,", 4)) {
strarray_push(&input_paths, argv[i]);
if (comma_arg(argv[i], &as_args, "-Wa,") ||
comma_arg(argv[i], &ld_extra_args, "-Wl,"))
continue;
}

if (comma_arg(argv[i], &as_args, "-Wa,"))
if (take_arg_s(argv, &i, &arg, "-l")) {
strarray_push(&ld_extra_args, "-l");
strarray_push(&ld_extra_args, arg);
continue;
}

if (!strcmp(argv[i], "-rdynamic")) {
strarray_push(&input_paths, "-Wl,--export-dynamic");
strarray_push(&ld_extra_args, "--export-dynamic");
continue;
}

Expand Down Expand Up @@ -357,7 +360,8 @@ static int parse_args(int argc, char **argv) {

if (!strcmp(argv[i], "-pthread")) {
define_macro_cli("_REENTRANT");
strarray_push(&input_paths, "-lpthread");
strarray_push(&ld_extra_args, "-l");
strarray_push(&ld_extra_args, "pthread");
continue;
}

Expand All @@ -374,8 +378,8 @@ static int parse_args(int argc, char **argv) {
}

if (take_arg_s(argv, &i, &arg, "-L")) {
strarray_push(&ld_extra_args, "-L");
strarray_push(&ld_extra_args, arg);
strarray_push(&ld_paths, "-L");
strarray_push(&ld_paths, arg);
continue;
}

Expand Down Expand Up @@ -816,12 +820,15 @@ static void run_linker(StringArray *inputs, char *output) {
strarray_push(&arr, "-dynamic-linker");
strarray_push(&arr, "/lib64/ld-linux-x86-64.so.2");
}
for (int i = 0; i < ld_extra_args.len; i++)
strarray_push(&arr, ld_extra_args.data[i]);
for (int i = 0; i < ld_paths.len; i++)
strarray_push(&arr, ld_paths.data[i]);

for (int i = 0; i < inputs->len; i++)
strarray_push(&arr, inputs->data[i]);

for (int i = 0; i < ld_extra_args.len; i++)
strarray_push(&arr, ld_extra_args.data[i]);

strarray_push(&arr, NULL);

run_subprocess(arr.data);
Expand All @@ -840,8 +847,8 @@ static void run_linker(StringArray *inputs, char *output) {
strarray_push(&arr, format("%s/crtbegin.o", gcc_libpath));
}

for (int i = 0; i < ld_extra_args.len; i++)
strarray_push(&arr, ld_extra_args.data[i]);
for (int i = 0; i < ld_paths.len; i++)
strarray_push(&arr, ld_paths.data[i]);

strarray_push(&arr, format("-L%s", gcc_libpath));
strarray_push(&arr, "-L/usr/lib/x86_64-linux-gnu");
Expand All @@ -861,6 +868,9 @@ static void run_linker(StringArray *inputs, char *output) {
for (int i = 0; i < inputs->len; i++)
strarray_push(&arr, inputs->data[i]);

for (int i = 0; i < ld_extra_args.len; i++)
strarray_push(&arr, ld_extra_args.data[i]);

if (opt_static) {
strarray_push(&arr, "--start-group");
strarray_push(&arr, "-lgcc");
Expand Down Expand Up @@ -929,31 +939,14 @@ int main(int argc, char **argv) {
bool no_fork = (input_cnt == 1);
StringArray ld_args = {0};
FileType opt_x = FILE_NONE;
bool run_ld = false;

for (int i = 0; i < input_paths.len; i++) {
if (!strcmp(input_paths.data[i], "-x")) {
opt_x = parse_opt_x(input_paths.data[++i]);
continue;
}

char *input = input_paths.data[i];

if (!strncmp(input, "-l", 2)) {
strarray_push(&ld_args, input);
continue;
}

if (!strncmp(input, "-Wl,", 4)) {
char *s = strdup(input + 4);
char *arg = strtok(s, ",");
while (arg) {
strarray_push(&ld_args, arg);
arg = strtok(NULL, ",");
}
continue;
}

char *output;
if (opt_o)
output = opt_o;
Expand All @@ -971,7 +964,6 @@ int main(int argc, char **argv) {
// Handle .o or .a
if (type == FILE_OBJ || type == FILE_AR || type == FILE_DSO) {
strarray_push(&ld_args, input);
run_ld = true;
continue;
}

Expand All @@ -988,7 +980,6 @@ int main(int argc, char **argv) {
char *tmp = create_tmpfile();
assemble(input, tmp);
strarray_push(&ld_args, tmp);
run_ld = true;
continue;
}

Expand All @@ -1009,7 +1000,6 @@ int main(int argc, char **argv) {
run_cc1(input, tmp1, no_fork, true);
assemble(tmp1, tmp2);
strarray_push(&ld_args, tmp2);
run_ld = true;
continue;
}

Expand Down Expand Up @@ -1041,11 +1031,10 @@ int main(int argc, char **argv) {
run_cc1(input, tmp1, no_fork, false);
assemble(tmp1, tmp2);
strarray_push(&ld_args, tmp2);
run_ld = true;
continue;
}

if (run_ld)
if (ld_args.len)
run_linker(&ld_args, opt_o ? opt_o : "a.out");
return 0;
}

0 comments on commit 1d82ab1

Please sign in to comment.