diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/archive/mdso_argen_common.c | 62 | ||||
-rw-r--r-- | src/driver/mdso_unit_ctx.c | 28 | ||||
-rw-r--r-- | src/internal/mdso_driver_impl.h | 1 | ||||
-rw-r--r-- | src/logic/mdso_asmgen_symfn.c | 43 | ||||
-rw-r--r-- | src/object/mdso_objgen_symfn.c | 241 | ||||
-rw-r--r-- | src/util/mdso_create_implib_archive.c | 24 | ||||
-rw-r--r-- | src/util/mdso_create_implib_objects.c | 10 | ||||
-rw-r--r-- | src/util/mdso_create_implib_sources.c | 17 |
8 files changed, 17 insertions, 409 deletions
diff --git a/src/archive/mdso_argen_common.c b/src/archive/mdso_argen_common.c index 5b534cf..a2553c0 100644 --- a/src/archive/mdso_argen_common.c +++ b/src/archive/mdso_argen_common.c @@ -51,14 +51,11 @@ static void mdso_write_big_endian_long(unsigned char * ch, uint32_t val) int mdso_argen_common( const struct mdso_driver_ctx * dctx, const char ** symv, - const int * stype, struct mdso_object * vobj) { int ret; const char ** psym; - int nsym; - int nobj; - int ndata; + intptr_t nobj; uint32_t objlen; uint32_t hdrlen; uint32_t mapstrsnum; @@ -78,16 +75,16 @@ int mdso_argen_common( /* init */ memset(sobj,0,sizeof(sobj)); - for (nsym=0,ndata=0,psym=symv; *psym; psym++,nsym++) - ndata += (stype[psym-symv] == MDSO_SYMBOL_TYPE_DATA); + for (psym=symv; *psym; psym++) + (void)0; - if ((nobj = 1 + (2*nsym) - ndata) < 256) + if ((nobj = psym - symv) < 255) aobj = sobj; else if (nobj > 1024*1024) return MDSO_CUSTOM_ERROR(dctx,MDSO_ERR_INVALID_VECTOR); - else if (!(aobj = calloc(1,nobj*sizeof(*aobj)))) + else if (!(aobj = calloc(1,++nobj*sizeof(*aobj)))) return MDSO_SYSTEM_ERROR(dctx); /* objlen: archive signature, index header */ @@ -107,24 +104,9 @@ int mdso_argen_common( mapstrslen = aobj->mapstrslen; mapstrsnum = aobj->mapstrsnum; - /* objlen: symfn, symentry */ + /* objlen: symentry */ for (psym=symv,pobj=&aobj[1]; *psym && !ret; psym++) { - if (stype[psym-symv] == MDSO_SYMBOL_TYPE_CODE) { - ret = mdso_objgen_symfn(dctx,*psym,pobj); - - pobj->size += 1; - pobj->size |= 1; - pobj->size ^= 1; - - objlen += pobj->size; - objlen += sizeof(struct pe_raw_archive_common_hdr); - - mapstrslen += pobj->mapstrslen; - mapstrsnum += pobj->mapstrsnum; - pobj++; - } - - ret |= mdso_objgen_symentry(dctx,*psym,pobj); + ret = mdso_objgen_symentry(dctx,*psym,pobj); pobj->size += 1; pobj->size |= 1; @@ -221,36 +203,8 @@ int mdso_argen_common( mark += aobj->arhdrlen + aobj->size; mapstrs += aobj->mapstrslen; - /* archive symfn and symentry objects */ + /* archive symentry objects */ for (psym=symv,pobj=&aobj[1]; *psym && !ret; psym++) { - /* symfn object */ - if (stype[psym-symv] == MDSO_SYMBOL_TYPE_CODE) { - pobj->mapstrs = mapstrs; - pobj->arhdrpos = (uint32_t)(mark - ar); - pobj->arhdrlen = sizeof(struct pe_raw_archive_common_hdr); - pobj->addr = &mark[pobj->arhdrlen]; - - for (symidx=0; symidx<pobj->mapstrsnum; symidx++) { - mdso_write_big_endian_long(idx,pobj->arhdrpos); - idx += sizeof(uint32_t); - } - - ret = mdso_objgen_symfn(dctx,*psym,pobj); - - sprintf( - objname,"f%06zu.o", - psym - symv); - - mdso_argen_common_hdr( - (struct pe_raw_archive_common_hdr *)mark, - objname,pobj->size); - - mark += pobj->arhdrlen + pobj->size; - mapstrs += pobj->mapstrslen; - pobj++; - } - - /* symentry object */ pobj->mapstrs = mapstrs; pobj->arhdrpos = (uint32_t)(mark - ar); diff --git a/src/driver/mdso_unit_ctx.c b/src/driver/mdso_unit_ctx.c index f7c4f83..d14beed 100644 --- a/src/driver/mdso_unit_ctx.c +++ b/src/driver/mdso_unit_ctx.c @@ -22,9 +22,6 @@ static int mdso_free_unit_ctx_impl(struct mdso_unit_ctx_impl * ctx, int status) if (ctx->expsyms && ctx->expsyms->buffer) free(ctx->expsyms->buffer); - if (ctx->expsyms && ctx->expsyms->stype) - free(ctx->expsyms->stype); - if (ctx->expsyms) free(ctx->expsyms); @@ -103,10 +100,8 @@ static int mdso_create_symbol_vector(struct mdso_unit_ctx_impl * ctx) size_t nbytes; size_t size; char * dst; - const char * base; const char * ch; const char ** sym; - int stype; const char exphdr[] = "EXPORTS\n"; const char imphdr[] = "IMPORTS\n"; @@ -126,10 +121,6 @@ static int mdso_create_symbol_vector(struct mdso_unit_ctx_impl * ctx) if (!(ctx->expsyms->buffer = calloc(1,ctx->map.size))) return -1; - if (!(ctx->expsyms->stype = calloc(nsyms,sizeof(int)))) - return -1; - - base = ctx->map.addr; ch = ctx->map.addr; nbytes = ctx->map.size; sym = ctx->expsyms->syms; @@ -154,7 +145,7 @@ static int mdso_create_symbol_vector(struct mdso_unit_ctx_impl * ctx) while (nbytes && ((nbytes < size) || (strncmp(ch,imphdr,size)))) { /* vector */ - *sym = dst; + *sym++ = dst; /* symbol */ for (; nbytes && ((*ch!=' ') @@ -169,22 +160,6 @@ static int mdso_create_symbol_vector(struct mdso_unit_ctx_impl * ctx) for (; nbytes && (*ch!='\n'); nbytes--) ch++; - /* stype */ - stype = ((*ch=='\n') - && ((ch-base) >= 6) - && !strncmp(&ch[-5]," DATA\n",6)) - ? MDSO_SYMBOL_TYPE_DATA - : MDSO_SYMBOL_TYPE_CODE; - - /* code vs. data: pending changes to the midipix gcc target */ - /* bits will remove the need to create .text thunks; in the */ - /* meantime, however, treat everything as code to compensate */ - /* for ld's incorrectly generated .def files. */ - stype = MDSO_SYMBOL_TYPE_CODE; - - ctx->expsyms->stype[sym - ctx->expsyms->syms] = stype; - sym++; - /* advance to next symbol */ for (; nbytes && ((*ch==' ') || (*ch=='\t') @@ -240,7 +215,6 @@ int mdso_get_unit_ctx( ctx->uctx.map = &ctx->map; ctx->uctx.cctx = &ctx->cctx; ctx->uctx.syms = ctx->expsyms->syms; - ctx->uctx.stype = ctx->expsyms->stype; *pctx = &ctx->uctx; diff --git a/src/internal/mdso_driver_impl.h b/src/internal/mdso_driver_impl.h index fc4782f..4cf4064 100644 --- a/src/internal/mdso_driver_impl.h +++ b/src/internal/mdso_driver_impl.h @@ -41,7 +41,6 @@ enum app_tags { struct mdso_expsyms { char * buffer; - int * stype; const char * syms[]; }; diff --git a/src/logic/mdso_asmgen_symfn.c b/src/logic/mdso_asmgen_symfn.c deleted file mode 100644 index e63ec5d..0000000 --- a/src/logic/mdso_asmgen_symfn.c +++ /dev/null @@ -1,43 +0,0 @@ -/****************************************************************/ -/* mdso: midipix dso scavenger */ -/* Copyright (C) 2015--2017 Z. Gilboa */ -/* Released under GPLv2 and GPLv3; see COPYING.MDSO. */ -/****************************************************************/ - -#include <stdint.h> -#include <stdio.h> - -#include <mdso/mdso.h> -#include "mdso_dprintf_impl.h" -#include "mdso_errinfo_impl.h" - -static const char * const asm_lines[] = { - "\t.section .text\n", - "\t.globl %s%s\n", - "\t.def %s%s; .scl 2; .type 32; .endef\n\n", - - "%s%s:\n", - "\tjmp *__imp_%s%s\n\n", - 0 -}; - -int mdso_asmgen_symfn( - const struct mdso_driver_ctx * dctx, - const char * sym, - int fdout) -{ - const char * const * line; - const char * uscore; - - if (mdso_dprintf(fdout,"\t.file \".%s_symfn.s\"\n",sym) < 0) - return MDSO_FILE_ERROR(dctx); - - uscore = (dctx->cctx->drvflags & MDSO_DRIVER_QUAD_PTR) - ? "" : "_"; - - for (line=asm_lines; *line; line++) - if (mdso_dprintf(fdout,*line,uscore,sym) < 0) - return MDSO_FILE_ERROR(dctx); - - return 0; -} diff --git a/src/object/mdso_objgen_symfn.c b/src/object/mdso_objgen_symfn.c deleted file mode 100644 index 6f4ac68..0000000 --- a/src/object/mdso_objgen_symfn.c +++ /dev/null @@ -1,241 +0,0 @@ -/****************************************************************/ -/* mdso: midipix dso scavenger */ -/* Copyright (C) 2015--2017 Z. Gilboa */ -/* Released under GPLv2 and GPLv3; see COPYING.MDSO. */ -/****************************************************************/ - -#include <stdio.h> -#include <stddef.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> -#include <sys/mman.h> - -#include <mdso/mdso.h> -#include "mdso_object_impl.h" -#include "mdso_errinfo_impl.h" -#include "perk_consts.h" -#include "perk_structs.h" - -static const unsigned char jmp_code_i386[16] = { - 0xff,0x25, - 0x0,0x0,0x0,0x0, - 0x90,0x90, - 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 -}; - -static const unsigned char jmp_code_amd64[16] = { - 0xff,0x24,0x25, - 0x0,0x0,0x0,0x0, - 0x90, - 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90 -}; - -struct mdso_symfn_code { - unsigned char code[16]; -}; - -struct mdso_symfn_object { - struct pe_raw_coff_object_hdr hdr; - struct pe_raw_sec_hdr sec[1]; - struct mdso_symfn_code cod[1]; - struct pe_raw_coff_reloc rel[1]; - struct pe_raw_coff_symbol sym[7]; - struct pe_raw_coff_strtbl cst; -}; - -int mdso_objgen_symfn( - const struct mdso_driver_ctx * dctx, - const char * sym, - struct mdso_object * vobj) -{ - struct mdso_symfn_object * symfn; - struct pe_raw_coff_symbol * symrec; - const unsigned char * code; - void * addr; - unsigned char * mark; - unsigned char * mapsym; - struct pe_raw_aux_rec_section * aux; - size_t buflen; - uint32_t symlen; - uint32_t cstlen; - uint32_t objlen; - uint32_t aattr; - uint32_t sattr; - uint16_t oattr; - uint16_t machine; - uint16_t reltype; - uint32_t relrva; - uint32_t reloff; - uint32_t symoff; - uint32_t cstoff; - uint32_t codoff; - uint32_t datoff; - uint32_t uscore; - - if ((buflen = strlen(sym)) > 1024*1024) - return MDSO_CUSTOM_ERROR(dctx,MDSO_ERR_INVALID_DATA); - - symlen = (uint32_t)buflen; - cstlen = (3 * symlen) + 32; - objlen = sizeof(*symfn) + cstlen; - uscore = !(dctx->cctx->drvflags & MDSO_DRIVER_QUAD_PTR); - - if (vobj->addr && (vobj->size < objlen)) - return MDSO_BUFFER_ERROR(dctx); - - if ((addr = vobj->addr)) { - (void)0; - - } else { - vobj->size = objlen; - vobj->mapstrsnum = 1; - vobj->mapstrslen = 1 + uscore + symlen; - - if (!vobj->name) - return 0; - - else if (mdso_create_object(dctx,vobj) < 0) - return MDSO_NESTED_ERROR(dctx); - } - - symfn = (struct mdso_symfn_object *)vobj->addr; - - if (dctx->cctx->drvflags & MDSO_DRIVER_QUAD_PTR) { - code = jmp_code_amd64; - aattr = PE_IMAGE_SCN_ALIGN_16BYTES; - machine = PE_IMAGE_FILE_MACHINE_AMD64; - reltype = PE_IMAGE_REL_AMD64_ADDR32; - relrva = 3; - } else { - code = jmp_code_i386; - aattr = PE_IMAGE_SCN_ALIGN_4BYTES; - machine = PE_IMAGE_FILE_MACHINE_I386; - reltype = PE_IMAGE_REL_I386_DIR32; - relrva = 2; - } - - sattr = aattr; - sattr |= PE_IMAGE_SCN_MEM_READ; - sattr |= PE_IMAGE_SCN_MEM_EXECUTE; - sattr |= PE_IMAGE_SCN_CNT_CODE; - - oattr = PE_IMAGE_FILE_LINE_NUMS_STRIPPED; - reloff = offsetof(struct mdso_symfn_object,rel); - symoff = offsetof(struct mdso_symfn_object,sym); - codoff = offsetof(struct mdso_symfn_object,cod); - cstoff = offsetof(struct pe_raw_coff_strtbl,cst_data); - datoff = 0; - - /* coff object header */ - mdso_obj_write_short(symfn->hdr.cfh_machine,machine); - mdso_obj_write_short(symfn->hdr.cfh_num_of_sections,1); - mdso_obj_write_long(symfn->hdr.cfh_ptr_to_sym_tbl,symoff); - mdso_obj_write_long(symfn->hdr.cfh_num_of_syms,7); - mdso_obj_write_short(symfn->hdr.cfh_characteristics,oattr); - - /* .text section header */ - memcpy(symfn->sec[0].sh_name,".text",5); - mdso_obj_write_long(symfn->sec[0].sh_size_of_raw_data,16); - mdso_obj_write_long(symfn->sec[0].sh_ptr_to_raw_data,codoff); - mdso_obj_write_long(symfn->sec[0].sh_ptr_to_relocs,reloff); - mdso_obj_write_short(symfn->sec[0].sh_num_of_relocs,1); - mdso_obj_write_long(symfn->sec[0].sh_characteristics,sattr); - - /* .text section content */ - memcpy(symfn->cod,code,16); - - /* .text relocation record */ - mdso_obj_write_long(symfn->rel[0].rel_sym,6); - mdso_obj_write_long(symfn->rel[0].rel_rva,relrva); - mdso_obj_write_short(symfn->rel[0].rel_type,reltype); - - /* coff string table */ - mdso_obj_write_long(symfn->cst.cst_size,cstlen); - - /* coff symbol table */ - symrec = symfn->sym; - mark = symfn->cst.cst_data; - - /* coff symbol: .file */ - symrec[0].cs_storage_class[0] = PE_IMAGE_SYM_CLASS_FILE; - symrec[0].cs_num_of_aux_symbols[0] = 1; - - mdso_obj_write_short(&symrec[0].cs_section_number[0],PE_IMAGE_SYM_DEBUG); - mdso_obj_write_long(&symrec[1].cs_name[4],cstoff+datoff); - - memcpy(symrec[0].cs_name,".file",5); - memcpy(&mark[0],".",1); - memcpy(&mark[1],sym,symlen); - memcpy(&mark[1+symlen],"_symfn.s",8); - - datoff += 10 + symlen; - mark += 10 + symlen; - symrec += 2; - - /* coff symbol: .text */ - symrec[0].cs_storage_class[0] = PE_IMAGE_SYM_CLASS_STATIC; - symrec[0].cs_num_of_aux_symbols[0] = 1; - - mdso_obj_write_short(symrec[0].cs_section_number,1); - memcpy(symrec[0].cs_name,".text",5); - - aux = (struct pe_raw_aux_rec_section *)&symrec[1]; - mdso_obj_write_long(aux->aux_size,relrva+4); - mdso_obj_write_short(aux->aux_num_of_relocs,1); - - datoff += 0; - mark += 0; - symrec += 2; - - /* coff symbol: sym */ - mapsym = mark; - symrec[0].cs_storage_class[0] = PE_IMAGE_SYM_CLASS_EXTERNAL; - symrec[0].cs_num_of_aux_symbols[0] = 1; - - mdso_obj_write_short(symrec[0].cs_type,PE_IMAGE_SYM_DTYPE_FUNCTION<<8); - mdso_obj_write_short(symrec[0].cs_section_number,1); - mdso_obj_write_long(&symrec[0].cs_name[4],cstoff+datoff); - - if (dctx->cctx->drvflags & MDSO_DRIVER_QUAD_PTR) { - memcpy(&mark[0],sym,symlen); - } else { - memcpy(&mark[1],sym,symlen); - mark[0] = '_'; - datoff++; - mark++; - } - - datoff += 1 + symlen; - mark += 1 + symlen; - symrec += 2; - - /* archive symbol map */ - if (vobj->mapstrs) - memcpy(vobj->mapstrs,mapsym,mark-mapsym); - - /* coff symbol: __imp_sym */ - symrec[0].cs_storage_class[0] = PE_IMAGE_SYM_CLASS_EXTERNAL; - symrec[0].cs_num_of_aux_symbols[0] = 0; - - mdso_obj_write_short(symrec[0].cs_section_number,0); - mdso_obj_write_long(&symrec[0].cs_name[4],cstoff+datoff); - - if (dctx->cctx->drvflags & MDSO_DRIVER_QUAD_PTR) { - memcpy(&mark[0],"__imp_",6); - memcpy(&mark[6],sym,symlen); - } else { - memcpy(&mark[0],"__imp_",6); - memcpy(&mark[7],sym,symlen); - mark[6] = '_'; - datoff++; - mark++; - } - - /* fs object unmap */ - if (!addr) - munmap(vobj->addr,vobj->size); - - /* tada */ - return 0; -} diff --git a/src/util/mdso_create_implib_archive.c b/src/util/mdso_create_implib_archive.c index 11373b4..a7e5cfe 100644 --- a/src/util/mdso_create_implib_archive.c +++ b/src/util/mdso_create_implib_archive.c @@ -14,17 +14,13 @@ static void mdso_free_uctx_vector( struct mdso_unit_ctx ** uctxv, - const char ** symv, - int * stype) + const char ** symv) { struct mdso_unit_ctx ** puctx; if (symv) free(symv); - if (stype) - free(stype); - for (puctx=uctxv; *puctx; puctx++) mdso_free_unit_ctx(*puctx); @@ -41,7 +37,6 @@ int mdso_create_implib_archive(const struct mdso_driver_ctx * dctx) const char ** unit; const char ** psym; const char ** symv; - int * stype; if (!dctx->cctx->implib) return MDSO_CUSTOM_ERROR(dctx,MDSO_ERR_INVALID_NAME); @@ -54,7 +49,7 @@ int mdso_create_implib_archive(const struct mdso_driver_ctx * dctx) for (puctx=uctxv,unit=dctx->units; *unit; unit++) { if (mdso_get_unit_ctx(dctx,*unit,puctx++)) { - mdso_free_uctx_vector(uctxv,0,0); + mdso_free_uctx_vector(uctxv,0); return MDSO_NESTED_ERROR(dctx); } } @@ -64,27 +59,20 @@ 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); - return MDSO_SYSTEM_ERROR(dctx); - } - - if (!(stype = calloc(nsym+1,sizeof(int)))) { - mdso_free_uctx_vector(uctxv,symv,0); + mdso_free_uctx_vector(uctxv,0); return MDSO_SYSTEM_ERROR(dctx); } for (psym=symv,puctx=uctxv; *puctx; puctx++) { - for (dsym=puctx[0]->syms; *dsym; dsym++) { - stype[psym-symv] = puctx[0]->stype[dsym-puctx[0]->syms]; + for (dsym=puctx[0]->syms; *dsym; dsym++) *psym++ = *dsym; - } } 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); + if (mdso_argen_common(dctx,symv,&obj) < 0) { + mdso_free_uctx_vector(uctxv,symv); return MDSO_NESTED_ERROR(dctx); } diff --git a/src/util/mdso_create_implib_objects.c b/src/util/mdso_create_implib_objects.c index 43f79ee..d310ff8 100644 --- a/src/util/mdso_create_implib_objects.c +++ b/src/util/mdso_create_implib_objects.c @@ -38,7 +38,7 @@ int mdso_create_implib_objects(const struct mdso_driver_ctx * dctx) const char * const * sym; char objname[PATH_MAX]; - /* symentry, symfn */ + /* symentry */ for (unit=dctx->units; *unit; unit++) { if (mdso_get_unit_ctx(dctx,*unit,&uctx)) return MDSO_NESTED_ERROR(dctx); @@ -49,14 +49,6 @@ int mdso_create_implib_objects(const struct mdso_driver_ctx * dctx) if (mdso_objgen_symentry(dctx,*sym,&obj) < 0) return MDSO_NESTED_ERROR(dctx); - - if (uctx->stype[sym-uctx->syms] == MDSO_SYMBOL_TYPE_CODE) { - mdso_init_objname(objname,".%s_symfn.o",*sym); - mdso_init_object(&obj,objname); - - if (mdso_objgen_symfn(dctx,*sym,&obj) < 0) - return MDSO_NESTED_ERROR(dctx); - } } mdso_free_unit_ctx(uctx); diff --git a/src/util/mdso_create_implib_sources.c b/src/util/mdso_create_implib_sources.c index 1ee647d..317f2c8 100644 --- a/src/util/mdso_create_implib_sources.c +++ b/src/util/mdso_create_implib_sources.c @@ -35,7 +35,7 @@ int mdso_create_implib_sources(const struct mdso_driver_ctx * dctx) int ret; - /* symentry, symfn */ + /* symentry */ for (unit=dctx->units; *unit; unit++) { if (mdso_get_unit_ctx(dctx,*unit,&uctx)) return MDSO_NESTED_ERROR(dctx); @@ -53,21 +53,6 @@ int mdso_create_implib_sources(const struct mdso_driver_ctx * dctx) if (ret < 0) return MDSO_NESTED_ERROR(dctx); - - if (uctx->stype[sym-uctx->syms] == MDSO_SYMBOL_TYPE_CODE) { - mdso_init_asmname(asmname,".%s_symfn.s",*sym); - - if ((fdout = mdso_create_asmsrc(dctx,asmname)) < 0) - return MDSO_NESTED_ERROR(dctx); - - ret = mdso_asmgen_symfn(dctx,*sym,fdout); - - if (fdout != mdso_driver_fdout(dctx)) - close(fdout); - - if (ret < 0) - return MDSO_NESTED_ERROR(dctx); - } } mdso_free_unit_ctx(uctx); |