From 2f4c03923b250a3bbf96188ceb91979841a95e91 Mon Sep 17 00:00:00 2001 From: midipix Date: Fri, 3 Aug 2018 23:16:42 +0000 Subject: library interfaces: archive generation: simplify, re-implement with mmap. --- include/mdso/mdso.h | 4 +-- src/archive/mdso_argen_common.c | 46 ++++++++++++++++------------------- src/helper/mdso_create_output.c | 33 ++++++------------------- src/util/mdso_create_implib_archive.c | 27 ++++++++++---------- 4 files changed, 43 insertions(+), 67 deletions(-) diff --git a/include/mdso/mdso.h b/include/mdso/mdso.h index 7927896..f8228e5 100644 --- a/include/mdso/mdso.h +++ b/include/mdso/mdso.h @@ -158,9 +158,9 @@ mdso_api int mdso_get_driver_fdctx (const struct mdso_driver_ctx *, struct mdso_api int mdso_set_driver_fdctx (struct mdso_driver_ctx *, const struct mdso_fd_ctx *); /* helper api */ -mdso_api FILE*mdso_create_archive (const struct mdso_driver_ctx *, const char * arname); mdso_api int mdso_create_asmsrc (const struct mdso_driver_ctx *, const char * asmname); mdso_api int mdso_create_object (const struct mdso_driver_ctx *, struct mdso_object *); +mdso_api int mdso_create_archive (const struct mdso_driver_ctx *, struct mdso_object *); /* utility api */ mdso_api int mdso_main (int, char **, char **, const struct mdso_fd_ctx *); @@ -192,7 +192,7 @@ mdso_api int mdso_objgen_symfn (const struct mdso_driver_ctx *, const c mdso_api int mdso_argen_common (const struct mdso_driver_ctx *, const char **, const int *, - FILE *, struct mdso_object *); + struct mdso_object *); /* package info */ mdso_api const struct mdso_source_version * mdso_source_version(void); diff --git a/src/archive/mdso_argen_common.c b/src/archive/mdso_argen_common.c index 62cca47..9cd80d6 100644 --- a/src/archive/mdso_argen_common.c +++ b/src/archive/mdso_argen_common.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "mdso_errinfo_impl.h" @@ -50,7 +51,6 @@ int mdso_argen_common( const struct mdso_driver_ctx * dctx, const char ** symv, const int * stype, - FILE * fout, struct mdso_object * vobj) { int ret; @@ -63,6 +63,7 @@ int mdso_argen_common( uint32_t mapstrsnum; uint32_t mapstrslen; uint32_t symidx; + void * addr; unsigned char * ar; unsigned char * mark; unsigned char * idx; @@ -154,26 +155,26 @@ int mdso_argen_common( objlen |= 15; objlen ^= 15; - if (vobj && vobj->addr && (vobj->size < objlen)) + if (vobj->addr && (vobj->size < objlen)) return MDSO_BUFFER_ERROR(dctx); - if (vobj && !vobj->addr) { - vobj->size = objlen; + if ((addr = vobj->addr)) { + (void)0; + + } else { + vobj->size = objlen; vobj->mapstrslen = mapstrslen; vobj->mapstrsnum = mapstrsnum; - return 0; - } - - if (vobj) - ar = (unsigned char *)vobj->addr; - else if (!(ar = calloc(1,objlen))) { - if (aobj != sobj) - free(aobj); + if (!vobj->name) + return 0; - return MDSO_SYSTEM_ERROR(dctx); + else if (mdso_create_archive(dctx,vobj) < 0) + return MDSO_NESTED_ERROR(dctx); } + ar = (unsigned char *)vobj->addr; + /* archive signature */ memcpy(ar,"!\n",8); mark = &ar[8]; @@ -274,23 +275,18 @@ int mdso_argen_common( if (aobj != sobj) free(aobj); - if (!vobj) - free(ar); - return ret; } - /* tada */ - if (fout) - ret = fwrite(ar,objlen,1,fout); - + /* aobj */ if (aobj != sobj) free(aobj); - if (!vobj) - free(ar); + /* fs object unmap */ + if (!addr) + munmap(vobj->addr,vobj->size); + + /* tada */ + return 0; - return (ret == 0) - ? MDSO_FILE_ERROR(dctx) - : 0; } diff --git a/src/helper/mdso_create_output.c b/src/helper/mdso_create_output.c index 3b9fe5d..d508cc3 100644 --- a/src/helper/mdso_create_output.c +++ b/src/helper/mdso_create_output.c @@ -60,25 +60,6 @@ static int mdso_map_output( return 0; } -static FILE * mdso_create_output_stream( - const struct mdso_driver_ctx * dctx, - const char * name) -{ - int fdout; - FILE * fout; - - if ((fdout = mdso_create_output(dctx,name)) < 0) - return 0; - - if (!(fout = fdopen(fdout,"w"))) { - close(fdout); - MDSO_SYSTEM_ERROR(dctx); - return 0; - } - - return fout; -} - static int mdso_create_mapped_output( const struct mdso_driver_ctx * dctx, struct mdso_object * obj) @@ -94,13 +75,6 @@ static int mdso_create_mapped_output( return 0; } -FILE * mdso_create_archive( - const struct mdso_driver_ctx * dctx, - const char * arname) -{ - return mdso_create_output_stream(dctx,arname); -} - int mdso_create_asmsrc( const struct mdso_driver_ctx * dctx, const char * asmname) @@ -121,3 +95,10 @@ int mdso_create_object( { return mdso_create_mapped_output(dctx,obj); } + +int mdso_create_archive( + const struct mdso_driver_ctx * dctx, + struct mdso_object * obj) +{ + return mdso_create_mapped_output(dctx,obj); +} diff --git a/src/util/mdso_create_implib_archive.c b/src/util/mdso_create_implib_archive.c index e5e5d01..11373b4 100644 --- a/src/util/mdso_create_implib_archive.c +++ b/src/util/mdso_create_implib_archive.c @@ -15,8 +15,7 @@ static void mdso_free_uctx_vector( struct mdso_unit_ctx ** uctxv, const char ** symv, - int * stype, - FILE * fout) + int * stype) { struct mdso_unit_ctx ** puctx; @@ -30,16 +29,14 @@ static void mdso_free_uctx_vector( mdso_free_unit_ctx(*puctx); free(uctxv); - fclose(fout); } int mdso_create_implib_archive(const struct mdso_driver_ctx * dctx) { - int ret; - FILE * fout; size_t nsym; struct mdso_unit_ctx ** uctxv; struct mdso_unit_ctx ** puctx; + struct mdso_object obj; const char * const * dsym; const char ** unit; const char ** psym; @@ -55,12 +52,9 @@ int mdso_create_implib_archive(const struct mdso_driver_ctx * dctx) if (!(uctxv = calloc(++unit - dctx->units,sizeof(*uctxv)))) return MDSO_SYSTEM_ERROR(dctx); - if (!(fout = mdso_create_archive(dctx,dctx->cctx->implib))) - return MDSO_NESTED_ERROR(dctx); - for (puctx=uctxv,unit=dctx->units; *unit; unit++) { if (mdso_get_unit_ctx(dctx,*unit,puctx++)) { - mdso_free_uctx_vector(uctxv,0,0,fout); + mdso_free_uctx_vector(uctxv,0,0); return MDSO_NESTED_ERROR(dctx); } } @@ -70,12 +64,12 @@ int mdso_create_implib_archive(const struct mdso_driver_ctx * dctx) nsym++; if (!(symv = calloc(nsym+1,sizeof(const char *)))) { - mdso_free_uctx_vector(uctxv,0,0,fout); + mdso_free_uctx_vector(uctxv,0,0); return MDSO_SYSTEM_ERROR(dctx); } if (!(stype = calloc(nsym+1,sizeof(int)))) { - mdso_free_uctx_vector(uctxv,symv,0,fout); + mdso_free_uctx_vector(uctxv,symv,0); return MDSO_SYSTEM_ERROR(dctx); } @@ -86,8 +80,13 @@ int mdso_create_implib_archive(const struct mdso_driver_ctx * dctx) } } - ret = mdso_argen_common(dctx,symv,stype,fout,0); - mdso_free_uctx_vector(uctxv,symv,stype,fout); + memset(&obj,0,sizeof(obj)); + obj.name = dctx->cctx->implib; + + if (mdso_argen_common(dctx,symv,stype,&obj) < 0) { + mdso_free_uctx_vector(uctxv,symv,stype); + return MDSO_NESTED_ERROR(dctx); + } - return ret ? MDSO_NESTED_ERROR(dctx) : 0; + return 0; } -- cgit v1.2.3