Skip to content

Commit

Permalink
Add -shared option
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent 1e9b6dd commit 4e5de36
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
25 changes: 21 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ static bool opt_c;
static bool opt_cc1;
static bool opt_hash_hash_hash;
static bool opt_static;
static bool opt_shared;
static char *opt_MF;
static char *opt_MT;
static char *opt_o;
Expand Down Expand Up @@ -284,6 +285,12 @@ static void parse_args(int argc, char **argv) {
continue;
}

if (!strcmp(argv[i], "-shared")) {
opt_shared = true;
strarray_push(&ld_extra_args, "-shared");
continue;
}

if (!strcmp(argv[i], "-hashmap-test")) {
hashmap_test();
exit(0);
Expand Down Expand Up @@ -598,9 +605,15 @@ static void run_linker(StringArray *inputs, char *output) {
char *libpath = find_libpath();
char *gcc_libpath = find_gcc_libpath();

strarray_push(&arr, format("%s/crt1.o", libpath));
strarray_push(&arr, format("%s/crti.o", libpath));
strarray_push(&arr, format("%s/crtbegin.o", gcc_libpath));
if (opt_shared) {
strarray_push(&arr, format("%s/crti.o", libpath));
strarray_push(&arr, format("%s/crtbeginS.o", gcc_libpath));
} else {
strarray_push(&arr, format("%s/crt1.o", libpath));
strarray_push(&arr, format("%s/crti.o", libpath));
strarray_push(&arr, format("%s/crtbegin.o", gcc_libpath));
}

strarray_push(&arr, format("-L%s", gcc_libpath));
strarray_push(&arr, "-L/usr/lib/x86_64-linux-gnu");
strarray_push(&arr, "-L/usr/lib64");
Expand Down Expand Up @@ -636,7 +649,11 @@ static void run_linker(StringArray *inputs, char *output) {
strarray_push(&arr, "--no-as-needed");
}

strarray_push(&arr, format("%s/crtend.o", gcc_libpath));
if (opt_shared)
strarray_push(&arr, format("%s/crtendS.o", gcc_libpath));
else
strarray_push(&arr, format("%s/crtend.o", gcc_libpath));

strarray_push(&arr, format("%s/crtn.o", libpath));
strarray_push(&arr, NULL);

Expand Down
6 changes: 6 additions & 0 deletions test/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,10 @@ check -static
file $tmp/foo | grep -q 'statically linked'
check -static

# -shared
echo 'extern int bar; int foo() { return bar; }' > $tmp/foo.c
echo 'int foo(); int bar=3; int main() { foo(); }' > $tmp/bar.c
$chibicc -fPIC -shared -o $tmp/foo.so $tmp/foo.c $tmp/bar.c
check -shared

echo OK

0 comments on commit 4e5de36

Please sign in to comment.