|
|
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"
|
|
|
c4b07e |
#include "slibtool_snprintf_impl.h"
|
|
|
c4b07e |
#include "slibtool_symlink_impl.h"
|
|
|
c4b07e |
#include "slibtool_spawn_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 |
|
|
|
c4b07e |
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,
|
|
|
c4b07e |
const char * relfilename)
|
|
|
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];
|
|
|
c4b07e |
struct slbt_deps_meta depsmeta = {0,0,0,0};
|
|
|
c4b07e |
|
|
|
c4b07e |
/* initial state */
|
|
|
c4b07e |
slbt_reset_arguments(ectx);
|
|
|
c4b07e |
|
|
|
c4b07e |
/* placeholders */
|
|
|
c4b07e |
slbt_reset_placeholders(ectx);
|
|
|
c4b07e |
|
|
|
c4b07e |
/* fdcwd */
|
|
|
c4b07e |
fdcwd = slbt_driver_fdcwd(dctx);
|
|
|
c4b07e |
|
|
|
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)
|
|
|
36d22a |
*ectx->noundef = !strcmp(dctx->cctx->host.flavor,"darwin")
|
|
|
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 |
|
|
|
36d22a |
if (!strcmp(dctx->cctx->host.flavor,"darwin")) {
|
|
|
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 |
|
|
|
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 |
|
|
|
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 */
|
|
|
c4b07e |
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 |
|
|
|
c4b07e |
/* step output */
|
|
|
c4b07e |
if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
|
|
|
c4b07e |
if (slbt_output_link(dctx,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 |
}
|