Blame src/util/slbt_create_mapfile.c

f7b3fd
/*******************************************************************/
eac61a
/*  slibtool: a strong 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"
279932
#include "slibtool_pecoff_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_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;
a9bad5
	const char *    dot;
a9bad5
	const char *    mark;
f7b3fd
	const char **   symv;
f7b3fd
	const char **   symstrv;
a9bad5
	char            strbuf[4096];
f7b3fd
cdc3c6
	fcoff = slbt_host_objfmt_is_coff(dctx);
cdc3c6
	fmach = slbt_host_objfmt_is_macho(dctx);
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++) {
a9bad5
		if (fcoff && slbt_is_strong_coff_symbol(*symv)) {
a9bad5
			if (slbt_dprintf(fdout,"%s\n",*symv) < 0)
a9bad5
				return SLBT_SYSTEM_ERROR(dctx,0);
a9bad5
a9bad5
		} else if (fcoff && !strncmp(*symv,".weak.",6)) {
a9bad5
			mark = &(*symv)[6];
a9bad5
			dot  = strchr(mark,'.');
a9bad5
a9bad5
			strncpy(strbuf,mark,dot-mark);
a9bad5
			strbuf[dot-mark] = '\0';
a9bad5
a9bad5
			if (slbt_dprintf(fdout,"    %s = %s\n",strbuf,++dot) < 0)
a9bad5
				return SLBT_SYSTEM_ERROR(dctx,0);
a9bad5
a9bad5
		} else if (fmach) {
a9bad5
			if (slbt_dprintf(fdout,"_%s\n",*symv) < 0)
a9bad5
				return SLBT_SYSTEM_ERROR(dctx,0);
a9bad5
a9bad5
		} else {
a9bad5
			if (slbt_dprintf(fdout,"\t\t%s;\n",*symv) < 0)
a9bad5
				return SLBT_SYSTEM_ERROR(dctx,0);
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
}