aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/prc/prc.c42
1 files changed, 34 insertions, 8 deletions
diff --git a/src/prc/prc.c b/src/prc/prc.c
index fa2e751..83c4baf 100644
--- a/src/prc/prc.c
+++ b/src/prc/prc.c
@@ -44,7 +44,7 @@ struct ldflags
struct state
{
- const char *as, *qbe, *ld, *prefix, *libexecdir, *name, *out;
+ const char *as, *qbe, *ld, *prefix, *libexecdir, *musl, *name, *out;
const struct arg *arg;
struct input sources, incs, libs, ldirs;
struct procs srcprocs, qbeprocs, asprocs;
@@ -521,19 +521,26 @@ static int isobj(const char *path)
static int ld(struct state *s)
{
int ret = -1;
+ struct dstr crt1 = {0}, crti = {0}, crtn = {0}, ldir = {0};
const struct input *srcs = &s->sources;
const struct procs *asp = &s->asprocs;
const struct ldflags *lf = &s->ldflags;
/*
- * argv[0]
- * -o
- * <out>
- * ...
+ * mandatory arguments:
+ * 1: argv[0]
+ * 2: -static
+ * 3: -o
+ * 4: <out>
+ * <objects>
* <ldflags>
- * -lc
- * argv[n]
+ * 5: <musl>/lib/crt1.o
+ * 6: <musl>/lib/crti.o
+ * 7: <musl>/lib/crtn.o
+ * 8: -L<musl>/lib
+ * 9: -lc
+ * 10: argv[n]
*/
- size_t n = asp->n + lf->n + 5, i = 0;
+ size_t n = asp->n + lf->n + 10, i = 0;
struct spawn *sp = NULL;
char **argv = NULL;
@@ -548,6 +555,7 @@ static int ld(struct state *s)
}
argv[i++] = (char *)s->ld;
+ argv[i++] = "-static";
argv[i++] = "-o";
argv[i++] = (char *)s->out;
@@ -565,6 +573,16 @@ static int ld(struct state *s)
for (size_t j = 0; j < lf->n; j++)
argv[i++] = (char *)lf->v[j];
+ if (append(&crt1, "%s/lib/crt1.o", s->musl)
+ || append(&crti, "%s/lib/crti.o", s->musl)
+ || append(&crtn, "%s/lib/crtn.o", s->musl)
+ || append(&ldir, "-L%s/lib", s->musl))
+ goto end;
+
+ argv[i++] = crt1.s;
+ argv[i++] = crti.s;
+ argv[i++] = crtn.s;
+ argv[i++] = ldir.s;
argv[i++] = "-lc";
argv[i++] = NULL;
@@ -578,6 +596,10 @@ static int ld(struct state *s)
end:
free(argv);
spawn_free(sp);
+ free_dstr(&crt1);
+ free_dstr(&crti);
+ free_dstr(&crtn);
+ free_dstr(&ldir);
return ret;
}
@@ -645,6 +667,9 @@ static void setdefaults(struct state *s)
if (!s->libexecdir)
s->libexecdir = LIBEXECDIR;
+ if (!s->musl)
+ s->musl = s->prefix;
+
s->out = "a.out";
}
@@ -658,6 +683,7 @@ int main(int argc, char *argv[])
.ld = getenv("LD"),
.as = getenv("AS"),
.qbe = getenv("PRCQBE"),
+ .musl = getenv("PRCMUSL"),
.prefix = getenv("PRCPREFIX"),
.libexecdir = getenv("PRCLIBEXECDIR")
};