summaryrefslogtreecommitdiff
path: root/src/logic/linkcmd
diff options
context:
space:
mode:
Diffstat (limited to 'src/logic/linkcmd')
-rw-r--r--src/logic/linkcmd/slbt_linkcmd_archive.c188
-rw-r--r--src/logic/linkcmd/slbt_linkcmd_argv.c1127
-rw-r--r--src/logic/linkcmd/slbt_linkcmd_deps.c788
-rw-r--r--src/logic/linkcmd/slbt_linkcmd_dsolib.c373
-rw-r--r--src/logic/linkcmd/slbt_linkcmd_executable.c278
-rw-r--r--src/logic/linkcmd/slbt_linkcmd_host.c70
-rw-r--r--src/logic/linkcmd/slbt_linkcmd_implib.c157
7 files changed, 2981 insertions, 0 deletions
diff --git a/src/logic/linkcmd/slbt_linkcmd_archive.c b/src/logic/linkcmd/slbt_linkcmd_archive.c
new file mode 100644
index 0000000..b7c090b
--- /dev/null
+++ b/src/logic/linkcmd/slbt_linkcmd_archive.c
@@ -0,0 +1,188 @@
+/*******************************************************************/
+/* slibtool: a strong libtool implementation, written in C */
+/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */
+/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
+/*******************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+#include <slibtool/slibtool.h>
+#include "slibtool_driver_impl.h"
+#include "slibtool_errinfo_impl.h"
+#include "slibtool_linkcmd_impl.h"
+#include "slibtool_mapfile_impl.h"
+#include "slibtool_metafile_impl.h"
+#include "slibtool_snprintf_impl.h"
+#include "slibtool_symlink_impl.h"
+#include "slibtool_spawn_impl.h"
+#include "slibtool_visibility_impl.h"
+
+static int slbt_exec_link_remove_file(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ const char * target)
+{
+ int fdcwd;
+ char * mark;
+ char * sbuf;
+
+ /* fdcwd */
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ /* remove target (if any) */
+ if (unlinkat(fdcwd,target,0) && (errno != ENOENT))
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ /* remove a previous .disabled placeholder */
+ sbuf = (slbt_get_exec_ictx(ectx))->sbuf;
+ mark = sbuf;
+ mark += sprintf(mark,"%s",target);
+ strcpy(mark,".disabled");
+
+ if (unlinkat(fdcwd,sbuf,0) && (errno != ENOENT))
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ return 0;
+}
+
+
+static bool slbt_archive_is_convenience_library(int fdcwd, const char * arpath)
+{
+ int fd;
+ char laipath[PATH_MAX];
+ char * dot;
+
+ strcpy(laipath,arpath);
+ dot = strrchr(laipath,'.');
+ strcpy(dot,".lai");
+
+ if ((fd = openat(fdcwd,laipath,O_RDONLY,0)) >= 0) {
+ close(fd);
+ return false;
+ }
+
+ return true;
+}
+
+
+slbt_hidden int slbt_exec_link_create_archive(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ const char * arfilename,
+ bool fpic,
+ bool fdep)
+{
+ int fdcwd;
+ char ** argv;
+ char ** aarg;
+ char ** parg;
+ char program[PATH_MAX];
+ char output [PATH_MAX];
+ char namebuf[PATH_MAX];
+
+ /* dlopen, dlpreopen: object compilation (derived from dynamic linking) */
+ if (ectx->dlopenobj) {
+ slbt_ectx_reset_arguments(ectx);
+ slbt_reset_placeholders(ectx);
+
+ sprintf(namebuf,"%s%s",ectx->ldirname,"@ARDLOPEN@");
+
+ if (slbt_exec_link_create_library(
+ dctx,ectx,
+ namebuf,namebuf,namebuf,
+ true,fpic) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+ }
+
+ /* restore initial state */
+ slbt_ectx_reset_arguments(ectx);
+
+ /* placeholders */
+ slbt_reset_placeholders(ectx);
+
+ /* output */
+ if (slbt_snprintf(output,sizeof(output),
+ "%s",arfilename) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ /* tool-specific argument vector */
+ argv = (slbt_get_driver_ictx(dctx))->host.ar_argv;
+
+ /* fdcwd */
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ /* input argument adjustment */
+ aarg = ectx->altv;
+
+ if ((parg = argv)) {
+ for (; *parg; )
+ *aarg++ = *parg++;
+ } else {
+ *aarg++ = program;
+ }
+
+ *aarg++ = "-crs";
+ *aarg++ = output;
+
+ for (parg=ectx->cargv; *parg; parg++)
+ if (slbt_adjust_object_argument(*parg,fpic,!fpic,fdcwd))
+ *aarg++ = *parg;
+
+ if (ectx->dlopenobj)
+ *aarg++ = ectx->dlopenobj;
+
+ *aarg = 0;
+ ectx->argv = ectx->altv;
+
+ /* ar program */
+ if (argv) {
+ ectx->program = argv[0];
+ } else {
+ if (slbt_snprintf(program,sizeof(program),
+ "%s",dctx->cctx->host.ar) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ ectx->program = program;
+ }
+
+ /* step output */
+ if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
+ if (slbt_output_link(ectx))
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* remove old archive as needed */
+ if (slbt_exec_link_remove_file(dctx,ectx,output))
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* .deps */
+ if (fdep)
+ if (slbt_exec_link_create_dep_file(
+ dctx,ectx,ectx->cargv,
+ arfilename,true))
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* ar spawn */
+ if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) {
+ return SLBT_SPAWN_ERROR(dctx);
+
+ } else if (ectx->exitcode) {
+ return SLBT_CUSTOM_ERROR(
+ dctx,
+ SLBT_ERR_AR_ERROR);
+ }
+
+ /* input objects associated with .la archives */
+ for (parg=ectx->cargv; *parg; parg++)
+ if (slbt_adjust_wrapper_argument(
+ *parg,true,
+ dctx->cctx->settings.arsuffix))
+ if (slbt_archive_is_convenience_library(fdcwd,*parg))
+ if (slbt_util_import_archive(ectx,output,*parg))
+ return SLBT_NESTED_ERROR(dctx);
+ return 0;
+}
diff --git a/src/logic/linkcmd/slbt_linkcmd_argv.c b/src/logic/linkcmd/slbt_linkcmd_argv.c
new file mode 100644
index 0000000..013317f
--- /dev/null
+++ b/src/logic/linkcmd/slbt_linkcmd_argv.c
@@ -0,0 +1,1127 @@
+/*******************************************************************/
+/* slibtool: a strong libtool implementation, written in C */
+/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */
+/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
+/*******************************************************************/
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+#include <slibtool/slibtool.h>
+#include "slibtool_driver_impl.h"
+#include "slibtool_errinfo_impl.h"
+#include "slibtool_spawn_impl.h"
+#include "slibtool_linkcmd_impl.h"
+#include "slibtool_mapfile_impl.h"
+#include "slibtool_metafile_impl.h"
+#include "slibtool_snprintf_impl.h"
+#include "slibtool_symlink_impl.h"
+#include "slibtool_readlink_impl.h"
+#include "slibtool_visibility_impl.h"
+#include "slibtool_ar_impl.h"
+
+
+static const char * slbt_ar_self_dlunit = "@PROGRAM@";
+
+static int slbt_linkcmd_exit(
+ struct slbt_deps_meta * depsmeta,
+ int ret)
+{
+ if (depsmeta->altv)
+ free(depsmeta->altv);
+
+ if (depsmeta->args)
+ free(depsmeta->args);
+
+ return ret;
+}
+
+
+static int slbt_emit_fdwrap_amend_dl_path(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ struct slbt_deps_meta * depsmeta,
+ const char * fmt,
+ ...)
+{
+ va_list ap;
+ char * buf;
+ int cnt;
+ char dlpathbuf[2048];
+ int fdwrap;
+ const char * fdwrap_fmt;
+ int size;
+
+ va_start(ap,fmt);
+
+ size = sizeof(dlpathbuf);
+
+ buf = ((cnt = vsnprintf(dlpathbuf,size,fmt,ap)) < size)
+ ? dlpathbuf : malloc((size = cnt + 1));
+
+ va_end(ap);
+
+ if (buf == dlpathbuf) {
+ (void)0;
+
+ } else if (buf) {
+ va_start(ap,fmt);
+ vsprintf(buf,fmt,ap);
+ va_end(ap);
+
+ } else {
+ return slbt_linkcmd_exit(
+ depsmeta,
+ SLBT_SYSTEM_ERROR(dctx,0));
+ }
+
+ if ((fdwrap = slbt_exec_get_fdwrapper(ectx)) >= 0) {
+ if (buf[0] == '/') {
+ fdwrap_fmt =
+ "DL_PATH=\"${DL_PATH}${COLON}%s\"\n"
+ "COLON=':'\n\n";
+ } else {
+ fdwrap_fmt =
+ "DL_PATH=\"${DL_PATH}${COLON}${DL_PATH_FIXUP}%s\"\n"
+ "COLON=':'\n\n";
+ }
+
+ if (slbt_dprintf(fdwrap,fdwrap_fmt,buf) < 0) {
+ return slbt_linkcmd_exit(
+ depsmeta,
+ SLBT_SYSTEM_ERROR(dctx,0));
+ }
+ }
+
+ return 0;
+}
+
+
+slbt_hidden bool slbt_adjust_object_argument(
+ char * arg,
+ bool fpic,
+ bool fany,
+ int fdcwd)
+{
+ char * slash;
+ char * dot;
+ char base[PATH_MAX];
+
+ if (*arg == '-')
+ return false;
+
+ /* object argument: foo.lo or foo.o */
+ if (!(dot = strrchr(arg,'.')))
+ return false;
+
+ if ((dot[1]=='l') && (dot[2]=='o') && !dot[3]) {
+ dot[1] = 'o';
+ dot[2] = 0;
+
+ } else if ((dot[1]=='o') && !dot[2]) {
+ (void)0;
+
+ } else {
+ return false;
+ }
+
+ /* foo.o requested and is present? */
+ if (!fpic && !faccessat(fdcwd,arg,0,0))
+ return true;
+
+ /* .libs/foo.o */
+ if ((slash = strrchr(arg,'/')))
+ slash++;
+ else
+ slash = arg;
+
+ if (slbt_snprintf(base,sizeof(base),
+ "%s",slash) < 0)
+ return false;
+
+ sprintf(slash,".libs/%s",base);
+
+ if (!faccessat(fdcwd,arg,0,0))
+ return true;
+
+ /* foo.o requested and neither is present? */
+ if (!fpic) {
+ strcpy(slash,base);
+ return true;
+ }
+
+ /* .libs/foo.o explicitly requested and is not present? */
+ if (!fany)
+ return true;
+
+ /* use foo.o in place of .libs/foo.o */
+ strcpy(slash,base);
+
+ if (faccessat(fdcwd,arg,0,0))
+ sprintf(slash,".libs/%s",base);
+
+ return true;
+}
+
+
+slbt_hidden bool slbt_adjust_wrapper_argument(
+ char * arg,
+ bool fpic,
+ const char * suffix)
+{
+ char * slash;
+ char * dot;
+ char base[PATH_MAX];
+
+ if (*arg == '-')
+ return false;
+
+ if (!(dot = strrchr(arg,'.')))
+ return false;
+
+ if (strcmp(dot,".la"))
+ return false;
+
+ if (fpic) {
+ if ((slash = strrchr(arg,'/')))
+ slash++;
+ else
+ slash = arg;
+
+ if (slbt_snprintf(base,sizeof(base),
+ "%s",slash) < 0)
+ return false;
+
+ sprintf(slash,".libs/%s",base);
+ dot = strrchr(arg,'.');
+ }
+
+ strcpy(dot,suffix);
+ return true;
+}
+
+
+slbt_hidden int slbt_adjust_linker_argument(
+ const struct slbt_driver_ctx * dctx,
+ char * arg,
+ char ** xarg,
+ bool fpic,
+ const char * dsosuffix,
+ const char * arsuffix,
+ struct slbt_deps_meta * depsmeta)
+{
+ int fdcwd;
+ int fdlib;
+ char * slash;
+ char * dot;
+ char base[PATH_MAX];
+
+ /* argv switch or non-library input argument? */
+ if (*arg == '-')
+ return 0;
+
+ if (!(dot = strrchr(arg,'.')))
+ return 0;
+
+ /* explicit .a input argument? */
+ if (!(strcmp(dot,arsuffix))) {
+ *xarg = arg;
+ return slbt_get_deps_meta(dctx,arg,1,depsmeta);
+ }
+
+ /* explicit .so input argument? */
+ if (!(strcmp(dot,dsosuffix)))
+ return slbt_get_deps_meta(dctx,arg,1,depsmeta);
+
+ /* not an .la library? */
+ if (strcmp(dot,".la"))
+ return 0;
+
+ /* .la file, associated .deps located under .libs */
+ if ((slash = strrchr(arg,'/'))) {
+ slash++;
+ } else {
+ slash = arg;
+ }
+
+ if (slbt_snprintf(base,sizeof(base),
+ "%s",slash) < 0)
+ return 0;
+
+ sprintf(slash,".libs/%s",base);
+ dot = strrchr(arg,'.');
+
+ /* fdcwd */
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ /* .a preferred but a.disabled present? */
+ sprintf(dot,"%s",arsuffix);
+
+ if (slbt_symlink_is_a_placeholder(fdcwd,arg))
+ fpic = true;
+
+ /* shared library dependency? */
+ if (fpic) {
+ sprintf(dot,"%s",dsosuffix);
+
+ if (slbt_symlink_is_a_placeholder(fdcwd,arg)) {
+ sprintf(dot,"%s",arsuffix);
+
+ } else if ((fdlib = openat(fdcwd,arg,O_RDONLY)) >= 0) {
+ close(fdlib);
+
+ } else {
+ sprintf(dot,"%s",arsuffix);
+ }
+
+ return slbt_get_deps_meta(dctx,arg,0,depsmeta);
+ }
+
+ /* input archive */
+ sprintf(dot,"%s",arsuffix);
+ return slbt_get_deps_meta(dctx,arg,0,depsmeta);
+}
+
+
+slbt_hidden int slbt_exec_link_adjust_argument_vector(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ struct slbt_deps_meta * depsmeta,
+ const char * cwd,
+ bool flibrary)
+{
+ int fd;
+ int fdcwd;
+ char ** carg;
+ char ** aarg;
+ char * slash;
+ char * mark;
+ char * darg;
+ char * dot;
+ char * base;
+ char * dpath;
+ int argc;
+ char arg[PATH_MAX];
+ char lib[PATH_MAX];
+ char depdir [PATH_MAX];
+ char rpathdir[PATH_MAX];
+ char rpathlnk[PATH_MAX];
+ struct stat st;
+ size_t size;
+ size_t dlen;
+ struct slbt_map_info * mapinfo = 0;
+ bool fwholearchive = false;
+ int ret;
+
+ for (argc=0,carg=ectx->cargv; *carg; carg++)
+ argc++;
+
+ if (!(depsmeta->args = calloc(1,depsmeta->infolen)))
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ argc *= 3;
+ argc += depsmeta->depscnt;
+
+ if (!(depsmeta->altv = calloc(argc,sizeof(char *))))
+ return slbt_linkcmd_exit(
+ depsmeta,
+ SLBT_SYSTEM_ERROR(dctx,0));
+
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ carg = ectx->cargv;
+ aarg = depsmeta->altv;
+ darg = depsmeta->args;
+ size = depsmeta->infolen;
+
+ for (; *carg; ) {
+ dpath = 0;
+
+ if (!strcmp(*carg,"-Wl,--whole-archive"))
+ fwholearchive = true;
+ else if (!strcmp(*carg,"-Wl,--no-whole-archive"))
+ fwholearchive = false;
+
+
+
+ /* output annotation */
+ if (carg == ectx->lout[0]) {
+ ectx->mout[0] = &aarg[0];
+ ectx->mout[1] = &aarg[1];
+ }
+
+ /* argument translation */
+ mark = *carg;
+
+ if ((mark[0] == '-') && (mark[1] == 'L')) {
+ if ((ret = slbt_emit_fdwrap_amend_dl_path(
+ dctx,ectx,depsmeta,
+ "%s",&mark[2])) < 0)
+ return ret;
+
+ *aarg++ = *carg++;
+
+ } else if (**carg == '-') {
+ *aarg++ = *carg++;
+
+ } else if (!(dot = strrchr(*carg,'.'))) {
+ *aarg++ = *carg++;
+
+ } else if (ectx->xargv[carg - ectx->cargv]) {
+ *aarg++ = *carg++;
+
+ } else if (!(strcmp(dot,".a"))) {
+ if (flibrary && !fwholearchive) {
+ strcpy(lib,*carg);
+ dot = strrchr(lib,'.');
+ strcpy(dot,".lai");
+
+ if ((fd = openat(fdcwd,lib,O_RDONLY,0)) < 0)
+ *aarg++ = "-Wl,--whole-archive";
+ }
+
+ dpath = lib;
+ sprintf(lib,"%s.slibtool.deps",*carg);
+ *aarg++ = *carg++;
+
+ if (flibrary && !fwholearchive) {
+ if (fd < 0) {
+ *aarg++ = "-Wl,--no-whole-archive";
+ } else {
+ close(fd);
+ }
+ }
+
+ } else if (strcmp(dot,dctx->cctx->settings.dsosuffix)) {
+ *aarg++ = *carg++;
+
+ } else if (carg == ectx->lout[1]) {
+ /* ^^^hoppla^^^ */
+ *aarg++ = *carg++;
+ } else {
+ /* -rpath */
+ sprintf(rpathlnk,"%s.slibtool.rpath",*carg);
+
+ if (!fstatat(fdcwd,rpathlnk,&st,AT_SYMLINK_NOFOLLOW)) {
+ if (slbt_readlinkat(
+ fdcwd,
+ rpathlnk,
+ rpathdir,
+ sizeof(rpathdir)))
+ return slbt_linkcmd_exit(
+ depsmeta,
+ SLBT_SYSTEM_ERROR(dctx,rpathlnk));
+
+ sprintf(darg,"-Wl,%s",rpathdir);
+ *aarg++ = "-Wl,-rpath";
+ *aarg++ = darg;
+ darg += strlen(darg);
+ darg++;
+ }
+
+ dpath = lib;
+ sprintf(lib,"%s.slibtool.deps",*carg);
+
+ /* account for {'-','L','-','l'} */
+ if (slbt_snprintf(arg,
+ sizeof(arg) - 4,
+ "%s",*carg) < 0)
+ return slbt_linkcmd_exit(
+ depsmeta,
+ SLBT_BUFFER_ERROR(dctx));
+
+ if ((slash = strrchr(arg,'/'))) {
+ sprintf(*carg,"-L%s",arg);
+
+ mark = strrchr(*carg,'/');
+ *mark = 0;
+ *slash = 0;
+
+ if ((ret = slbt_emit_fdwrap_amend_dl_path(
+ dctx,ectx,depsmeta,
+ "%s%s%s",
+ ((arg[0] == '/') ? "" : cwd),
+ ((arg[0] == '/') ? "" : "/"),
+ arg)) < 0) {
+ return ret;
+ }
+
+ dlen = strlen(dctx->cctx->settings.dsoprefix);
+
+ /* -module? (todo: non-portable usage, display warning) */
+ if (strncmp(++slash,dctx->cctx->settings.dsoprefix,dlen)) {
+ *--slash = '/';
+ strcpy(*carg,arg);
+ *aarg++ = *carg++;
+ } else {
+ *aarg++ = *carg++;
+ *aarg++ = ++mark;
+
+ slash += dlen;
+
+ sprintf(mark,"-l%s",slash);
+ dot = strrchr(mark,'.');
+ *dot = 0;
+ }
+ } else {
+ *aarg++ = *carg++;
+ }
+ }
+
+ if (dpath && !fstatat(fdcwd,dpath,&st,0)) {
+ if (!(mapinfo = slbt_map_file(
+ fdcwd,dpath,
+ SLBT_MAP_INPUT)))
+ return slbt_linkcmd_exit(
+ depsmeta,
+ SLBT_SYSTEM_ERROR(dctx,dpath));
+
+ if (!(strncmp(lib,".libs/",6))) {
+ *aarg++ = "-L.libs";
+ lib[1] = 0;
+ } else if ((base = strrchr(lib,'/'))) {
+ if (base - lib == 5) {
+ if (!(strncmp(&base[-5],".libs/",6)))
+ base -= 4;
+
+ } else if (base - lib >= 6) {
+ if (!(strncmp(&base[-6],"/.libs/",7)))
+ base -= 6;
+ }
+
+ *base = 0;
+ } else {
+ lib[0] = '.';
+ lib[1] = 0;
+ }
+
+ while (mapinfo->mark < mapinfo->cap) {
+ if (slbt_mapped_readline(dctx,mapinfo,darg,size))
+ return slbt_linkcmd_exit(
+ depsmeta,
+ SLBT_NESTED_ERROR(dctx));
+
+ if (darg[0] != '#') {
+ *aarg++ = darg;
+ }
+
+ mark = darg;
+ dlen = strlen(darg);
+ size -= dlen;
+ darg += dlen;
+ darg[-1] = 0;
+
+ /* handle -L... and ::... as needed */
+ if ((mark[0] == '-')
+ && (mark[1] == 'L')
+ && (mark[2] != '/')) {
+ if (strlen(mark) >= sizeof(depdir) - 1)
+ return slbt_linkcmd_exit(
+ depsmeta,
+ SLBT_BUFFER_ERROR(dctx));
+
+ darg = mark;
+ strcpy(depdir,&mark[2]);
+ sprintf(darg,"-L%s/%s",lib,depdir);
+
+ darg += strlen(darg);
+ darg++;
+
+ if ((ret = slbt_emit_fdwrap_amend_dl_path(
+ dctx,ectx,depsmeta,
+ "%s/%s",lib,depdir)) < 0)
+ return ret;
+
+ } else if ((mark[0] == ':') && (mark[1] == ':')) {
+ if (strlen(mark) >= sizeof(depdir) - 1)
+ return slbt_linkcmd_exit(
+ depsmeta,
+ SLBT_BUFFER_ERROR(dctx));
+
+ darg = mark;
+ strcpy(depdir,&mark[2]);
+
+ sprintf(darg,"%s/%s",
+ mark[2] == '/' ? "" : lib,
+ depdir);
+
+ darg += strlen(darg);
+ darg++;
+
+ } else if ((mark[0] == '-') && (mark[1] == 'L')) {
+ if ((ret = slbt_emit_fdwrap_amend_dl_path(
+ dctx,ectx,depsmeta,
+ "%s",&mark[2])) < 0)
+ return ret;
+ }
+ }
+ }
+
+ if (mapinfo) {
+ slbt_unmap_file(mapinfo);
+ mapinfo = 0;
+ }
+ }
+
+ if (dctx->cctx->drvflags & SLBT_DRIVER_EXPORT_DYNAMIC)
+ if (!slbt_host_objfmt_is_coff(dctx))
+ *aarg++ = "-Wl,--export-dynamic";
+
+ return 0;
+}
+
+
+static int slbt_exec_link_remove_file(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ const char * target)
+{
+ int fdcwd;
+
+ (void)ectx;
+
+ /* fdcwd */
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ /* remove target (if any) */
+ if (!unlinkat(fdcwd,target,0) || (errno == ENOENT))
+ return 0;
+
+ return SLBT_SYSTEM_ERROR(dctx,0);
+}
+
+
+static int slbt_exec_link_create_expsyms_archive(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ char ** lobjv,
+ char ** cnvlv,
+ char (*arname)[PATH_MAX])
+{
+ int ret;
+ char * dot;
+ char ** argv;
+ char ** aarg;
+ char ** parg;
+ struct slbt_archive_ctx * arctx;
+ char ** ectx_argv;
+ char * ectx_program;
+ char output [PATH_MAX];
+ char program[PATH_MAX];
+
+ /* output */
+ if (slbt_snprintf(output,sizeof(output),
+ "%s",ectx->mapfilename) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ if (!(dot = strrchr(output,'.')))
+ return SLBT_CUSTOM_ERROR(
+ dctx,
+ SLBT_ERR_FLOW_ERROR);
+
+ /* .expsyms.xxx --> .expsyms.a */
+ dot[1] = 'a';
+ dot[2] = '\0';
+
+ if (arname)
+ strcpy(*arname,output);
+
+ /* tool-specific argument vector */
+ argv = (slbt_get_driver_ictx(dctx))->host.ar_argv;
+
+ /* ar alternate argument vector */
+ if (!argv)
+ if (slbt_snprintf(program,sizeof(program),
+ "%s",dctx->cctx->host.ar) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ /* ar command argument vector */
+ aarg = lobjv;
+
+ if ((parg = argv)) {
+ for (; *parg; )
+ *aarg++ = *parg++;
+ } else {
+ *aarg++ = program;
+ }
+
+ *aarg++ = "-crs";
+ *aarg++ = output;
+
+ ectx_argv = ectx->argv;
+ ectx_program = ectx->program;
+
+ ectx->argv = lobjv;
+ ectx->program = ectx->argv[0];
+
+ /* step output */
+ if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
+ if (slbt_output_link(ectx))
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* remove old archive as needed */
+ if (slbt_exec_link_remove_file(dctx,ectx,output))
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* ar spawn */
+ if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) {
+ return SLBT_SPAWN_ERROR(dctx);
+
+ } else if (ectx->exitcode) {
+ return SLBT_CUSTOM_ERROR(
+ dctx,
+ SLBT_ERR_AR_ERROR);
+ }
+
+ /* restore link command ectx */
+ ectx->argv = ectx_argv;
+ ectx->program = ectx_program;
+
+ /* input objects associated with .la archives */
+ for (parg=cnvlv; *parg; parg++)
+ if (slbt_util_import_archive(ectx,output,*parg))
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* do the thing */
+ if (slbt_ar_get_archive_ctx(dctx,output,&arctx) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* .expsyms.a --> .exp */
+ if ((*dot = '\0'), !(dot = strrchr(output,'.'))) {
+ slbt_ar_free_archive_ctx(arctx);
+ return SLBT_CUSTOM_ERROR(
+ dctx,
+ SLBT_ERR_FLOW_ERROR);
+ }
+
+ dot[1] = 'e';
+ dot[2] = 'x';
+ dot[3] = 'p';
+ dot[4] = '\0';
+
+ /* symfile */
+ if (dctx->cctx->expsyms) {
+ struct slbt_symlist_ctx * sctx;
+ sctx = (slbt_get_exec_ictx(ectx))->sctx;
+
+ ret = slbt_util_create_symfile(
+ sctx,output,0644);
+ } else {
+ ret = slbt_ar_create_symfile(
+ arctx->meta,
+ output,
+ 0644);
+ }
+
+ /* mapfile */
+ if ((ret == 0) && (dctx->cctx->regex)) {
+ ret = slbt_ar_create_mapfile(
+ arctx->meta,
+ ectx->mapfilename,
+ 0644);
+ }
+
+ slbt_ar_free_archive_ctx(arctx);
+
+ return (ret < 0) ? SLBT_NESTED_ERROR(dctx) : 0;
+}
+
+
+slbt_hidden int slbt_exec_link_finalize_argument_vector(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx)
+{
+ size_t nargs;
+ char * sargv[1024];
+ char ** sargvbuf;
+ char ** base;
+ char ** parg;
+ char ** pcap;
+ char ** argv;
+ char ** mark;
+ char ** aarg;
+ char ** oarg;
+ char ** lobj;
+ char ** cnvl;
+ char ** larg;
+ char ** darg;
+ char ** earg;
+ char ** rarg;
+ char ** aargv;
+ char ** oargv;
+ char ** lobjv;
+ char ** cnvlv;
+ char ** dlargv;
+ char ** cap;
+ char ** src;
+ char ** dst;
+ char * arg;
+ char * dot;
+ char * ccwrap;
+ char * program;
+ const char * arsuffix;
+
+ /* vector size */
+ base = ectx->argv;
+ arsuffix = dctx->cctx->settings.arsuffix;
+
+ for (parg=base; *parg; parg++)
+ (void)0;
+
+ if (dctx->cctx->regex) {
+ argv = (slbt_get_driver_ictx(dctx))->host.ar_argv;
+
+ for (mark=argv; mark && *mark; mark++)
+ (void)0;
+ } else {
+ argv = 0;
+ mark = 0;
+ }
+
+ /* buffer */
+ if ((nargs = ((parg - base) + (mark - argv))) < 256) {
+ aargv = &sargv[0];
+ oargv = &sargv[1*256];
+ lobjv = &sargv[2*256];
+ cnvlv = &sargv[3*256];
+ sargvbuf = 0;
+
+ parg = &sargv[0];
+ pcap = &sargv[1024];
+
+ for (; parg<pcap; )
+ *parg++ = 0;
+
+ } else if (!(sargvbuf = calloc(4*(nargs+1),sizeof(char *)))) {
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ } else {
+ aargv = &sargvbuf[0];
+ oargv = &sargvbuf[1*(nargs+1)];
+ lobjv = &sargvbuf[2*(nargs+1)];
+ cnvlv = &sargvbuf[3*(nargs+1)];
+ }
+
+ aarg = aargv;
+ oarg = oargv;
+ cnvl = cnvlv;
+ lobj = lobjv;
+
+ /* -export-symbols-regex: lobjv in place: ar [arg] [arg] -crs <output> */
+ if (dctx->cctx->regex && argv)
+ lobj += mark - argv + 2;
+ else
+ lobj += 3;
+
+ /* (program name) */
+ parg = &base[1];
+
+ /* split object args from all other args, record output */
+ /* annotation, and remove redundant -l arguments; and */
+ /* create additional vectors of all input objects as */
+ /* convenience libraries for -export-symbols-regex. */
+ for (; *parg; ) {
+ if (ectx->lout[0] == parg) {
+ ectx->lout[0] = &aarg[0];
+ ectx->lout[1] = &aarg[1];
+ }
+
+ if (ectx->mout[0] == parg) {
+ ectx->mout[0] = &aarg[0];
+ ectx->mout[1] = &aarg[1];
+ }
+
+ arg = *parg;
+ dot = strrchr(arg,'.');
+
+ /* object input argument? */
+ if (dot && (!strcmp(dot,".o") || !strcmp(dot,".lo"))) {
+ *lobj++ = *parg;
+ *oarg++ = *parg++;
+
+ /* --whole-archive input argument? */
+ } else if ((arg[0] == '-')
+ && (arg[1] == 'W')
+ && (arg[2] == 'l')
+ && (arg[3] == ',')
+ && !strcmp(&arg[4],"--whole-archive")
+ && parg[1] && parg[2]
+ && !strcmp(parg[2],"-Wl,--no-whole-archive")
+ && (dot = strrchr(parg[1],'.'))
+ && !strcmp(dot,arsuffix)) {
+ *cnvl++ = parg[1];
+ *oarg++ = *parg++;
+ *oarg++ = *parg++;
+ *oarg++ = *parg++;
+
+ /* local archive input argument? */
+ } else if (dot && !strcmp(dot,arsuffix)) {
+ *aarg++ = *parg++;
+
+ /* -l argument? */
+ } else if ((parg[0][0] == '-') && (parg[0][1] == 'l')) {
+ /* find the previous occurence of this -l argument */
+ for (rarg=0, larg=&aarg[-1]; !rarg && (larg>=aargv); larg--)
+ if (!strcmp(*larg,*parg))
+ rarg = larg;
+
+ /* first occurence of this specific -l argument? */
+ if (!rarg) {
+ *aarg++ = *parg++;
+
+ } else {
+ larg = rarg;
+
+ /* if all -l arguments following the previous */
+ /* occurence had already appeared before the */
+ /* previous argument, then the current */
+ /* occurence is (possibly) redundant. */
+
+ for (darg=&larg[1]; rarg && darg<aarg; darg++) {
+ /* only test -l arguments */
+ if ((darg[0][0] == '-') && (darg[0][1] == 'l')) {
+ for (rarg=0, earg=aargv; !rarg && earg<larg; earg++)
+ if (!strcmp(*earg,*darg))
+ rarg = darg;
+ }
+ }
+
+ /* any archive (.a) input arguments between the */
+ /* current occurrence and the previous one? */
+ for (darg=&larg[1]; rarg && darg<aarg; darg++)
+ if ((dot = strrchr(*darg,'.')))
+ if (!(strcmp(dot,arsuffix)))
+ rarg = 0;
+
+ /* final verdict: repeated -l argument? */
+ if (rarg) {
+ parg++;
+
+ } else {
+ *aarg++ = *parg++;
+ }
+ }
+
+ /* -L argument? */
+ } else if ((parg[0][0] == '-') && (parg[0][1] == 'L')) {
+ /* find a previous occurence of this -L argument */
+ for (rarg=0, larg=aargv; !rarg && (larg<aarg); larg++)
+ if (!strcmp(*larg,*parg))
+ rarg = larg;
+
+ /* repeated -L argument? */
+ if (rarg) {
+ parg++;
+ } else {
+ *aarg++ = *parg++;
+ }
+
+ /* dlsyms vtable object must only be added once (see below) */
+ } else if (!strcmp(*parg,"-dlpreopen")) {
+ parg++;
+
+ /* placeholder argument? */
+ } else if (!strncmp(*parg,"-USLIBTOOL_PLACEHOLDER_",23)) {
+ parg++;
+
+ /* all other arguments */
+ } else {
+ *aarg++ = *parg++;
+ }
+ }
+
+ /* dlsyms vtable object inclusion */
+ if (ectx->dlopenobj)
+ *oarg++ = ectx->dlopenobj;
+
+ /* export-symbols-regex, proper dlpreopen support */
+ if (dctx->cctx->libname)
+ if (slbt_exec_link_create_expsyms_archive(
+ dctx,ectx,lobjv,cnvlv,0) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* -dlpreopen self */
+ if (dctx->cctx->drvflags & SLBT_DRIVER_DLPREOPEN_SELF) {
+ struct slbt_archive_ctx * arctx;
+ struct slbt_archive_ctx ** arctxv;
+ struct slbt_exec_ctx_impl * ictx;
+ char arname[PATH_MAX];
+
+ ictx = slbt_get_exec_ictx(ectx);
+ arctxv = ictx->dlactxv;
+ arctx = 0;
+
+ /* add or repalce the archive context */
+ for (; !arctx && *arctxv; )
+ if (!strcmp(*arctxv[0]->path,slbt_ar_self_dlunit))
+ arctx = *arctxv;
+ else
+ arctxv++;
+
+ if (arctx)
+ slbt_ar_free_archive_ctx(arctx);
+
+ if (slbt_exec_link_create_expsyms_archive(
+ dctx,ectx,lobjv,cnvlv,&arname) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ if (slbt_ar_get_archive_ctx(dctx,arname,arctxv) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ arctx = *arctxv;
+ arctx->path = &slbt_ar_self_dlunit;
+
+ if (slbt_ar_update_syminfo(arctx) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* regenerate the dlsyms vtable source */
+ if (slbt_ar_create_dlsyms(
+ ictx->dlactxv,
+ ectx->dlunit,
+ ectx->dlopensrc,
+ 0644) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+ }
+
+ /* program name, ccwrap */
+ if ((ccwrap = (char *)dctx->cctx->ccwrap)) {
+ base[1] = base[0];
+ base[0] = ccwrap;
+ base++;
+ }
+
+ /* join object args */
+ src = oargv;
+ cap = oarg;
+ dst = &base[1];
+
+ for (; src<cap; )
+ *dst++ = *src++;
+
+ /* dlpreopen */
+ if (ectx->dlpreopen)
+ *dst++ = ectx->dlpreopen;
+
+ /* join all other args, eliminate no-op linker path args */
+ src = aargv;
+ cap = aarg;
+
+ for (; src<cap; ) {
+ if ((src[0][0] == '-') && (src[0][1] == 'L')) {
+ for (larg=0,rarg=src; *rarg && !larg; rarg++)
+ if ((rarg[0][0] == '-') && (rarg[0][1] == 'l'))
+ larg = rarg;
+
+ if (larg) {
+ *dst++ = *src++;
+ } else {
+ src++;
+ }
+ } else {
+ *dst++ = *src++;
+ }
+ }
+
+ /* properly null-terminate argv, accounting for redundant -l arguments */
+ *dst = 0;
+
+ /* output annotation */
+ if (ectx->lout[0]) {
+ ectx->lout[0] = &base[1] + (oarg - oargv) + (ectx->lout[0] - aargv);
+ ectx->lout[1] = ectx->lout[0] + 1;
+ }
+
+ if (ectx->mout[0]) {
+ ectx->mout[0] = &base[1] + (oarg - oargv) + (ectx->mout[0] - aargv);
+ ectx->mout[1] = ectx->mout[0] + 1;
+ }
+
+ /* dlsyms vtable object compilation */
+ if (ectx->dlopenobj) {
+ dlargv = (slbt_get_exec_ictx(ectx))->dlargv;
+ *dlargv = base[0];
+
+ src = aargv;
+ cap = aarg;
+ dst = &dlargv[1];
+
+ /* compile argv, based on the linkcmd argv */
+ for (; src<cap; ) {
+ if ((src[0][0] == '-') && (src[0][1] == '-')) {
+ *dst++ = *src;
+
+ } else if ((src[0][0] == '-') && (src[0][1] == 'L')) {
+ (void)0;
+
+ } else if ((src[0][0] == '-') && (src[0][1] == 'l')) {
+ (void)0;
+
+ } else if ((dot = strrchr(*src,'.')) && (dot[1] == 'a') && !dot[2]) {
+ (void)0;
+
+ } else if ((src[0][0] == '-') && (src[0][1] == 'o')) {
+ src++;
+
+ } else if ((src[0][0] == '-') && (src[0][1] == 'D')) {
+ if (!src[0][2])
+ src++;
+
+ } else if ((src[0][0] == '-') && (src[0][1] == 'U')) {
+ if (!src[0][2])
+ src++;
+
+ } else if ((src[0][0] == '-') && (src[0][1] == 'W')) {
+ if ((src[0][2] == 'a') && (src[0][3] == ','))
+ *dst++ = *src;
+
+ } else {
+ *dst++ = *src;
+ }
+
+ src++;
+ }
+
+ *dst++ = dctx->cctx->settings.picswitch
+ ? dctx->cctx->settings.picswitch
+ : *ectx->fpic;
+
+ *dst++ = "-c";
+ *dst++ = ectx->dlopensrc;
+
+ *dst++ = "-o";
+ *dst++ = ectx->dlopenobj;
+
+ *dst++ = 0;
+
+ /* nested compile step */
+ program = ectx->program;
+ ectx->argv = dlargv;
+ ectx->program = dlargv[0];
+
+ if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
+ if (slbt_output_compile(ectx))
+ return SLBT_NESTED_ERROR(dctx);
+
+ if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0))
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ if (ectx->exitcode)
+ return SLBT_CUSTOM_ERROR(
+ dctx,
+ SLBT_ERR_COMPILE_ERROR);
+
+ ectx->argv = base;
+ ectx->program = program;
+ }
+
+ /* all done */
+ if (sargvbuf)
+ free(sargvbuf);
+
+ return 0;
+}
diff --git a/src/logic/linkcmd/slbt_linkcmd_deps.c b/src/logic/linkcmd/slbt_linkcmd_deps.c
new file mode 100644
index 0000000..19ed716
--- /dev/null
+++ b/src/logic/linkcmd/slbt_linkcmd_deps.c
@@ -0,0 +1,788 @@
+/*******************************************************************/
+/* slibtool: a strong libtool implementation, written in C */
+/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */
+/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
+/*******************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+#include <slibtool/slibtool.h>
+#include "slibtool_driver_impl.h"
+#include "slibtool_errinfo_impl.h"
+#include "slibtool_linkcmd_impl.h"
+#include "slibtool_mapfile_impl.h"
+#include "slibtool_metafile_impl.h"
+#include "slibtool_realpath_impl.h"
+#include "slibtool_snprintf_impl.h"
+#include "slibtool_visibility_impl.h"
+
+slbt_hidden int slbt_get_deps_meta(
+ const struct slbt_driver_ctx * dctx,
+ char * libfilename,
+ int fexternal,
+ struct slbt_deps_meta * depsmeta)
+{
+ int fdcwd;
+ char * ch;
+ char * cap;
+ char * base;
+ size_t libexlen;
+ struct stat st;
+ struct slbt_map_info * mapinfo;
+ char depfile[PATH_MAX];
+
+ /* fdcwd */
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ /* -rpath */
+ if (slbt_snprintf(depfile,sizeof(depfile),
+ "%s.slibtool.rpath",
+ libfilename) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ /* -Wl,%s */
+ if (!fstatat(fdcwd,depfile,&st,AT_SYMLINK_NOFOLLOW)) {
+ depsmeta->infolen += st.st_size + 4;
+ depsmeta->infolen++;
+ }
+
+ /* .deps */
+ if (slbt_snprintf(depfile,sizeof(depfile),
+ "%s.slibtool.deps",
+ libfilename) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ /* mapinfo */
+ if (!(mapinfo = slbt_map_file(fdcwd,depfile,SLBT_MAP_INPUT)))
+ return (fexternal && (errno == ENOENT))
+ ? 0 : SLBT_SYSTEM_ERROR(dctx,depfile);
+
+ /* copied length */
+ depsmeta->infolen += mapinfo->size;
+ depsmeta->infolen++;
+
+ /* libexlen */
+ libexlen = (base = strrchr(libfilename,'/'))
+ ? strlen(depfile) + 2 + (base - libfilename)
+ : strlen(depfile) + 2;
+
+ /* iterate */
+ ch = mapinfo->addr;
+ cap = mapinfo->cap;
+
+ for (; ch<cap; ) {
+ if (*ch++ == '\n') {
+ depsmeta->infolen += libexlen;
+ depsmeta->depscnt++;
+ }
+ }
+
+ slbt_unmap_file(mapinfo);
+
+ return 0;
+}
+
+
+static int slbt_exec_link_normalize_dep_file(
+ const struct slbt_driver_ctx * dctx,
+ int deps,
+ char (*depfile)[PATH_MAX])
+{
+ int ret;
+ int fdcwd;
+ int fdtgt;
+ char * dot;
+ char * slash;
+ const char * base;
+ const char * mark;
+ struct slbt_txtfile_ctx * tctx;
+ const char ** pline;
+ char * tgtmark;
+ char * depmark;
+ char * relmark;
+ char * tgtnext;
+ char * depnext;
+ char tgtdir [PATH_MAX];
+ char tgtpath [PATH_MAX];
+ char deppath [PATH_MAX];
+ char pathbuf [PATH_MAX];
+ char depsbuf [PATH_MAX];
+ char basebuf [PATH_MAX];
+ char relapath[PATH_MAX];
+
+ /* fdcwd */
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ /* first-pass dependency file */
+ if (slbt_impl_get_txtfile_ctx(dctx,*depfile,deps,&tctx) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* second-pass dependency file */
+ dot = strrchr(*depfile,'.');
+ strcpy(dot,".tmp2");
+
+ if ((deps = openat(fdcwd,*depfile,O_RDWR|O_CREAT|O_TRUNC,0644)) < 0) {
+ slbt_lib_free_txtfile_ctx(tctx);
+ return SLBT_SYSTEM_ERROR(dctx,depfile);
+ }
+
+ /* fdtgt */
+ strcpy(tgtdir,*depfile);
+
+ if (!(slash = strrchr(tgtdir,'/')))
+ slash = tgtdir;
+
+ slash[0] = '\0';
+
+ if ((slash = strrchr(tgtdir,'/'))) {
+ slash[0] = '\0';
+ slash++;
+ } else {
+ slash = tgtdir;
+ slash[0] = '.';
+ slash[1] = '/';
+ slash[2] = '\0';
+ }
+
+ if ((slash > tgtdir) && strcmp(slash,".libs")) {
+ close(deps);
+ slbt_lib_free_txtfile_ctx(tctx);
+
+ return SLBT_CUSTOM_ERROR(
+ dctx,
+ SLBT_ERR_FLOW_ERROR);
+ }
+
+ if ((fdtgt = openat(fdcwd,tgtdir,O_DIRECTORY|O_CLOEXEC,0)) < 0) {
+ close(deps);
+ slbt_lib_free_txtfile_ctx(tctx);
+
+ return SLBT_CUSTOM_ERROR(
+ dctx,
+ SLBT_ERR_FLOW_ERROR);
+ }
+
+ if (slbt_realpath(fdcwd,tgtdir,0,tgtpath,sizeof(tgtpath)) < 0) {
+ close(fdtgt);
+ close(deps);
+ slbt_lib_free_txtfile_ctx(tctx);
+
+ return SLBT_CUSTOM_ERROR(
+ dctx,
+ SLBT_ERR_FLOW_ERROR);
+ }
+
+ strcpy(pathbuf,tgtpath);
+
+ /* normalize dependency lines as needed */
+ for (pline=tctx->txtlinev; *pline; pline++) {
+ if ((mark = *pline)) {
+ if ((mark[0] == '-') && (mark[1] == 'L')) {
+ mark = &mark[2];
+ base = 0;
+
+ } else if ((mark[0] == ':') && (mark[1] == ':')) {
+ mark = &mark[2];
+ base = mark;
+ }
+
+ if ((mark > *pline) && (mark[0] == '/'))
+ mark = *pline;
+ }
+
+ if (mark > *pline) {
+ if (slbt_realpath(
+ fdtgt,mark,0,deppath,
+ sizeof(deppath)) < 0)
+ mark = *pline;
+
+ else if ((tgtpath[0] != '/') || (deppath[0] != '/'))
+ mark = 0;
+
+ else
+ strcpy(depsbuf,deppath);
+
+ if ((mark > *pline) && base) {
+ slash = strrchr(deppath,'/');
+ *slash = '\0';
+
+ slash = strrchr(deppath,'/');
+ *slash = '\0';
+
+ base = basebuf;
+ slash = &depsbuf[slash - deppath];
+
+ strcpy(basebuf,++slash);
+ strcpy(depsbuf,deppath);
+ }
+ }
+
+ if (mark > *pline) {
+ tgtmark = tgtpath;
+ depmark = deppath;
+
+ tgtnext = strchr(tgtmark,'/');
+ depnext = strchr(depmark,'/');
+
+ while (tgtnext && depnext) {
+ *tgtnext = '\0';
+ *depnext = '\0';
+
+ if (strcmp(tgtmark,depmark)) {
+ tgtnext = 0;
+ depnext = 0;
+ } else {
+ tgtmark = &tgtnext[1];
+ depmark = &depnext[1];
+
+ if (*tgtmark && *depmark) {
+ tgtnext = strchr(tgtmark,'/');
+ depnext = strchr(depmark,'/');
+ } else {
+ tgtnext = 0;
+ depnext = 0;
+ }
+ }
+ }
+
+ strcpy(tgtmark,&pathbuf[tgtmark-tgtpath]);
+ strcpy(depmark,&depsbuf[depmark-deppath]);
+
+ if ((tgtmark - tgtpath) == (depmark - deppath)) {
+ mark = &depmark[strlen(tgtmark)];
+
+ if ((mark[0] == '/') && !strncmp(tgtmark,depmark,mark-depmark))
+ sprintf(relapath,"-L.%s",mark);
+ else
+ mark = relapath;
+ } else {
+ mark = relapath;
+ }
+
+ if (mark == relapath) {
+ relmark = relapath;
+ relmark += sprintf(relapath,"-L../");
+
+ while ((tgtnext = strchr(tgtmark,'/'))) {
+ tgtmark = &tgtnext[1];
+ relmark += sprintf(relmark,"../");
+ }
+
+ strcpy(relmark,depmark);
+ }
+
+ if (base) {
+ relapath[0] = ':';
+ relapath[1] = ':';
+ }
+
+ mark = relapath;
+ }
+
+ if ((mark == relapath) && base) {
+ ret = slbt_dprintf(deps,"%s/%s\n",mark,base);
+
+ } else if (mark) {
+ ret = slbt_dprintf(deps,"%s\n",mark);
+
+ } else {
+ ret = (-1);
+ }
+
+ if (ret < 0) {
+ close(deps);
+ close(fdtgt);
+ slbt_lib_free_txtfile_ctx(tctx);
+
+ return mark
+ ? SLBT_SYSTEM_ERROR(dctx,0)
+ : SLBT_NESTED_ERROR(dctx);
+ }
+
+ if (mark == relapath)
+ strcpy(tgtpath,pathbuf);
+ }
+
+ close(fdtgt);
+ slbt_lib_free_txtfile_ctx(tctx);
+
+ return deps;
+}
+
+
+static int slbt_exec_link_compact_dep_file(
+ const struct slbt_driver_ctx * dctx,
+ int deps,
+ char (*depfile)[PATH_MAX])
+{
+ int fdcwd;
+ struct slbt_txtfile_ctx * tctx;
+ const char ** pline;
+ const char ** pcomp;
+ const char * depline;
+ char * dot;
+
+ /* fdcwd */
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ /* second-pass dependency file */
+ if (slbt_impl_get_txtfile_ctx(dctx,*depfile,deps,&tctx) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* second-pass dependency file */
+ dot = strrchr(*depfile,'.');
+ dot[0] = '\0';
+
+ if ((deps = openat(fdcwd,*depfile,O_RDWR|O_CREAT|O_TRUNC,0644)) < 0) {
+ slbt_lib_free_txtfile_ctx(tctx);
+ return SLBT_SYSTEM_ERROR(dctx,depfile);
+ }
+
+ /* iterate, only write unique entries */
+ for (pline=tctx->txtlinev; *pline; pline++) {
+ depline = *pline;
+
+ if ((depline[0] == '-') && (depline[1] == 'L'))
+ for (pcomp=tctx->txtlinev; depline && pcomp<pline; pcomp++)
+ if (!strcmp(*pcomp,depline))
+ depline = 0;
+
+ if (depline && (slbt_dprintf(deps,"%s\n",depline) < 0)) {
+ close(deps);
+ slbt_lib_free_txtfile_ctx(tctx);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+ }
+
+ slbt_lib_free_txtfile_ctx(tctx);
+
+ return deps;
+}
+
+
+static bool slbt_archive_is_convenience_library(int fdcwd, const char * arpath)
+{
+ int fd;
+ char laipath[PATH_MAX];
+ char * dot;
+
+ strcpy(laipath,arpath);
+ dot = strrchr(laipath,'.');
+ strcpy(dot,".lai");
+
+ if ((fd = openat(fdcwd,laipath,O_RDONLY,0)) >= 0) {
+ close(fd);
+ return false;
+ }
+
+ return true;
+}
+
+
+slbt_hidden int slbt_exec_link_create_dep_file(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ char ** altv,
+ const char * libfilename,
+ bool farchive)
+{
+ int ret;
+ int deps;
+ int fdtmp;
+ int slen;
+ int fdcwd;
+ char ** parg;
+ char * popt;
+ char * plib;
+ char * path;
+ char * mark;
+ char * base;
+ size_t size;
+ bool fdep;
+ char deplib [PATH_MAX];
+ char deppref[PATH_MAX];
+ char reladir[PATH_MAX];
+ char depfile[PATH_MAX];
+ char pathbuf[PATH_MAX];
+ struct stat st;
+ int ldepth;
+ int fardep;
+ int fdyndep;
+ struct slbt_map_info * mapinfo;
+ bool is_reladir;
+
+ /* fdcwd */
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ /* depfile */
+ if (slbt_snprintf(depfile,sizeof(depfile),
+ "%s.slibtool.deps.tmp1",
+ libfilename) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ strcpy(pathbuf,depfile);
+
+ /* dependency prefix */
+ if (depfile[0] == '/') {
+ deppref[0] = '\0';
+ } else {
+ if ((mark = strrchr(depfile,'/')))
+ *mark = 0;
+
+ if (!mark)
+ mark = depfile;
+
+ if (slbt_realpath(fdcwd,depfile,0,reladir,sizeof(reladir)) < 0)
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ if (slbt_realpath(fdcwd,"./",0,deppref,sizeof(deppref)) < 0)
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ if (mark > depfile)
+ *mark = '/';
+
+ mark = &reladir[strlen(deppref)];
+ *mark++ = '\0';
+
+ if (strcmp(reladir,deppref))
+ return SLBT_CUSTOM_ERROR(
+ dctx,
+ SLBT_ERR_FLOW_ERROR);
+
+ if ((base = strrchr(mark,'/')))
+ base++;
+
+ if (!base)
+ base = mark;
+
+ if (strcmp(base,".libs"))
+ return SLBT_CUSTOM_ERROR(
+ dctx,
+ SLBT_ERR_FLOW_ERROR);
+
+ base = mark;
+ mark = deppref;
+
+ for (; base; ) {
+ if ((base = strchr(base,'/'))) {
+ mark += sprintf(mark,"../");
+ base++;
+ }
+ }
+
+ *mark = '\0';
+ }
+
+ /* deps */
+ if ((deps = openat(fdcwd,depfile,O_RDWR|O_CREAT|O_TRUNC,0644)) < 0)
+ return SLBT_SYSTEM_ERROR(dctx,depfile);
+
+ /* informational header */
+ if (slbt_realpath(fdcwd,"./",0,reladir,sizeof(reladir)) < 0)
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ if (slbt_dprintf(deps,
+ "# makefile target: %s\n"
+ "# slibtool target: %s\n"
+ "# cprocess fdcwd: %s\n",
+ dctx->cctx->output,
+ libfilename,
+ reladir) < 0)
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ fdep = 0;
+
+ /* iterate */
+ for (parg=altv; *parg; parg++) {
+ popt = 0;
+ plib = 0;
+ path = 0;
+ mapinfo = 0;
+
+ if (!strncmp(*parg,"-l",2)) {
+ if (fdep) {
+ if (slbt_dprintf(
+ deps,
+ "#\n# makefile target: %s\n",
+ dctx->cctx->output) < 0)
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ fdep = false;
+ }
+
+ popt = *parg;
+ plib = popt + 2;
+
+ } else if (!strncmp(*parg,"-L",2)) {
+ if (fdep) {
+ if (slbt_dprintf(
+ deps,
+ "#\n# makefile target: %s\n",
+ dctx->cctx->output) < 0)
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ fdep = false;
+ }
+
+ popt = *parg;
+ path = popt + 2;
+
+ } else if (!strncmp(*parg,"-f",2)) {
+ (void)0;
+
+ } else if ((popt = strrchr(*parg,'.')) && !strcmp(popt,".la")) {
+ /* import dependency list */
+ fdep = true;
+
+ if ((base = strrchr(*parg,'/')))
+ base++;
+ else
+ base = *parg;
+
+ /* [relative .la directory] */
+ if (base > *parg) {
+ slen = slbt_snprintf(
+ reladir,
+ sizeof(reladir),
+ "%s",*parg);
+
+ if (slen < 0) {
+ close(deps);
+ return SLBT_BUFFER_ERROR(dctx);
+ }
+
+ is_reladir = true;
+ reladir[base - *parg - 1] = 0;
+ } else {
+ is_reladir = false;
+ reladir[0] = '.';
+ reladir[1] = 0;
+ }
+
+
+ /* dynamic library dependency? */
+ strcpy(depfile,*parg);
+ mark = depfile + (base - *parg);
+ size = sizeof(depfile) - (base - *parg);
+ slen = slbt_snprintf(mark,size,".libs/%s",base);
+
+ if (slen < 0) {
+ close(deps);
+ return SLBT_BUFFER_ERROR(dctx);
+ }
+
+ mark = strrchr(mark,'.');
+ strcpy(mark,dctx->cctx->settings.dsosuffix);
+
+ fardep = 0;
+ fdyndep = !fstatat(fdcwd,depfile,&st,0);
+
+ if (fdyndep && farchive) {
+ mark = strrchr(mark,'.');
+ strcpy(mark,dctx->cctx->settings.arsuffix);
+
+ if (fstatat(fdcwd,depfile,&st,0) < 0) {
+ strcpy(mark,dctx->cctx->settings.dsosuffix);
+ } else {
+ fdyndep = 0;
+ }
+ }
+
+ /* [-L... as needed] */
+ if (fdyndep && (ectx->ldirdepth >= 0)) {
+ if (slbt_dprintf(deps,"-L") < 0) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+
+ for (ldepth=ectx->ldirdepth; ldepth; ldepth--) {
+ if (slbt_dprintf(deps,"../") < 0) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+ }
+
+ if (slbt_dprintf(deps,"%s%s.libs\n",
+ (is_reladir ? reladir : ""),
+ (is_reladir ? "/" : "")) < 0) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+ }
+
+ /* -ldeplib */
+ if (fdyndep) {
+ *popt = 0;
+ mark = base;
+ mark += strlen(dctx->cctx->settings.dsoprefix);
+
+ if (slbt_dprintf(deps,"-l%s\n",mark) < 0) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+
+ *popt = '.';
+ } else {
+ strcpy(depfile,*parg);
+
+ slbt_adjust_wrapper_argument(
+ depfile,true,
+ dctx->cctx->settings.arsuffix);
+
+
+ fardep = farchive;
+ fardep |= !slbt_archive_is_convenience_library(fdcwd,depfile);
+ }
+
+ if (fardep) {
+ if (slbt_dprintf(deps,"::") < 0) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+
+ for (ldepth=ectx->ldirdepth; ldepth; ldepth--) {
+ if (slbt_dprintf(deps,"../") < 0) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+ }
+
+
+ if (ectx->ldirdepth >= 0) {
+ if (slbt_dprintf(deps,"%s\n",depfile) < 0) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+ } else {
+ if (slbt_dprintf(deps,"::./%s\n",depfile) < 0) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+ }
+ }
+
+ /* [open dependency list] */
+ strcpy(depfile,*parg);
+ mark = depfile + (base - *parg);
+ size = sizeof(depfile) - (base - *parg);
+ slen = slbt_snprintf(mark,size,".libs/%s",base);
+
+ if (slen < 0) {
+ close(deps);
+ return SLBT_BUFFER_ERROR(dctx);
+ }
+
+ mapinfo = 0;
+
+ mark = strrchr(mark,'.');
+ size = sizeof(depfile) - (mark - depfile);
+
+ if (!fardep) {
+ slen = slbt_snprintf(mark,size,
+ "%s.slibtool.deps",
+ dctx->cctx->settings.dsosuffix);
+
+ if (slen < 0) {
+ close(deps);
+ return SLBT_BUFFER_ERROR(dctx);
+ }
+
+ mapinfo = slbt_map_file(
+ fdcwd,depfile,
+ SLBT_MAP_INPUT);
+
+ if (!mapinfo && (errno != ENOENT)) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+ }
+
+ if (!mapinfo) {
+ slen = slbt_snprintf(mark,size,
+ ".a.slibtool.deps");
+
+ if (slen < 0) {
+ close(deps);
+ return SLBT_BUFFER_ERROR(dctx);
+ }
+
+ mapinfo = slbt_map_file(
+ fdcwd,depfile,
+ SLBT_MAP_INPUT);
+
+ if (!mapinfo) {
+ strcpy(mark,".a.disabled");
+
+ if (fstatat(fdcwd,depfile,&st,AT_SYMLINK_NOFOLLOW)) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,depfile);
+ }
+ }
+ }
+
+ /* [-l... as needed] */
+ while (mapinfo && (mapinfo->mark < mapinfo->cap)) {
+ ret = slbt_mapped_readline(
+ dctx,mapinfo,
+ deplib,sizeof(deplib));
+
+ if (ret) {
+ close(deps);
+ return SLBT_NESTED_ERROR(dctx);
+ }
+
+ if ((deplib[0] == '-') && (deplib[1] == 'L') && (deplib[2] != '/')) {
+ ret = slbt_dprintf(
+ deps,"-L%s%s/%s",
+ deppref,reladir,&deplib[2]);
+
+ } else if ((deplib[0] == ':') && (deplib[1] == ':') && (deplib[2] != '/')) {
+ ret = slbt_dprintf(
+ deps,"::%s%s/%s",
+ deppref,reladir,&deplib[2]);
+
+ } else {
+ ret = slbt_dprintf(
+ deps,"%s",
+ deplib);
+ }
+
+ if (ret < 0) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+ }
+
+ if (mapinfo)
+ slbt_unmap_file(mapinfo);
+ }
+
+ if (plib && (slbt_dprintf(deps,"-l%s\n",plib) < 0)) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+
+ if (path && (slbt_dprintf(deps,"-L%s\n",path) < 0)) {
+ close(deps);
+ return SLBT_SYSTEM_ERROR(dctx,0);
+ }
+ }
+
+ if ((fdtmp = slbt_exec_link_normalize_dep_file(dctx,deps,&pathbuf)) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ close(deps);
+
+ if ((deps = slbt_exec_link_compact_dep_file(dctx,fdtmp,&pathbuf)) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ close(deps);
+ close(fdtmp);
+
+ return 0;
+}
diff --git a/src/logic/linkcmd/slbt_linkcmd_dsolib.c b/src/logic/linkcmd/slbt_linkcmd_dsolib.c
new file mode 100644
index 0000000..cca6aac
--- /dev/null
+++ b/src/logic/linkcmd/slbt_linkcmd_dsolib.c
@@ -0,0 +1,373 @@
+/*******************************************************************/
+/* slibtool: a strong libtool implementation, written in C */
+/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */
+/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
+/*******************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+#include <slibtool/slibtool.h>
+#include "slibtool_driver_impl.h"
+#include "slibtool_errinfo_impl.h"
+#include "slibtool_linkcmd_impl.h"
+#include "slibtool_mapfile_impl.h"
+#include "slibtool_metafile_impl.h"
+#include "slibtool_realpath_impl.h"
+#include "slibtool_snprintf_impl.h"
+#include "slibtool_symlink_impl.h"
+#include "slibtool_spawn_impl.h"
+#include "slibtool_visibility_impl.h"
+
+static int slbt_linkcmd_exit(
+ struct slbt_deps_meta * depsmeta,
+ int ret)
+{
+ if (depsmeta->altv)
+ free(depsmeta->altv);
+
+ if (depsmeta->args)
+ free(depsmeta->args);
+
+ return ret;
+}
+
+static int slbt_exec_link_remove_file(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ const char * target)
+{
+ int fdcwd;
+
+ (void)ectx;
+
+ /* fdcwd */
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ /* remove target (if any) */
+ if (!unlinkat(fdcwd,target,0) || (errno == ENOENT))
+ return 0;
+
+ return SLBT_SYSTEM_ERROR(dctx,0);
+}
+
+static int slbt_exec_link_remove_dso_files(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ const char * target)
+{
+ int fdcwd;
+ char * mark;
+ char * sbuf;
+
+ /* fdcwd */
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ /* remove target (if any) */
+ if (unlinkat(fdcwd,target,0) && (errno != ENOENT))
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ /* remove a previous .disabled placeholder */
+ sbuf = (slbt_get_exec_ictx(ectx))->sbuf;
+ mark = sbuf;
+ mark += sprintf(mark,"%s",target);
+ strcpy(mark,".disabled");
+
+ if (unlinkat(fdcwd,sbuf,0) && (errno != ENOENT))
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ return 0;
+}
+
+slbt_hidden int slbt_exec_link_create_library(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ const char * dsobasename,
+ const char * dsofilename,
+ const char * relfilename,
+ bool fardlopen,
+ bool fpic)
+{
+ int fdcwd;
+ char ** parg;
+ char ** xarg;
+ char * ccwrap;
+ const char * laout;
+ const char * dot;
+ char cwd [PATH_MAX];
+ char output [PATH_MAX];
+ char soname [PATH_MAX];
+ char symfile[PATH_MAX];
+ char mapfile[PATH_MAX];
+ struct slbt_deps_meta depsmeta = {0,0,0,0};
+
+ /* initial state */
+ slbt_ectx_reset_arguments(ectx);
+
+ /* placeholders */
+ slbt_reset_placeholders(ectx);
+
+ /* fdcwd */
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ /* remove previous libfoo.so, libfoo.so.disabled */
+ if (slbt_exec_link_remove_dso_files(dctx,ectx,ectx->dsofilename) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* input argument adjustment */
+ for (parg=ectx->cargv; *parg; parg++)
+ slbt_adjust_object_argument(*parg,fpic,false,fdcwd);
+
+ /* .deps */
+ if (slbt_exec_link_create_dep_file(
+ dctx,ectx,ectx->cargv,
+ dsofilename,false))
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_NESTED_ERROR(dctx));
+
+ /* linker argument adjustment */
+ for (parg=ectx->cargv, xarg=ectx->xargv; *parg; parg++, xarg++)
+ if (slbt_adjust_linker_argument(
+ dctx,
+ *parg,xarg,true,
+ dctx->cctx->settings.dsosuffix,
+ dctx->cctx->settings.arsuffix,
+ &depsmeta) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* --no-undefined */
+ if (dctx->cctx->drvflags & SLBT_DRIVER_NO_UNDEFINED)
+ *ectx->noundef = slbt_host_group_is_darwin(dctx)
+ ? "-Wl,-undefined,error"
+ : "-Wl,--no-undefined";
+
+ /* -soname */
+ dot = strrchr(dctx->cctx->output,'.');
+ laout = (dot && !strcmp(dot,".la"))
+ ? dctx->cctx->output
+ : 0;
+
+ char wl_soname[24];
+
+ if (slbt_host_group_is_darwin(dctx)) {
+ strcpy(wl_soname,"-Wl,-install_name");
+ } else {
+ strcpy(wl_soname,"-Wl,-soname");
+ }
+
+ if ((dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_MACHO)) {
+ (void)0;
+
+ } else if (!laout && (dctx->cctx->drvflags & SLBT_DRIVER_MODULE)) {
+ if (slbt_snprintf(soname,sizeof(soname),
+ "-Wl,%s",dctx->cctx->output) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ *ectx->soname = wl_soname;
+ *ectx->lsoname = soname;
+
+ } else if (relfilename && dctx->cctx->verinfo.verinfo) {
+ if (slbt_snprintf(soname,sizeof(soname),
+ "-Wl,%s%s%s%s%s.%d%s",
+ ectx->sonameprefix,
+ dctx->cctx->libname,
+ dctx->cctx->release ? "-" : "",
+ dctx->cctx->release ? dctx->cctx->release : "",
+ dctx->cctx->settings.osdsuffix,
+ dctx->cctx->verinfo.major,
+ dctx->cctx->settings.osdfussix) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ *ectx->soname = wl_soname;
+ *ectx->lsoname = soname;
+
+ } else if (relfilename) {
+ if (slbt_snprintf(soname,sizeof(soname),
+ "-Wl,%s%s%s%s%s",
+ ectx->sonameprefix,
+ dctx->cctx->libname,
+ dctx->cctx->release ? "-" : "",
+ dctx->cctx->release ? dctx->cctx->release : "",
+ dctx->cctx->settings.dsosuffix) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ *ectx->soname = wl_soname;
+ *ectx->lsoname = soname;
+
+ } else if (dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION) {
+ if (slbt_snprintf(soname,sizeof(soname),
+ "-Wl,%s%s%s",
+ ectx->sonameprefix,
+ dctx->cctx->libname,
+ dctx->cctx->settings.dsosuffix) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ *ectx->soname = wl_soname;
+ *ectx->lsoname = soname;
+
+ } else {
+ if (slbt_snprintf(soname,sizeof(soname),
+ "-Wl,%s%s%s.%d%s",
+ ectx->sonameprefix,
+ dctx->cctx->libname,
+ dctx->cctx->settings.osdsuffix,
+ dctx->cctx->verinfo.major,
+ dctx->cctx->settings.osdfussix) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ *ectx->soname = wl_soname;
+ *ectx->lsoname = soname;
+ }
+
+ /* PE: --output-def */
+ if (dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_PE) {
+ if (slbt_snprintf(symfile,sizeof(symfile),
+ "-Wl,%s",
+ ectx->deffilename) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ *ectx->symdefs = "-Wl,--output-def";
+ *ectx->symfile = symfile;
+ }
+
+ /* -export-symbols */
+ if (dctx->cctx->expsyms) {
+ struct slbt_symlist_ctx * sctx;
+ sctx = (slbt_get_exec_ictx(ectx))->sctx;
+
+ if (slbt_util_create_mapfile(sctx,ectx->mapfilename,0644) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ if (slbt_snprintf(mapfile,sizeof(mapfile),
+ "-Wl,%s",
+ ectx->mapfilename) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ if (slbt_host_group_is_darwin(dctx)) {
+ *ectx->explarg = "-Wl,-exported_symbols_list";
+ *ectx->expsyms = mapfile;
+
+ } else if (slbt_host_group_is_winnt(dctx)) {
+ *ectx->expsyms = mapfile;
+ } else {
+ *ectx->explarg = "-Wl,--version-script";
+ *ectx->expsyms = mapfile;
+ }
+ }
+
+ /* -export-symbols-regex; and see also: */
+ /* slbt_exec_link_create_expsyms_archive() */
+ if (dctx->cctx->regex) {
+ if (slbt_snprintf(mapfile,sizeof(mapfile),
+ "-Wl,%s",
+ ectx->mapfilename) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ if (slbt_host_group_is_darwin(dctx)) {
+ *ectx->explarg = "-Wl,-exported_symbols_list";
+ *ectx->expsyms = mapfile;
+
+ } else if (slbt_host_group_is_winnt(dctx)) {
+ *ectx->expsyms = mapfile;
+ } else {
+ *ectx->explarg = "-Wl,--version-script";
+ *ectx->expsyms = mapfile;
+ }
+ }
+
+ /* shared/static */
+ if (dctx->cctx->drvflags & SLBT_DRIVER_ALL_STATIC) {
+ *ectx->dpic = "-static";
+ } else if (dctx->cctx->settings.picswitch) {
+ *ectx->dpic = "-shared";
+ *ectx->fpic = dctx->cctx->settings.picswitch;
+ } else {
+ *ectx->dpic = "-shared";
+ }
+
+ /* output */
+ if (!laout && dctx->cctx->drvflags & SLBT_DRIVER_MODULE) {
+ strcpy(output,dctx->cctx->output);
+ } else if (relfilename) {
+ strcpy(output,relfilename);
+ } else if (dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION) {
+ strcpy(output,dsofilename);
+ } else {
+ if (slbt_snprintf(output,sizeof(output),
+ "%s%s.%d.%d.%d%s",
+ dsobasename,
+ dctx->cctx->settings.osdsuffix,
+ dctx->cctx->verinfo.major,
+ dctx->cctx->verinfo.minor,
+ dctx->cctx->verinfo.revision,
+ dctx->cctx->settings.osdfussix) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+ }
+
+ /* output marks */
+ *ectx->lout[0] = "-o";
+ *ectx->lout[1] = output;
+
+ /* ldrpath */
+ if (dctx->cctx->host.ldrpath) {
+ if (slbt_exec_link_remove_file(dctx,ectx,ectx->rpathfilename))
+ return SLBT_NESTED_ERROR(dctx);
+
+ if (slbt_create_symlink(
+ dctx,ectx,
+ dctx->cctx->host.ldrpath,
+ ectx->rpathfilename,
+ SLBT_SYMLINK_LITERAL))
+ return SLBT_NESTED_ERROR(dctx);
+ }
+
+ /* cwd */
+ if (slbt_realpath(fdcwd,".",O_DIRECTORY,cwd,sizeof(cwd)))
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ /* .libs/libfoo.so --> -L.libs -lfoo */
+ if (slbt_exec_link_adjust_argument_vector(
+ dctx,ectx,&depsmeta,cwd,true))
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* using alternate argument vector */
+ ccwrap = (char *)dctx->cctx->ccwrap;
+ ectx->argv = depsmeta.altv;
+ ectx->program = ccwrap ? ccwrap : depsmeta.altv[0];
+
+ /* sigh */
+ if (slbt_exec_link_finalize_argument_vector(dctx,ectx))
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* all done? */
+ if (fardlopen)
+ return 0;
+
+ /* step output */
+ if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
+ if (slbt_output_link(ectx))
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_NESTED_ERROR(dctx));
+
+ /* spawn */
+ if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) {
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_SPAWN_ERROR(dctx));
+
+ } else if (ectx->exitcode) {
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_CUSTOM_ERROR(
+ dctx,
+ SLBT_ERR_LINK_ERROR));
+ }
+
+ return slbt_linkcmd_exit(&depsmeta,0);
+}
diff --git a/src/logic/linkcmd/slbt_linkcmd_executable.c b/src/logic/linkcmd/slbt_linkcmd_executable.c
new file mode 100644
index 0000000..936bc02
--- /dev/null
+++ b/src/logic/linkcmd/slbt_linkcmd_executable.c
@@ -0,0 +1,278 @@
+/*******************************************************************/
+/* slibtool: a strong libtool implementation, written in C */
+/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */
+/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
+/*******************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+#include <slibtool/slibtool.h>
+#include "slibtool_driver_impl.h"
+#include "slibtool_errinfo_impl.h"
+#include "slibtool_linkcmd_impl.h"
+#include "slibtool_mapfile_impl.h"
+#include "slibtool_metafile_impl.h"
+#include "slibtool_realpath_impl.h"
+#include "slibtool_snprintf_impl.h"
+#include "slibtool_symlink_impl.h"
+#include "slibtool_spawn_impl.h"
+#include "slibtool_visibility_impl.h"
+
+static int slbt_linkcmd_exit(
+ struct slbt_deps_meta * depsmeta,
+ int ret)
+{
+ if (depsmeta->altv)
+ free(depsmeta->altv);
+
+ if (depsmeta->args)
+ free(depsmeta->args);
+
+ return ret;
+}
+
+static void slbt_emit_fdwrap_dl_path_fixup(
+ char * cwd,
+ char * dpfixup,
+ size_t dpfixup_size,
+ char * wrapper)
+{
+ char * p;
+ char * q;
+ char * wrapper_dname;
+
+ /* obtain cwd-relative directory name of wrapper */
+ for (p=cwd,q=wrapper; *p && *q && (*p==*q); p++,q++)
+ (void)0;
+
+ wrapper_dname = (*q == '/') ? (q + 1) : q;
+
+ dpfixup[0] = 0; strncat(dpfixup,"${0%/*}",dpfixup_size - 1);
+
+ /* append parent directory fixup for each level of depth in wrapper_dname */
+ for (p=wrapper_dname,q=0; *p; ) {
+ if ((p[0] == '.') && (p[1] == '/')) {
+ p++; p++;
+ } else if ((q = strchr(p, '/'))) {
+ strncat(dpfixup,"/..",dpfixup_size-1); p = (q + 1);
+ } else {
+ break;
+ }
+ }
+
+ strncat(dpfixup,"/",dpfixup_size-1);
+}
+
+slbt_hidden int slbt_exec_link_create_executable(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ const char * exefilename)
+{
+ int fdcwd;
+ int fdwrap;
+ char ** parg;
+ char ** xarg;
+ char * base;
+ char * ccwrap;
+ char cwd [PATH_MAX];
+ char dpfixup[PATH_MAX];
+ char output [PATH_MAX];
+ char wrapper[PATH_MAX];
+ char wraplnk[PATH_MAX];
+ bool fabspath;
+ bool fpic;
+ const struct slbt_source_version * verinfo;
+ struct slbt_deps_meta depsmeta = {0,0,0,0};
+ struct stat st;
+
+ /* initial state */
+ slbt_ectx_reset_arguments(ectx);
+
+ /* placeholders */
+ slbt_reset_placeholders(ectx);
+
+ /* fdcwd */
+ fdcwd = slbt_driver_fdcwd(dctx);
+
+ /* fpic */
+ fpic = (dctx->cctx->drvflags & SLBT_DRIVER_SHARED);
+ fpic &= !(dctx->cctx->drvflags & SLBT_DRIVER_PREFER_STATIC);
+
+ /* input argument adjustment */
+ for (parg=ectx->cargv; *parg; parg++)
+ slbt_adjust_object_argument(*parg,fpic,true,fdcwd);
+
+ /* linker argument adjustment */
+ for (parg=ectx->cargv, xarg=ectx->xargv; *parg; parg++, xarg++)
+ if (slbt_adjust_linker_argument(
+ dctx,
+ *parg,xarg,fpic,
+ dctx->cctx->settings.dsosuffix,
+ dctx->cctx->settings.arsuffix,
+ &depsmeta) < 0)
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* --no-undefined */
+ if (dctx->cctx->drvflags & SLBT_DRIVER_NO_UNDEFINED)
+ *ectx->noundef = slbt_host_group_is_darwin(dctx)
+ ? "-Wl,-undefined,error"
+ : "-Wl,--no-undefined";
+
+ /* executable wrapper: create */
+ if (slbt_snprintf(wrapper,sizeof(wrapper),
+ "%s.wrapper.tmp",
+ dctx->cctx->output) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ if ((fdwrap = openat(fdcwd,wrapper,O_RDWR|O_CREAT|O_TRUNC,0644)) < 0)
+ return SLBT_SYSTEM_ERROR(dctx,wrapper);
+
+ slbt_exec_set_fdwrapper(ectx,fdwrap);
+
+ /* executable wrapper: header */
+ verinfo = slbt_api_source_version();
+
+ /* cwd, DL_PATH fixup */
+ if (slbt_realpath(fdcwd,".",O_DIRECTORY,cwd,sizeof(cwd)))
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ slbt_emit_fdwrap_dl_path_fixup(
+ cwd,dpfixup,sizeof(dpfixup),
+ wrapper);
+
+ if (slbt_dprintf(fdwrap,
+ "#!/bin/sh\n"
+ "# libtool compatible executable wrapper\n"
+ "# Generated by %s (slibtool %d.%d.%d)\n"
+ "# [commit reference: %s]\n\n"
+
+ "if [ -z \"$%s\" ]; then\n"
+ "\tDL_PATH=\n"
+ "\tCOLON=\n"
+ "\tLCOLON=\n"
+ "else\n"
+ "\tDL_PATH=\n"
+ "\tCOLON=\n"
+ "\tLCOLON=':'\n"
+ "fi\n\n"
+ "DL_PATH_FIXUP=\"%s\";\n\n",
+
+ dctx->program,
+ verinfo->major,verinfo->minor,verinfo->revision,
+ verinfo->commit,
+ dctx->cctx->settings.ldpathenv,
+ dpfixup) < 0)
+ return SLBT_SYSTEM_ERROR(dctx,0);
+
+ /* output */
+ if (slbt_snprintf(output,sizeof(output),
+ "%s",exefilename) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ *ectx->lout[0] = "-o";
+ *ectx->lout[1] = output;
+
+ /* static? */
+ if (dctx->cctx->drvflags & SLBT_DRIVER_ALL_STATIC)
+ *ectx->dpic = "-static";
+
+ /* .libs/libfoo.so --> -L.libs -lfoo */
+ if (slbt_exec_link_adjust_argument_vector(
+ dctx,ectx,&depsmeta,cwd,false))
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* using alternate argument vector */
+ ccwrap = (char *)dctx->cctx->ccwrap;
+ ectx->argv = depsmeta.altv;
+ ectx->program = ccwrap ? ccwrap : depsmeta.altv[0];
+
+ /* executable wrapper symlink */
+ if (slbt_snprintf(wraplnk,sizeof(wraplnk),
+ "%s.exe.wrapper",
+ exefilename) < 0)
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_BUFFER_ERROR(dctx));
+
+ /* executable wrapper: base name */
+ base = strrchr(wraplnk,'/');
+ base++;
+
+ /* executable wrapper: footer */
+ fabspath = (exefilename[0] == '/');
+
+ if (slbt_dprintf(fdwrap,
+ "DL_PATH=\"${DL_PATH}${LCOLON}${%s}\"\n\n"
+ "export %s=\"$DL_PATH\"\n\n"
+ "if [ $(basename \"$0\") = \"%s\" ]; then\n"
+ "\tprogram=\"$1\"; shift\n"
+ "\texec \"$program\" \"$@\"\n"
+ "fi\n\n"
+ "exec %s/%s \"$@\"\n",
+ dctx->cctx->settings.ldpathenv,
+ dctx->cctx->settings.ldpathenv,
+ base,
+ fabspath ? "" : cwd,
+ fabspath ? &exefilename[1] : exefilename) < 0)
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_SYSTEM_ERROR(dctx,0));
+
+ /* sigh */
+ if (slbt_exec_link_finalize_argument_vector(dctx,ectx))
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* step output */
+ if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
+ if (slbt_output_link(ectx))
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_NESTED_ERROR(dctx));
+
+ /* spawn */
+ if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) {
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_SPAWN_ERROR(dctx));
+
+ } else if (ectx->exitcode) {
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_CUSTOM_ERROR(
+ dctx,
+ SLBT_ERR_LINK_ERROR));
+ }
+
+ /* executable wrapper: finalize */
+ slbt_exec_close_fdwrapper(ectx);
+
+ if (slbt_create_symlink(
+ dctx,ectx,
+ dctx->cctx->output,wraplnk,
+ SLBT_SYMLINK_WRAPPER))
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_NESTED_ERROR(dctx));
+
+ if (fstatat(fdcwd,wrapper,&st,0))
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_SYSTEM_ERROR(dctx,wrapper));
+
+ if (renameat(fdcwd,wrapper,fdcwd,dctx->cctx->output))
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_SYSTEM_ERROR(dctx,dctx->cctx->output));
+
+ if (fchmodat(fdcwd,dctx->cctx->output,0755,0))
+ return slbt_linkcmd_exit(
+ &depsmeta,
+ SLBT_SYSTEM_ERROR(dctx,dctx->cctx->output));
+
+ return slbt_linkcmd_exit(&depsmeta,0);
+}
diff --git a/src/logic/linkcmd/slbt_linkcmd_host.c b/src/logic/linkcmd/slbt_linkcmd_host.c
new file mode 100644
index 0000000..c3d51f8
--- /dev/null
+++ b/src/logic/linkcmd/slbt_linkcmd_host.c
@@ -0,0 +1,70 @@
+/*******************************************************************/
+/* slibtool: a strong libtool implementation, written in C */
+/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */
+/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
+/*******************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+#include <slibtool/slibtool.h>
+#include "slibtool_driver_impl.h"
+#include "slibtool_errinfo_impl.h"
+#include "slibtool_linkcmd_impl.h"
+#include "slibtool_mapfile_impl.h"
+#include "slibtool_metafile_impl.h"
+#include "slibtool_snprintf_impl.h"
+#include "slibtool_symlink_impl.h"
+#include "slibtool_visibility_impl.h"
+
+slbt_hidden int slbt_exec_link_create_host_tag(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ char * deffilename)
+{
+ char * slash;
+ char hosttag[PATH_MAX];
+ char hostlnk[PATH_MAX];
+
+ /* libfoo.so.def.{flavor} */
+ if (slbt_snprintf(hosttag,
+ sizeof(hosttag),
+ "%s.%s",
+ deffilename,
+ dctx->cctx->host.flavor) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ if (slbt_snprintf(hostlnk,
+ sizeof(hostlnk),
+ "%s.host",
+ deffilename) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ /* libfoo.so.def is under .libs/ */
+ if (!(slash = strrchr(deffilename,'/')))
+ return SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_LINK_FLOW);
+
+ if (slbt_create_symlink(
+ dctx,ectx,
+ deffilename,
+ hosttag,
+ SLBT_SYMLINK_DEFAULT))
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* libfoo.so.def.{flavor} is under .libs/ */
+ if (!(slash = strrchr(hosttag,'/')))
+ return SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_LINK_FLOW);
+
+ if (slbt_create_symlink(
+ dctx,ectx,
+ ++slash,
+ hostlnk,
+ SLBT_SYMLINK_DEFAULT))
+ return SLBT_NESTED_ERROR(dctx);
+
+ return 0;
+}
diff --git a/src/logic/linkcmd/slbt_linkcmd_implib.c b/src/logic/linkcmd/slbt_linkcmd_implib.c
new file mode 100644
index 0000000..b2c0375
--- /dev/null
+++ b/src/logic/linkcmd/slbt_linkcmd_implib.c
@@ -0,0 +1,157 @@
+/*******************************************************************/
+/* slibtool: a strong libtool implementation, written in C */
+/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */
+/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
+/*******************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+#include <slibtool/slibtool.h>
+#include "slibtool_driver_impl.h"
+#include "slibtool_errinfo_impl.h"
+#include "slibtool_linkcmd_impl.h"
+#include "slibtool_mapfile_impl.h"
+#include "slibtool_metafile_impl.h"
+#include "slibtool_snprintf_impl.h"
+#include "slibtool_symlink_impl.h"
+#include "slibtool_spawn_impl.h"
+#include "slibtool_visibility_impl.h"
+
+slbt_hidden int slbt_exec_link_create_import_library(
+ const struct slbt_driver_ctx * dctx,
+ struct slbt_exec_ctx * ectx,
+ char * impfilename,
+ char * deffilename,
+ char * soname)
+{
+ int fmdso;
+ char ** argv;
+ char ** parg;
+ char ** aarg;
+ char program[PATH_MAX];
+ char as[PATH_MAX];
+
+ /* dlltool or mdso? */
+ if (dctx->cctx->drvflags & SLBT_DRIVER_IMPLIB_DSOMETA)
+ fmdso = 1;
+
+ else if (dctx->cctx->drvflags & SLBT_DRIVER_IMPLIB_IDATA)
+ fmdso = 0;
+
+ else if (!(strcmp(dctx->cctx->host.flavor,"midipix")))
+ fmdso = 1;
+
+ else
+ fmdso = 0;
+
+ /* alternate argument vector */
+ ectx->argv = ectx->altv;
+ ectx->program = program;
+
+ /* tool-specific argument vector */
+ argv = (fmdso)
+ ? (slbt_get_driver_ictx(dctx))->host.mdso_argv
+ : (slbt_get_driver_ictx(dctx))->host.dlltool_argv;
+
+ /* tool-specific argv */
+ aarg = ectx->altv;
+
+ if ((parg = argv)) {
+ ectx->program = argv[0];
+
+ for (; *parg; )
+ *aarg++ = *parg++;
+ } else {
+ *aarg++ = program;
+ }
+
+ /* altv */
+ if (fmdso) {
+ if (!argv)
+ if (slbt_snprintf(program,sizeof(program),
+ "%s",dctx->cctx->host.mdso) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ *aarg++ = "-i";
+ *aarg++ = impfilename;
+ *aarg++ = "-n";
+ *aarg++ = soname;
+ *aarg++ = deffilename;
+ *aarg = 0;
+ } else {
+ if (!argv)
+ if (slbt_snprintf(program,sizeof(program),
+ "%s",dctx->cctx->host.dlltool) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ *aarg++ = "-l";
+ *aarg++ = impfilename;
+ *aarg++ = "-d";
+ *aarg++ = deffilename;
+ *aarg++ = "-D";
+ *aarg++ = soname;
+ *aarg = 0;
+
+ if (dctx->cctx->host.as) {
+ if ((argv = (slbt_get_driver_ictx(dctx))->host.as_argv)) {
+ *aarg++ = "-S";
+ *aarg++ = argv[0];
+
+ for (parg=&argv[1]; *parg; parg++) {
+ *aarg++ = "-f";
+ *aarg++ = *parg;
+ }
+ } else {
+ if (slbt_snprintf(as,sizeof(as),
+ "%s",dctx->cctx->host.as) < 0)
+ return SLBT_BUFFER_ERROR(dctx);
+
+ *aarg++ = "-S";
+ *aarg++ = as;
+ }
+
+ const char * host = dctx->cctx->host.host;
+
+ if (host && (host[0] == 'i')
+ && (host[1] >= '3')
+ && (host[1] <= '6')
+ && (host[2] == '8')
+ && (host[3] == '6')
+ && (host[4] == '-')) {
+ *aarg++ = "-f";
+ *aarg++ = "--32";
+ *aarg++ = "-m";
+ *aarg++ = "i386";
+ *aarg++ = 0;
+ } else {
+ *aarg++ = "-f";
+ *aarg++ = "--64";
+ *aarg++ = "-m";
+ *aarg++ = "i386:x86-64";
+ *aarg = 0;
+ }
+ }
+ }
+
+ /* step output */
+ if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
+ if (slbt_output_link(ectx))
+ return SLBT_NESTED_ERROR(dctx);
+
+ /* dlltool/mdso spawn */
+ if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) {
+ return SLBT_SPAWN_ERROR(dctx);
+
+ } else if (ectx->exitcode) {
+ return SLBT_CUSTOM_ERROR(
+ dctx,
+ fmdso ? SLBT_ERR_MDSO_ERROR : SLBT_ERR_DLLTOOL_ERROR);
+ }
+
+ return 0;
+}