firasuke / cross / slibtool

Forked from cross/slibtool 3 months ago
Clone

Blame src/util/slbt_create_mapfile.c

f7b3fd
/*******************************************************************/
f7b3fd
/*  slibtool: a skinny libtool implementation, written in C        */
f7b3fd
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
f7b3fd
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
f7b3fd
/*******************************************************************/
f7b3fd
f7b3fd
#include <time.h>
f7b3fd
#include <locale.h>
f7b3fd
#include <regex.h>
f7b3fd
#include <inttypes.h>
f7b3fd
#include <slibtool/slibtool.h>
f7b3fd
#include <slibtool/slibtool_output.h>
f7b3fd
#include "slibtool_driver_impl.h"
f7b3fd
#include "slibtool_dprintf_impl.h"
f7b3fd
#include "slibtool_errinfo_impl.h"
f7b3fd
#include "slibtool_ar_impl.h"
f7b3fd
f7b3fd
/****************************************************/
f7b3fd
/* Generate a linker version script (aka mapfile)   */
f7b3fd
/* that could be passed to the host linker.         */
f7b3fd
/*                                                  */
f7b3fd
/* Since the provided symbol list is host-neautral, */
f7b3fd
/* prepend symbol names with an underscore whenever */
f7b3fd
/* necessary.                                       */
f7b3fd
/****************************************************/
f7b3fd
f7b3fd
static int slbt_is_strong_coff_symbol(const char * sym)
f7b3fd
{
f7b3fd
	return strncmp(sym,"__imp_",6) && strncmp(sym,".weak.",6);
f7b3fd
}
f7b3fd
f7b3fd
static int slbt_util_output_mapfile_impl(
f7b3fd
	const struct slbt_driver_ctx *  dctx,
f7b3fd
	const struct slbt_symlist_ctx * sctx,
f7b3fd
	int                             fdout)
f7b3fd
{
f7b3fd
	bool            fcoff;
f7b3fd
	bool            fmach;
f7b3fd
	const char **   symv;
f7b3fd
	const char **   symstrv;
f7b3fd
f7b3fd
	fmach = !strcmp(dctx->cctx->host.flavor,"darwin");
f7b3fd
f7b3fd
	fcoff = !strcmp(dctx->cctx->host.flavor,"midipix");
f7b3fd
	fcoff = fcoff || !strcmp(dctx->cctx->host.flavor,"cygwin");
f7b3fd
	fcoff = fcoff || !strcmp(dctx->cctx->host.flavor,"mingw");
f7b3fd
	fcoff = fcoff || !strcmp(dctx->cctx->host.flavor,"msys2");
f7b3fd
f7b3fd
	if (fcoff) {
f7b3fd
		if (slbt_dprintf(fdout,"EXPORTS\n") < 0)
f7b3fd
			return SLBT_SYSTEM_ERROR(dctx,0);
f7b3fd
	} else if (fmach) {
f7b3fd
		if (slbt_dprintf(fdout,"# export_list, underscores prepended\n") < 0)
f7b3fd
			return SLBT_SYSTEM_ERROR(dctx,0);
f7b3fd
	} else {
f7b3fd
		if (slbt_dprintf(fdout,"{\n" "\t" "global:\n") < 0)
f7b3fd
			return SLBT_SYSTEM_ERROR(dctx,0);
f7b3fd
	}
f7b3fd
f7b3fd
	symstrv = sctx->symstrv;
f7b3fd
f7b3fd
	for (symv=symstrv; *symv; symv++) {
f7b3fd
		if (!fcoff || slbt_is_strong_coff_symbol(*symv)) {
f7b3fd
			if (fcoff) {
f7b3fd
				if (slbt_dprintf(fdout,"%s\n",*symv) < 0)
f7b3fd
					return SLBT_SYSTEM_ERROR(dctx,0);
f7b3fd
			} else if (fmach) {
f7b3fd
				if (slbt_dprintf(fdout,"_%s\n",*symv) < 0)
f7b3fd
					return SLBT_SYSTEM_ERROR(dctx,0);
f7b3fd
			} else {
f7b3fd
				if (slbt_dprintf(fdout,"\t\t%s;\n",*symv) < 0)
f7b3fd
					return SLBT_SYSTEM_ERROR(dctx,0);
f7b3fd
			}
f7b3fd
		}
f7b3fd
	}
f7b3fd
f7b3fd
	if (!fcoff && !fmach)
f7b3fd
		if (slbt_dprintf(fdout,"\n\t" "local:\n" "\t\t*;\n" "};\n") < 0)
f7b3fd
			return SLBT_SYSTEM_ERROR(dctx,0);
f7b3fd
f7b3fd
	return 0;
f7b3fd
}
f7b3fd
f7b3fd
f7b3fd
static int slbt_util_create_mapfile_impl(
f7b3fd
	const struct slbt_symlist_ctx *   sctx,
f7b3fd
	const char *                      path,
f7b3fd
	mode_t                            mode)
f7b3fd
{
f7b3fd
	int                             ret;
f7b3fd
	const struct slbt_driver_ctx *  dctx;
f7b3fd
	struct slbt_fd_ctx              fdctx;
f7b3fd
	int                             fdout;
f7b3fd
f7b3fd
	dctx = (slbt_get_symlist_ictx(sctx))->dctx;
f7b3fd
f7b3fd
	if (slbt_lib_get_driver_fdctx(dctx,&fdctx) < 0)
f7b3fd
		return SLBT_NESTED_ERROR(dctx);
f7b3fd
f7b3fd
	if (path) {
f7b3fd
		if ((fdout = openat(
f7b3fd
				fdctx.fdcwd,path,
f7b3fd
				O_WRONLY|O_CREAT|O_TRUNC,
f7b3fd
				mode)) < 0)
f7b3fd
			return SLBT_SYSTEM_ERROR(dctx,0);
f7b3fd
	} else {
f7b3fd
		fdout = fdctx.fdout;
f7b3fd
	}
f7b3fd
f7b3fd
	ret = slbt_util_output_mapfile_impl(
f7b3fd
		dctx,sctx,fdout);
f7b3fd
f7b3fd
	if (path) {
f7b3fd
		close(fdout);
f7b3fd
	}
f7b3fd
f7b3fd
	return ret;
f7b3fd
}
f7b3fd
f7b3fd
f7b3fd
int slbt_util_create_mapfile(
f7b3fd
	const struct slbt_symlist_ctx *   sctx,
f7b3fd
	const char *                      path,
f7b3fd
	mode_t                            mode)
f7b3fd
{
f7b3fd
	return slbt_util_create_mapfile_impl(sctx,path,mode);
f7b3fd
}