Blame src/arbits/slbt_archive_symfile.c

77c7bc
/*******************************************************************/
eac61a
/*  slibtool: a strong libtool implementation, written in C        */
77c7bc
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
77c7bc
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
77c7bc
/*******************************************************************/
77c7bc
77c7bc
#include <time.h>
77c7bc
#include <locale.h>
77c7bc
#include <regex.h>
77c7bc
#include <inttypes.h>
77c7bc
#include <slibtool/slibtool.h>
77c7bc
#include <slibtool/slibtool_output.h>
77c7bc
#include "slibtool_driver_impl.h"
77c7bc
#include "slibtool_dprintf_impl.h"
77c7bc
#include "slibtool_errinfo_impl.h"
77c7bc
#include "slibtool_pecoff_impl.h"
77c7bc
#include "slibtool_ar_impl.h"
77c7bc
77c7bc
/********************************************************/
77c7bc
/* Generate a symbol list that could be used by a build */
77c7bc
/* system for code generation or any other purpose.     */
77c7bc
/*                                                      */
77c7bc
/* The output matches the -Wposix variant of symbol     */
77c7bc
/* printing, but the interface should be kept separate  */
77c7bc
/* since the _au_output_ functions implement several    */
77c7bc
/* output format, whereas here only a single, plain     */
77c7bc
/* output is expected.                                  */
77c7bc
/********************************************************/
77c7bc
77c7bc
static int slbt_ar_output_symfile_impl(
77c7bc
	const struct slbt_driver_ctx *  dctx,
77c7bc
	struct slbt_archive_meta_impl * mctx,
77c7bc
	int                             fdout)
77c7bc
{
77c7bc
	bool            fsort;
77c7bc
	bool            fcoff;
7ff943
	const char *    dot;
7ff943
	const char *    mark;
77c7bc
	const char *    regex;
77c7bc
	const char **   symv;
77c7bc
	const char **   symstrv;
77c7bc
	regex_t         regctx;
77c7bc
	regmatch_t      pmatch[2] = {{0,0},{0,0}};
7ff943
	char            strbuf[4096];
77c7bc
77c7bc
	fsort = !(dctx->cctx->fmtflags & SLBT_OUTPUT_ARCHIVE_NOSORT);
77c7bc
77c7bc
	fcoff  = slbt_host_objfmt_is_coff(dctx);
77c7bc
	fcoff |= (mctx->ofmtattr & AR_OBJECT_ATTR_COFF);
77c7bc
77c7bc
	if (fsort && !mctx->mapstrv)
77c7bc
		if (slbt_update_mapstrv(dctx,mctx) < 0)
77c7bc
			return SLBT_NESTED_ERROR(dctx);
77c7bc
77c7bc
	if ((regex = dctx->cctx->regex))
77c7bc
		if (regcomp(&regctx,regex,REG_EXTENDED|REG_NEWLINE))
77c7bc
			return SLBT_CUSTOM_ERROR(
77c7bc
				dctx,
77c7bc
				SLBT_ERR_FLOW_ERROR);
77c7bc
77c7bc
	symstrv = fsort ? mctx->mapstrv : mctx->symstrv;
77c7bc
77c7bc
	for (symv=symstrv; *symv; symv++) {
77c7bc
		if (!fcoff || slbt_is_strong_coff_symbol(*symv)) {
77c7bc
			if (!regex || !regexec(&regctx,*symv,1,pmatch,0)) {
77c7bc
				if (slbt_dprintf(fdout,"%s\n",*symv) < 0)
77c7bc
					return SLBT_SYSTEM_ERROR(dctx,0);
77c7bc
			}
7ff943
7ff943
		/* coff weak symbols: expsym = .weak.alias.strong */
7ff943
		} else if (fcoff && !strncmp(*symv,".weak.",6)) {
7ff943
			mark = &(*symv)[6];
7ff943
			dot  = strchr(mark,'.');
7ff943
7ff943
			strncpy(strbuf,mark,dot-mark);
7ff943
			strbuf[dot-mark] = '\0';
7ff943
7ff943
			if (!regex || !regexec(&regctx,strbuf,1,pmatch,0))
7ff943
				if (slbt_dprintf(fdout,"    %s = %s\n",strbuf,++dot) < 0)
7ff943
					return SLBT_SYSTEM_ERROR(dctx,0);
77c7bc
		}
77c7bc
	}
77c7bc
77c7bc
	if (regex)
77c7bc
		regfree(&regctx);
77c7bc
77c7bc
	return 0;
77c7bc
}
77c7bc
77c7bc
77c7bc
static int slbt_ar_create_symfile_impl(
77c7bc
	const struct slbt_archive_meta *  meta,
77c7bc
	const char *                      path,
77c7bc
	mode_t                            mode)
77c7bc
{
77c7bc
	int                             ret;
77c7bc
	struct slbt_archive_meta_impl * mctx;
77c7bc
	const struct slbt_driver_ctx *  dctx;
77c7bc
	struct slbt_fd_ctx              fdctx;
77c7bc
	int                             fdout;
77c7bc
77c7bc
	mctx = slbt_archive_meta_ictx(meta);
77c7bc
	dctx = (slbt_archive_meta_ictx(meta))->dctx;
77c7bc
77c7bc
	if (slbt_lib_get_driver_fdctx(dctx,&fdctx) < 0)
77c7bc
		return SLBT_NESTED_ERROR(dctx);
77c7bc
77c7bc
	if (!meta->a_memberv)
77c7bc
		return 0;
77c7bc
77c7bc
	if (path) {
77c7bc
		if ((fdout = openat(
77c7bc
				fdctx.fdcwd,path,
77c7bc
				O_WRONLY|O_CREAT|O_TRUNC,
77c7bc
				mode)) < 0)
926d54
			return SLBT_SYSTEM_ERROR(dctx,path);
77c7bc
	} else {
77c7bc
		fdout = fdctx.fdout;
77c7bc
	}
77c7bc
77c7bc
	ret = slbt_ar_output_symfile_impl(
77c7bc
		dctx,mctx,fdout);
77c7bc
77c7bc
	if (path) {
77c7bc
		close(fdout);
77c7bc
	}
77c7bc
77c7bc
	return ret;
77c7bc
}
77c7bc
77c7bc
77c7bc
int slbt_ar_create_symfile(
77c7bc
	const struct slbt_archive_meta *  meta,
77c7bc
	const char *                      path,
77c7bc
	mode_t                            mode)
77c7bc
{
77c7bc
	return slbt_ar_create_symfile_impl(meta,path,mode);
77c7bc
}