From a126a7f68ef374d65d014226195380eb7dceb634 Mon Sep 17 00:00:00 2001 From: midipix Date: Sun, 28 May 2023 10:14:58 +0000 Subject: driver: added -objectlist support. --- project/common.mk | 1 + project/headers.mk | 1 + src/driver/slbt_driver_ctx.c | 94 +++++++++++++++++++++++++++++++----- src/internal/slibtool_driver_impl.h | 11 +++++ src/internal/slibtool_objlist_impl.c | 82 +++++++++++++++++++++++++++++++ src/internal/slibtool_objlist_impl.h | 10 ++++ src/skin/slbt_skin_default.c | 5 ++ 7 files changed, 191 insertions(+), 13 deletions(-) create mode 100644 src/internal/slibtool_objlist_impl.c create mode 100644 src/internal/slibtool_objlist_impl.h diff --git a/project/common.mk b/project/common.mk index 31d0ff4..2769569 100644 --- a/project/common.mk +++ b/project/common.mk @@ -28,6 +28,7 @@ INTERNAL_SRCS = \ src/internal/$(PACKAGE)_lconf_impl.c \ src/internal/$(PACKAGE)_libmeta_impl.c \ src/internal/$(PACKAGE)_mapfile_impl.c \ + src/internal/$(PACKAGE)_objlist_impl.c \ src/internal/$(PACKAGE)_objmeta_impl.c \ src/internal/$(PACKAGE)_symlink_impl.c \ diff --git a/project/headers.mk b/project/headers.mk index 62b6f52..03bd019 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -12,6 +12,7 @@ INTERNAL_HEADERS = \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_mapfile_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_metafile_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_mkdir_impl.h \ + $(PROJECT_DIR)/src/internal/$(PACKAGE)_objlist_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_readlink_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_spawn_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_symlink_impl.h \ diff --git a/src/driver/slbt_driver_ctx.c b/src/driver/slbt_driver_ctx.c index 4a5bf62..fe5f19e 100644 --- a/src/driver/slbt_driver_ctx.c +++ b/src/driver/slbt_driver_ctx.c @@ -18,6 +18,7 @@ #include #include "slibtool_version.h" #include "slibtool_driver_impl.h" +#include "slibtool_objlist_impl.h" #include "slibtool_errinfo_impl.h" #include "slibtool_lconf_impl.h" #include "argv/argv.h" @@ -171,8 +172,12 @@ static uint32_t slbt_argv_flags(uint32_t flags) return ret; } -static int slbt_free_argv_buffer(struct slbt_split_vector * sargv) +static int slbt_free_argv_buffer( + struct slbt_split_vector * sargv, + struct slbt_obj_list * objlistv) { + struct slbt_obj_list * objlistp; + if (sargv->dargs) free(sargv->dargs); @@ -182,6 +187,15 @@ static int slbt_free_argv_buffer(struct slbt_split_vector * sargv) if (sargv->targv) free(sargv->targv); + if (objlistv) { + for (objlistp=objlistv; objlistp->name; objlistp++) { + free(objlistp->objv); + free(objlistp->addr); + } + + free(objlistv); + } + return -1; } @@ -192,6 +206,7 @@ static int slbt_driver_usage( const struct argv_option ** optv, struct argv_meta * meta, struct slbt_split_vector * sargv, + struct slbt_obj_list * objlistv, int noclr) { char header[512]; @@ -211,7 +226,7 @@ static int slbt_driver_usage( } argv_free(meta); - slbt_free_argv_buffer(sargv); + slbt_free_argv_buffer(sargv,objlistv); return SLBT_USAGE; } @@ -221,6 +236,7 @@ static struct slbt_driver_ctx_impl * slbt_driver_ctx_alloc( const struct slbt_fd_ctx * fdctx, const struct slbt_common_ctx * cctx, struct slbt_split_vector * sargv, + struct slbt_obj_list * objlistv, char ** envp) { struct slbt_driver_ctx_alloc * ictx; @@ -230,7 +246,7 @@ static struct slbt_driver_ctx_impl * slbt_driver_ctx_alloc( size = sizeof(struct slbt_driver_ctx_alloc); if (!(ictx = calloc(1,size))) { - slbt_free_argv_buffer(sargv); + slbt_free_argv_buffer(sargv,objlistv); return 0; } @@ -248,8 +264,11 @@ static struct slbt_driver_ctx_impl * slbt_driver_ctx_alloc( ictx->ctx.errinfp = &ictx->ctx.erriptr[0]; ictx->ctx.erricap = &ictx->ctx.erriptr[--elements]; + ictx->ctx.objlistv = objlistv; + ictx->meta = meta; ictx->ctx.ctx.errv = ictx->ctx.errinfp; + return &ictx->ctx; } @@ -271,16 +290,22 @@ static int slbt_split_argv( char ** argv, uint32_t flags, struct slbt_split_vector * sargv, - int fderr) + struct slbt_obj_list ** aobjlistv, + int fderr, + int fdcwd) { int i; int argc; + int objc; const char * program; char * compiler; char * csysroot; char ** dargv; char ** targv; char ** cargv; + char ** objp; + struct slbt_obj_list * objlistv; + struct slbt_obj_list * objlistp; char * dst; bool flast; bool fcopy; @@ -311,7 +336,7 @@ static int slbt_split_argv( if (!argv[1] && (flags & SLBT_DRIVER_VERBOSITY_USAGE)) return slbt_driver_usage( fderr,program, - 0,optv,0,sargv, + 0,optv,0,sargv,0, !!getenv("NO_COLOR")); /* initial argv scan: ... --mode=xxx ... ... */ @@ -387,6 +412,14 @@ static int slbt_split_argv( else if (!(sargv->dargs = calloc(1,size+1))) return -1; + else if (!(*aobjlistv = calloc(argc >> 1,sizeof(**aobjlistv)))) { + free(sargv->dargv); + free(sargv->dargs); + return -1; + } + + objlistv = *aobjlistv; + objlistp = objlistv; csysroot = 0; for (i=0,flast=false,dargv=sargv->dargv,dst=sargv->dargs; iname = dst; + objlistp++; + fcopy = true; + } else { fcopy = true; } @@ -484,8 +526,16 @@ static int slbt_split_argv( argc = dargv - sargv->dargv; argv = sargv->dargv; + /* iterate through the object list vector: map, parse, store */ + for (objlistp=objlistv; objlistp->name; objlistp++) + if (slbt_objlist_read(fdcwd,objlistp) < 0) + return -1; + + for (objc=0,objlistp=objlistv; objlistp->name; objlistp++) + objc += objlistp->objc; + /* allocate split vectors, account for cargv's added sysroot */ - if ((sargv->targv = calloc(2*(argc+3),sizeof(char *)))) + if ((sargv->targv = calloc(objc + 2*(argc+3),sizeof(char *)))) sargv->cargv = sargv->targv + argc + 2; else return -1; @@ -541,7 +591,7 @@ static int slbt_split_argv( *cargv++ = csysroot; /* remaining vector */ - for (; ifderr)) - return slbt_free_argv_buffer(&sargv); + if (slbt_split_argv(argv,flags,&sargv,&objlistv,fdctx->fderr,fdctx->fdcwd)) + return slbt_free_argv_buffer(&sargv,objlistv); if (!(meta = argv_get( sargv.targv,optv, slbt_argv_flags(flags), fdctx->fderr))) - return slbt_free_argv_buffer(&sargv); + return slbt_free_argv_buffer(&sargv,objlistv); lconf = 0; program = argv_program_name(argv[0]); @@ -1412,7 +1472,7 @@ int slbt_get_driver_ctx( ? slbt_driver_usage( fdctx->fdout,program, entry->arg,optv, - meta,&sargv, + meta,&sargv,objlistv, (cctx.drvflags & SLBT_DRIVER_ANNOTATE_NEVER)) : SLBT_USAGE; } @@ -1777,7 +1837,7 @@ int slbt_get_driver_ctx( cctx.tag = SLBT_TAG_CC; /* driver context */ - if (!(ctx = slbt_driver_ctx_alloc(meta,fdctx,&cctx,&sargv,envp))) + if (!(ctx = slbt_driver_ctx_alloc(meta,fdctx,&cctx,&sargv,objlistv,envp))) return slbt_get_driver_ctx_fail(0,meta); /* ctx */ @@ -1860,6 +1920,7 @@ static void slbt_free_driver_ctx_impl(struct slbt_driver_ctx_alloc * ictx) { struct slbt_error_info ** perr; struct slbt_error_info * erri; + struct slbt_obj_list * objlistp; for (perr=ictx->ctx.errinfp; *perr; perr++) { erri = *perr; @@ -1868,10 +1929,16 @@ static void slbt_free_driver_ctx_impl(struct slbt_driver_ctx_alloc * ictx) free(erri->eany); } - if (ictx->ctx.libname) free(ictx->ctx.libname); + for (objlistp=ictx->ctx.objlistv; objlistp->name; objlistp++) { + free(objlistp->objv); + free(objlistp->addr); + } + + free(ictx->ctx.objlistv); + free(ictx->ctx.dargs); free(ictx->ctx.dargv); free(ictx->ctx.targv); @@ -1879,6 +1946,7 @@ static void slbt_free_driver_ctx_impl(struct slbt_driver_ctx_alloc * ictx) slbt_free_host_params(&ictx->ctx.host); slbt_free_host_params(&ictx->ctx.ahost); argv_free(ictx->meta); + free(ictx); } diff --git a/src/internal/slibtool_driver_impl.h b/src/internal/slibtool_driver_impl.h index 0971fd9..0293308 100644 --- a/src/internal/slibtool_driver_impl.h +++ b/src/internal/slibtool_driver_impl.h @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -54,6 +55,7 @@ enum app_tags { TAG_RPATH, TAG_SYSROOT, TAG_RELEASE, + TAG_OBJECTLIST, TAG_DLOPEN, TAG_DLPREOPEN, TAG_EXPORT_DYNAMIC, @@ -99,12 +101,21 @@ struct slbt_host_strs { char * mdso; }; +struct slbt_obj_list { + const char * name; + void * addr; + size_t size; + int objc; + char ** objv; +}; + struct slbt_driver_ctx_impl { struct slbt_common_ctx cctx; struct slbt_driver_ctx ctx; struct slbt_host_strs host; struct slbt_host_strs ahost; struct slbt_fd_ctx fdctx; + struct slbt_obj_list * objlistv; char * libname; char * dargs; char ** dargv; diff --git a/src/internal/slibtool_objlist_impl.c b/src/internal/slibtool_objlist_impl.c new file mode 100644 index 0000000..be3d994 --- /dev/null +++ b/src/internal/slibtool_objlist_impl.c @@ -0,0 +1,82 @@ +/*******************************************************************/ +/* slibtool: a skinny libtool implementation, written in C */ +/* Copyright (C) 2016--2023 SysDeer Technologies, LLC */ +/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ +/*******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "slibtool_driver_impl.h" +#include "slibtool_errinfo_impl.h" +#include "slibtool_mapfile_impl.h" +#include "slibtool_objlist_impl.h" + +int slbt_objlist_read( + int fdcwd, + struct slbt_obj_list * objlist) +{ + struct slbt_map_info * mapinfo; + int objc; + char ** objv; + char * src; + char * dst; + char * mark; + int skip; + + /* temporarily map the object list */ + if (!(mapinfo = slbt_map_file(fdcwd,objlist->name,SLBT_MAP_INPUT))) + return -1; + + /* object list, cautionary null termination, vector null termination */ + objlist->size = mapinfo->size; + objlist->size++; + objlist->size++; + + if (!(objlist->addr = calloc(1,objlist->size))) { + slbt_unmap_file(mapinfo); + return -1; + } + + /* object list file to normalized object strings */ + objc = 0; + skip = true; + + for (src=mapinfo->addr,dst=objlist->addr; srccap; src++) { + if (!*src || (*src==' ') || (*src=='\n') || (*src=='\r')) { + if (!skip) + *dst++ = 0; + + skip = true; + } else { + *dst++ = *src; + + objc += !!skip; + skip = false; + } + } + + /* object vector */ + objlist->objc = objc; + + if (!(objlist->objv = calloc(++objc,sizeof(char *)))) { + free(objlist->addr); + slbt_unmap_file(mapinfo); + return -1; + } + + for (objv=objlist->objv,mark=objlist->addr; *mark; objv++) { + *objv = mark; + mark += strlen(mark) + 1; + } + + slbt_unmap_file(mapinfo); + + return 0; +} diff --git a/src/internal/slibtool_objlist_impl.h b/src/internal/slibtool_objlist_impl.h new file mode 100644 index 0000000..cb11d07 --- /dev/null +++ b/src/internal/slibtool_objlist_impl.h @@ -0,0 +1,10 @@ +#ifndef SLIBTOOL_OBJLIST_IMPL_H +#define SLIBTOOL_OBJLIST_IMPL_H + +#include "slibtool_driver_impl.h" + +int slbt_objlist_read( + int fdcwd, + struct slbt_obj_list * objlist); + +#endif diff --git a/src/skin/slbt_skin_default.c b/src/skin/slbt_skin_default.c index 3be3b74..5c4d67d 100644 --- a/src/skin/slbt_skin_default.c +++ b/src/skin/slbt_skin_default.c @@ -177,6 +177,11 @@ const struct argv_option slbt_default_options[] = { "followed by the creation of libfoo.so " "as a symlink thereto"}, + {"objectlist", 0,TAG_OBJECTLIST,ARGV_OPTARG_REQUIRED, + ARGV_OPTION_HYBRID_ONLY|ARGV_OPTION_HYBRID_SPACE, + 0,"", + "add the object listed in %s to the object vector}"}, + {"dlopen", 0,TAG_DLOPEN,ARGV_OPTARG_REQUIRED, ARGV_OPTION_HYBRID_ONLY|ARGV_OPTION_HYBRID_SPACE, 0,"", -- cgit v1.2.3