|
|
c4b07e |
/*******************************************************************/
|
|
|
c4b07e |
/* slibtool: a skinny libtool implementation, written in C */
|
|
|
49181b |
/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */
|
|
|
c4b07e |
/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
|
|
|
c4b07e |
/*******************************************************************/
|
|
|
c4b07e |
|
|
|
c4b07e |
#include <stdlib.h>
|
|
|
c4b07e |
#include <stdio.h>
|
|
|
c4b07e |
#include <string.h>
|
|
|
c4b07e |
#include <fcntl.h>
|
|
|
c4b07e |
#include <errno.h>
|
|
|
c4b07e |
#include <sys/stat.h>
|
|
|
c4b07e |
|
|
|
c4b07e |
#include <slibtool/slibtool.h>
|
|
|
c4b07e |
#include "slibtool_driver_impl.h"
|
|
|
c4b07e |
#include "slibtool_errinfo_impl.h"
|
|
|
c4b07e |
#include "slibtool_linkcmd_impl.h"
|
|
|
c4b07e |
#include "slibtool_mapfile_impl.h"
|
|
|
c4b07e |
#include "slibtool_metafile_impl.h"
|
|
|
d4b2a5 |
#include "slibtool_realpath_impl.h"
|
|
|
c4b07e |
#include "slibtool_snprintf_impl.h"
|
|
|
c4b07e |
#include "slibtool_symlink_impl.h"
|
|
|
c4b07e |
#include "slibtool_spawn_impl.h"
|
|
|
4b56de |
#include "slibtool_visibility_impl.h"
|
|
|
c4b07e |
|
|
|
c4b07e |
static int slbt_linkcmd_exit(
|
|
|
c4b07e |
struct slbt_deps_meta * depsmeta,
|
|
|
c4b07e |
int ret)
|
|
|
c4b07e |
{
|
|
|
c4b07e |
if (depsmeta->altv)
|
|
|
c4b07e |
free(depsmeta->altv);
|
|
|
c4b07e |
|
|
|
c4b07e |
if (depsmeta->args)
|
|
|
c4b07e |
free(depsmeta->args);
|
|
|
c4b07e |
|
|
|
c4b07e |
return ret;
|
|
|
c4b07e |
}
|
|
|
c4b07e |
|
|
|
c4b07e |
static int slbt_exec_link_remove_file(
|
|
|
c4b07e |
const struct slbt_driver_ctx * dctx,
|
|
|
c4b07e |
struct slbt_exec_ctx * ectx,
|
|
|
c4b07e |
const char * target)
|
|
|
c4b07e |
{
|
|
|
c4b07e |
int fdcwd;
|
|
|
c4b07e |
|
|
|
c4b07e |
(void)ectx;
|
|
|
c4b07e |
|
|
|
c4b07e |
/* fdcwd */
|
|
|
c4b07e |
fdcwd = slbt_driver_fdcwd(dctx);
|
|
|
c4b07e |
|
|
|
c4b07e |
/* remove target (if any) */
|
|
|
c4b07e |
if (!unlinkat(fdcwd,target,0) || (errno == ENOENT))
|
|
|
c4b07e |
return 0;
|
|
|
c4b07e |
|
|
|
c4b07e |
return SLBT_SYSTEM_ERROR(dctx,0);
|
|
|
c4b07e |
}
|
|
|
c4b07e |
|
|
|
93e38b |
static int slbt_exec_link_remove_dso_files(
|
|
|
93e38b |
const struct slbt_driver_ctx * dctx,
|
|
|
93e38b |
struct slbt_exec_ctx * ectx,
|
|
|
93e38b |
const char * target)
|
|
|
93e38b |
{
|
|
|
93e38b |
int fdcwd;
|
|
|
93e38b |
char * mark;
|
|
|
93e38b |
char * sbuf;
|
|
|
93e38b |
|
|
|
93e38b |
/* fdcwd */
|
|
|
93e38b |
fdcwd = slbt_driver_fdcwd(dctx);
|
|
|
93e38b |
|
|
|
93e38b |
/* remove target (if any) */
|
|
|
93e38b |
if (unlinkat(fdcwd,target,0) && (errno != ENOENT))
|
|
|
93e38b |
return SLBT_SYSTEM_ERROR(dctx,0);
|
|
|
93e38b |
|
|
|
93e38b |
/* remove a previous .disabled placeholder */
|
|
|
93e38b |
sbuf = (slbt_get_exec_ictx(ectx))->sbuf;
|
|
|
93e38b |
mark = sbuf;
|
|
|
93e38b |
mark += sprintf(mark,"%s",target);
|
|
|
93e38b |
strcpy(mark,".disabled");
|
|
|
93e38b |
|
|
|
93e38b |
if (unlinkat(fdcwd,sbuf,0) && (errno != ENOENT))
|
|
|
93e38b |
return SLBT_SYSTEM_ERROR(dctx,0);
|
|
|
93e38b |
|
|
|
93e38b |
return 0;
|
|
|
93e38b |
}
|
|
|
93e38b |
|
|
|
4b56de |
slbt_hidden int slbt_exec_link_create_library(
|
|
|
c4b07e |
const struct slbt_driver_ctx * dctx,
|
|
|
c4b07e |
struct slbt_exec_ctx * ectx,
|
|
|
c4b07e |
const char * dsobasename,
|
|
|
c4b07e |
const char * dsofilename,
|
|
|
4af256 |
const char * relfilename,
|
|
|
4af256 |
bool fardlopen)
|
|
|
c4b07e |
{
|
|
|
c4b07e |
int fdcwd;
|
|
|
c4b07e |
char ** parg;
|
|
|
c4b07e |
char ** xarg;
|
|
|
c4b07e |
char * ccwrap;
|
|
|
c4b07e |
const char * laout;
|
|
|
c4b07e |
const char * dot;
|
|
|
c4b07e |
char cwd [PATH_MAX];
|
|
|
c4b07e |
char output [PATH_MAX];
|
|
|
c4b07e |
char soname [PATH_MAX];
|
|
|
c4b07e |
char symfile[PATH_MAX];
|
|
|
af35a1 |
char mapfile[PATH_MAX];
|
|
|
c4b07e |
struct slbt_deps_meta depsmeta = {0,0,0,0};
|
|
|
c4b07e |
|
|
|
c4b07e |
/* initial state */
|
|
|
f3d47a |
slbt_ectx_reset_arguments(ectx);
|
|
|
c4b07e |
|
|
|
c4b07e |
/* placeholders */
|
|
|
c4b07e |
slbt_reset_placeholders(ectx);
|
|
|
c4b07e |
|
|
|
c4b07e |
/* fdcwd */
|
|
|
c4b07e |
fdcwd = slbt_driver_fdcwd(dctx);
|
|
|
c4b07e |
|
|
|
93e38b |
/* remove previous libfoo.so, libfoo.so.disabled */
|
|
|
93e38b |
if (slbt_exec_link_remove_dso_files(dctx,ectx,ectx->dsofilename) < 0)
|
|
|
93e38b |
return SLBT_NESTED_ERROR(dctx);
|
|
|
93e38b |
|
|
|
c4b07e |
/* input argument adjustment */
|
|
|
c4b07e |
for (parg=ectx->cargv; *parg; parg++)
|
|
|
c4b07e |
slbt_adjust_object_argument(*parg,true,false,fdcwd);
|
|
|
c4b07e |
|
|
|
c4b07e |
/* .deps */
|
|
|
c4b07e |
if (slbt_exec_link_create_dep_file(
|
|
|
c4b07e |
dctx,ectx,ectx->cargv,
|
|
|
c4b07e |
dsofilename,false))
|
|
|
c4b07e |
return slbt_linkcmd_exit(
|
|
|
c4b07e |
&depsmeta,
|
|
|
c4b07e |
SLBT_NESTED_ERROR(dctx));
|
|
|
c4b07e |
|
|
|
c4b07e |
/* linker argument adjustment */
|
|
|
c4b07e |
for (parg=ectx->cargv, xarg=ectx->xargv; *parg; parg++, xarg++)
|
|
|
c4b07e |
if (slbt_adjust_linker_argument(
|
|
|
c4b07e |
dctx,
|
|
|
c4b07e |
*parg,xarg,true,
|
|
|
c4b07e |
dctx->cctx->settings.dsosuffix,
|
|
|
c4b07e |
dctx->cctx->settings.arsuffix,
|
|
|
c4b07e |
&depsmeta) < 0)
|
|
|
c4b07e |
return SLBT_NESTED_ERROR(dctx);
|
|
|
c4b07e |
|
|
|
c4b07e |
/* --no-undefined */
|
|
|
c4b07e |
if (dctx->cctx->drvflags & SLBT_DRIVER_NO_UNDEFINED)
|
|
|
82838b |
*ectx->noundef = slbt_host_group_is_darwin(dctx)
|
|
|
816e28 |
? "-Wl,-undefined,error"
|
|
|
816e28 |
: "-Wl,--no-undefined";
|
|
|
c4b07e |
|
|
|
c4b07e |
/* -soname */
|
|
|
c4b07e |
dot = strrchr(dctx->cctx->output,'.');
|
|
|
c4b07e |
laout = (dot && !strcmp(dot,".la"))
|
|
|
c4b07e |
? dctx->cctx->output
|
|
|
c4b07e |
: 0;
|
|
|
c4b07e |
|
|
|
5d0af4 |
char wl_soname[24];
|
|
|
5d0af4 |
|
|
|
82838b |
if (slbt_host_group_is_darwin(dctx)) {
|
|
|
5d0af4 |
strcpy(wl_soname,"-Wl,-install_name");
|
|
|
5d0af4 |
} else {
|
|
|
5d0af4 |
strcpy(wl_soname,"-Wl,-soname");
|
|
|
5d0af4 |
}
|
|
|
5d0af4 |
|
|
|
c4b07e |
if ((dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_MACHO)) {
|
|
|
c4b07e |
(void)0;
|
|
|
c4b07e |
|
|
|
c4b07e |
} else if (!laout && (dctx->cctx->drvflags & SLBT_DRIVER_MODULE)) {
|
|
|
c4b07e |
if (slbt_snprintf(soname,sizeof(soname),
|
|
|
c4b07e |
"-Wl,%s",dctx->cctx->output) < 0)
|
|
|
c4b07e |
return SLBT_BUFFER_ERROR(dctx);
|
|
|
c4b07e |
|
|
|
5d0af4 |
*ectx->soname = wl_soname;
|
|
|
c4b07e |
*ectx->lsoname = soname;
|
|
|
c4b07e |
|
|
|
c4b07e |
} else if (relfilename && dctx->cctx->verinfo.verinfo) {
|
|
|
c4b07e |
if (slbt_snprintf(soname,sizeof(soname),
|
|
|
c4b07e |
"-Wl,%s%s-%s%s.%d%s",
|
|
|
c4b07e |
ectx->sonameprefix,
|
|
|
c4b07e |
dctx->cctx->libname,
|
|
|
c4b07e |
dctx->cctx->release,
|
|
|
c4b07e |
dctx->cctx->settings.osdsuffix,
|
|
|
c4b07e |
dctx->cctx->verinfo.major,
|
|
|
c4b07e |
dctx->cctx->settings.osdfussix) < 0)
|
|
|
c4b07e |
return SLBT_BUFFER_ERROR(dctx);
|
|
|
c4b07e |
|
|
|
5d0af4 |
*ectx->soname = wl_soname;
|
|
|
c4b07e |
*ectx->lsoname = soname;
|
|
|
c4b07e |
|
|
|
c4b07e |
} else if (relfilename) {
|
|
|
c4b07e |
if (slbt_snprintf(soname,sizeof(soname),
|
|
|
c4b07e |
"-Wl,%s%s-%s%s",
|
|
|
c4b07e |
ectx->sonameprefix,
|
|
|
c4b07e |
dctx->cctx->libname,
|
|
|
c4b07e |
dctx->cctx->release,
|
|
|
c4b07e |
dctx->cctx->settings.dsosuffix) < 0)
|
|
|
c4b07e |
return SLBT_BUFFER_ERROR(dctx);
|
|
|
c4b07e |
|
|
|
5d0af4 |
*ectx->soname = wl_soname;
|
|
|
c4b07e |
*ectx->lsoname = soname;
|
|
|
c4b07e |
|
|
|
c4b07e |
} else if (dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION) {
|
|
|
c4b07e |
if (slbt_snprintf(soname,sizeof(soname),
|
|
|
c4b07e |
"-Wl,%s%s%s",
|
|
|
c4b07e |
ectx->sonameprefix,
|
|
|
c4b07e |
dctx->cctx->libname,
|
|
|
c4b07e |
dctx->cctx->settings.dsosuffix) < 0)
|
|
|
c4b07e |
return SLBT_BUFFER_ERROR(dctx);
|
|
|
c4b07e |
|
|
|
5d0af4 |
*ectx->soname = wl_soname;
|
|
|
c4b07e |
*ectx->lsoname = soname;
|
|
|
c4b07e |
|
|
|
c4b07e |
} else {
|
|
|
c4b07e |
if (slbt_snprintf(soname,sizeof(soname),
|
|
|
c4b07e |
"-Wl,%s%s%s.%d%s",
|
|
|
c4b07e |
ectx->sonameprefix,
|
|
|
c4b07e |
dctx->cctx->libname,
|
|
|
c4b07e |
dctx->cctx->settings.osdsuffix,
|
|
|
c4b07e |
dctx->cctx->verinfo.major,
|
|
|
c4b07e |
dctx->cctx->settings.osdfussix) < 0)
|
|
|
c4b07e |
return SLBT_BUFFER_ERROR(dctx);
|
|
|
c4b07e |
|
|
|
5d0af4 |
*ectx->soname = wl_soname;
|
|
|
c4b07e |
*ectx->lsoname = soname;
|
|
|
c4b07e |
}
|
|
|
c4b07e |
|
|
|
c4b07e |
/* PE: --output-def */
|
|
|
c4b07e |
if (dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_PE) {
|
|
|
c4b07e |
if (slbt_snprintf(symfile,sizeof(symfile),
|
|
|
c4b07e |
"-Wl,%s",
|
|
|
c4b07e |
ectx->deffilename) < 0)
|
|
|
c4b07e |
return SLBT_BUFFER_ERROR(dctx);
|
|
|
c4b07e |
|
|
|
c4b07e |
*ectx->symdefs = "-Wl,--output-def";
|
|
|
c4b07e |
*ectx->symfile = symfile;
|
|
|
c4b07e |
}
|
|
|
c4b07e |
|
|
|
af35a1 |
/* -export-symbols */
|
|
|
af35a1 |
if (dctx->cctx->expsyms) {
|
|
|
af35a1 |
struct slbt_symlist_ctx * sctx;
|
|
|
af35a1 |
sctx = (slbt_get_exec_ictx(ectx))->sctx;
|
|
|
af35a1 |
|
|
|
af35a1 |
if (slbt_util_create_mapfile(sctx,ectx->mapfilename,0644) < 0)
|
|
|
af35a1 |
return SLBT_NESTED_ERROR(dctx);
|
|
|
af35a1 |
|
|
|
af35a1 |
if (slbt_snprintf(mapfile,sizeof(mapfile),
|
|
|
af35a1 |
"-Wl,%s",
|
|
|
af35a1 |
ectx->mapfilename) < 0)
|
|
|
af35a1 |
return SLBT_BUFFER_ERROR(dctx);
|
|
|
af35a1 |
|
|
|
af35a1 |
if (slbt_host_group_is_darwin(dctx)) {
|
|
|
af35a1 |
*ectx->explarg = "-Wl,-exported_symbols_list";
|
|
|
af35a1 |
*ectx->expsyms = mapfile;
|
|
|
af35a1 |
|
|
|
af35a1 |
} else if (slbt_host_group_is_winnt(dctx)) {
|
|
|
af35a1 |
*ectx->expsyms = mapfile;
|
|
|
af35a1 |
} else {
|
|
|
af35a1 |
*ectx->explarg = "-Wl,--version-script";
|
|
|
af35a1 |
*ectx->expsyms = mapfile;
|
|
|
af35a1 |
}
|
|
|
af35a1 |
}
|
|
|
af35a1 |
|
|
|
752c02 |
/* -export-symbols-regex; and see also: */
|
|
|
752c02 |
/* slbt_exec_link_create_expsyms_archive() */
|
|
|
752c02 |
if (dctx->cctx->regex) {
|
|
|
752c02 |
if (slbt_snprintf(mapfile,sizeof(mapfile),
|
|
|
752c02 |
"-Wl,%s",
|
|
|
752c02 |
ectx->mapfilename) < 0)
|
|
|
752c02 |
return SLBT_BUFFER_ERROR(dctx);
|
|
|
752c02 |
|
|
|
752c02 |
if (slbt_host_group_is_darwin(dctx)) {
|
|
|
752c02 |
*ectx->explarg = "-Wl,-exported_symbols_list";
|
|
|
752c02 |
*ectx->expsyms = mapfile;
|
|
|
752c02 |
|
|
|
752c02 |
} else if (slbt_host_group_is_winnt(dctx)) {
|
|
|
752c02 |
*ectx->expsyms = mapfile;
|
|
|
752c02 |
} else {
|
|
|
752c02 |
*ectx->explarg = "-Wl,--version-script";
|
|
|
752c02 |
*ectx->expsyms = mapfile;
|
|
|
752c02 |
}
|
|
|
752c02 |
}
|
|
|
752c02 |
|
|
|
c4b07e |
/* shared/static */
|
|
|
c4b07e |
if (dctx->cctx->drvflags & SLBT_DRIVER_ALL_STATIC) {
|
|
|
c4b07e |
*ectx->dpic = "-static";
|
|
|
c4b07e |
} else if (dctx->cctx->settings.picswitch) {
|
|
|
c4b07e |
*ectx->dpic = "-shared";
|
|
|
c4b07e |
*ectx->fpic = dctx->cctx->settings.picswitch;
|
|
|
c4b07e |
} else {
|
|
|
c4b07e |
*ectx->dpic = "-shared";
|
|
|
c4b07e |
}
|
|
|
c4b07e |
|
|
|
c4b07e |
/* output */
|
|
|
c4b07e |
if (!laout && dctx->cctx->drvflags & SLBT_DRIVER_MODULE) {
|
|
|
c4b07e |
strcpy(output,dctx->cctx->output);
|
|
|
c4b07e |
} else if (relfilename) {
|
|
|
c4b07e |
strcpy(output,relfilename);
|
|
|
c4b07e |
} else if (dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION) {
|
|
|
c4b07e |
strcpy(output,dsofilename);
|
|
|
c4b07e |
} else {
|
|
|
c4b07e |
if (slbt_snprintf(output,sizeof(output),
|
|
|
c4b07e |
"%s%s.%d.%d.%d%s",
|
|
|
c4b07e |
dsobasename,
|
|
|
c4b07e |
dctx->cctx->settings.osdsuffix,
|
|
|
c4b07e |
dctx->cctx->verinfo.major,
|
|
|
c4b07e |
dctx->cctx->verinfo.minor,
|
|
|
c4b07e |
dctx->cctx->verinfo.revision,
|
|
|
c4b07e |
dctx->cctx->settings.osdfussix) < 0)
|
|
|
c4b07e |
return SLBT_BUFFER_ERROR(dctx);
|
|
|
c4b07e |
}
|
|
|
c4b07e |
|
|
|
4af256 |
/* output marks */
|
|
|
c4b07e |
*ectx->lout[0] = "-o";
|
|
|
c4b07e |
*ectx->lout[1] = output;
|
|
|
c4b07e |
|
|
|
c4b07e |
/* ldrpath */
|
|
|
c4b07e |
if (dctx->cctx->host.ldrpath) {
|
|
|
c4b07e |
if (slbt_exec_link_remove_file(dctx,ectx,ectx->rpathfilename))
|
|
|
c4b07e |
return SLBT_NESTED_ERROR(dctx);
|
|
|
c4b07e |
|
|
|
c4b07e |
if (slbt_create_symlink(
|
|
|
c4b07e |
dctx,ectx,
|
|
|
c4b07e |
dctx->cctx->host.ldrpath,
|
|
|
c4b07e |
ectx->rpathfilename,
|
|
|
c4b07e |
SLBT_SYMLINK_LITERAL))
|
|
|
c4b07e |
return SLBT_NESTED_ERROR(dctx);
|
|
|
c4b07e |
}
|
|
|
c4b07e |
|
|
|
c4b07e |
/* cwd */
|
|
|
d4b2a5 |
if (slbt_realpath(fdcwd,".",O_DIRECTORY,cwd,sizeof(cwd)))
|
|
|
c4b07e |
return SLBT_SYSTEM_ERROR(dctx,0);
|
|
|
c4b07e |
|
|
|
c4b07e |
/* .libs/libfoo.so --> -L.libs -lfoo */
|
|
|
c4b07e |
if (slbt_exec_link_adjust_argument_vector(
|
|
|
c4b07e |
dctx,ectx,&depsmeta,cwd,true))
|
|
|
c4b07e |
return SLBT_NESTED_ERROR(dctx);
|
|
|
c4b07e |
|
|
|
c4b07e |
/* using alternate argument vector */
|
|
|
c4b07e |
ccwrap = (char *)dctx->cctx->ccwrap;
|
|
|
c4b07e |
ectx->argv = depsmeta.altv;
|
|
|
c4b07e |
ectx->program = ccwrap ? ccwrap : depsmeta.altv[0];
|
|
|
c4b07e |
|
|
|
c4b07e |
/* sigh */
|
|
|
c4b07e |
if (slbt_exec_link_finalize_argument_vector(dctx,ectx))
|
|
|
c4b07e |
return SLBT_NESTED_ERROR(dctx);
|
|
|
c4b07e |
|
|
|
4af256 |
/* all done? */
|
|
|
4af256 |
if (fardlopen)
|
|
|
4af256 |
return 0;
|
|
|
4af256 |
|
|
|
c4b07e |
/* step output */
|
|
|
c4b07e |
if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
|
|
|
33a569 |
if (slbt_output_link(ectx))
|
|
|
c4b07e |
return slbt_linkcmd_exit(
|
|
|
c4b07e |
&depsmeta,
|
|
|
c4b07e |
SLBT_NESTED_ERROR(dctx));
|
|
|
c4b07e |
|
|
|
c4b07e |
/* spawn */
|
|
|
c4b07e |
if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) {
|
|
|
c4b07e |
return slbt_linkcmd_exit(
|
|
|
c4b07e |
&depsmeta,
|
|
|
c4b07e |
SLBT_SPAWN_ERROR(dctx));
|
|
|
c4b07e |
|
|
|
c4b07e |
} else if (ectx->exitcode) {
|
|
|
c4b07e |
return slbt_linkcmd_exit(
|
|
|
c4b07e |
&depsmeta,
|
|
|
c4b07e |
SLBT_CUSTOM_ERROR(
|
|
|
c4b07e |
dctx,
|
|
|
c4b07e |
SLBT_ERR_LINK_ERROR));
|
|
|
c4b07e |
}
|
|
|
c4b07e |
|
|
|
c4b07e |
return slbt_linkcmd_exit(&depsmeta,0);
|
|
|
c4b07e |
}
|