Blame src/object/mdso_objgen_symentry.c

bc1dc4
/****************************************************************/
bc1dc4
/*  mdso: midipix dso scavenger                                 */
bc1dc4
/*  Copyright (C) 2015--2017  Z. Gilboa                         */
bc1dc4
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
bc1dc4
/****************************************************************/
bc1dc4
bc1dc4
#include <stdio.h>
bc1dc4
#include <stddef.h>
bc1dc4
#include <stdint.h>
bc1dc4
#include <stdlib.h>
bc1dc4
#include <string.h>
a3e2cf
#include <sys/mman.h>
bc1dc4
bc1dc4
#include <mdso/mdso.h>
bc1dc4
#include "mdso_object_impl.h"
bc1dc4
#include "mdso_errinfo_impl.h"
bc1dc4
#include "perk_consts.h"
bc1dc4
#include "perk_structs.h"
bc1dc4
bc1dc4
struct mdso_symfn_references {
bc1dc4
	unsigned char 	refs[16];
bc1dc4
};
bc1dc4
bc1dc4
struct mdso_symentry_object {
bc1dc4
	struct pe_raw_coff_object_hdr	hdr;
bc1dc4
	struct pe_raw_sec_hdr		sec[2];
bc1dc4
	struct mdso_symfn_references	ref[1];
bc1dc4
	struct pe_raw_coff_reloc	rel[2];
bc1dc4
	struct pe_raw_coff_symbol	sym[9];
bc1dc4
	struct pe_raw_coff_strtbl	cst;
bc1dc4
};
bc1dc4
bc1dc4
int mdso_objgen_symentry(
bc1dc4
	const struct mdso_driver_ctx *	dctx,
bc1dc4
	const char *			sym,
bc1dc4
	struct mdso_object *		vobj)
bc1dc4
{
bc1dc4
	struct mdso_symentry_object *	syment;
bc1dc4
	struct pe_raw_coff_symbol *	symrec;
a3e2cf
	void *				addr;
bc1dc4
	unsigned char *			mark;
9c013b
	unsigned char *			mapsym;
bbf7fc
	struct pe_raw_aux_rec_section *	aux;
bc1dc4
	size_t				buflen;
bc1dc4
	size_t				liblen;
bc1dc4
	uint32_t			symlen;
bc1dc4
	uint32_t			cstlen;
bc1dc4
	uint32_t			objlen;
bc1dc4
	uint32_t			aattr;
bc1dc4
	uint32_t			sattr;
bc1dc4
	uint32_t			rattr;
bc1dc4
	uint16_t			oattr;
bc1dc4
	uint16_t			machine;
bc1dc4
	uint16_t			reltype;
bc1dc4
	uint32_t			relrva;
bc1dc4
	uint32_t			refoff;
bc1dc4
	uint32_t			reloff;
bc1dc4
	uint32_t			symoff;
bc1dc4
	uint32_t			cstoff;
bc1dc4
	uint32_t			datoff;
bc1dc4
	uint32_t			stroff;
9c013b
	uint32_t			uscore;
bc1dc4
bc1dc4
	if ((buflen = strlen(sym)) > 1024*1024)
bc1dc4
		return MDSO_CUSTOM_ERROR(dctx,MDSO_ERR_INVALID_DATA);
bc1dc4
bc1dc4
	if ((liblen = strlen(dctx->cctx->libname)) > 1024*1024)
bc1dc4
		return MDSO_CUSTOM_ERROR(dctx,MDSO_ERR_INVALID_DATA);
bc1dc4
bc1dc4
	symlen = (uint32_t)buflen;
bc1dc4
	cstlen = (uint32_t)liblen + (3 * symlen) + 64;
bc1dc4
	objlen = sizeof(*syment) + cstlen;
9c013b
	uscore = !(dctx->cctx->drvflags & MDSO_DRIVER_QUAD_PTR);
bc1dc4
a3e2cf
	if (vobj->addr && (vobj->size < objlen))
bc1dc4
		return MDSO_BUFFER_ERROR(dctx);
bc1dc4
a3e2cf
	if ((addr = vobj->addr)) {
a3e2cf
		(void)0;
a3e2cf
a3e2cf
	} else {
a3e2cf
		vobj->size       = objlen;
9c013b
		vobj->mapstrsnum = 1;
9c013b
		vobj->mapstrslen = 7 + uscore + symlen;
bc1dc4
a3e2cf
		if (!vobj->name)
a3e2cf
			return 0;
a3e2cf
a3e2cf
		else if (mdso_create_object(dctx,vobj) < 0)
a3e2cf
			return MDSO_NESTED_ERROR(dctx);
a3e2cf
	}
bc1dc4
a3e2cf
	syment = (struct mdso_symentry_object *)vobj->addr;
bc1dc4
bc1dc4
	if (dctx->cctx->drvflags & MDSO_DRIVER_QUAD_PTR) {
bc1dc4
		aattr   = PE_IMAGE_SCN_ALIGN_16BYTES;
bc1dc4
		machine = PE_IMAGE_FILE_MACHINE_AMD64;
bc1dc4
		reltype = PE_IMAGE_REL_AMD64_ADDR64;
bc1dc4
		relrva  = 8;
bc1dc4
	} else {
bc1dc4
		aattr   = PE_IMAGE_SCN_ALIGN_4BYTES;
bc1dc4
		machine = PE_IMAGE_FILE_MACHINE_I386;
bc1dc4
		reltype = PE_IMAGE_REL_I386_DIR32;
bc1dc4
		relrva  = 4;
bc1dc4
	}
bc1dc4
bc1dc4
	sattr  = PE_IMAGE_SCN_ALIGN_1BYTES;
bc1dc4
	sattr |= PE_IMAGE_SCN_MEM_READ;
bc1dc4
	sattr |= PE_IMAGE_SCN_CNT_INITIALIZED_DATA;
bc1dc4
bc1dc4
	rattr  = aattr;
bc1dc4
	rattr |= PE_IMAGE_SCN_MEM_READ;
bc1dc4
	rattr |= PE_IMAGE_SCN_CNT_INITIALIZED_DATA;
bc1dc4
bc1dc4
	oattr  = PE_IMAGE_FILE_LINE_NUMS_STRIPPED;
bc1dc4
	refoff = offsetof(struct mdso_symentry_object,ref);
bc1dc4
	reloff = offsetof(struct mdso_symentry_object,rel);
bc1dc4
	symoff = offsetof(struct mdso_symentry_object,sym);
bc1dc4
	cstoff = offsetof(struct pe_raw_coff_strtbl,cst_data);
bc1dc4
	datoff = 0;
bc1dc4
bc1dc4
	stroff  = objlen - symlen - 16;
bc1dc4
	stroff += 0xf;
bc1dc4
	stroff |= 0xf;
bc1dc4
	stroff ^= 0xf;
bc1dc4
bc1dc4
	/* coff object header */
bc1dc4
	mdso_obj_write_short(syment->hdr.cfh_machine,machine);
bc1dc4
	mdso_obj_write_short(syment->hdr.cfh_num_of_sections,2);
bc1dc4
	mdso_obj_write_long(syment->hdr.cfh_ptr_to_sym_tbl,symoff);
bc1dc4
	mdso_obj_write_long(syment->hdr.cfh_num_of_syms,9);
bc1dc4
	mdso_obj_write_short(syment->hdr.cfh_characteristics,oattr);
bc1dc4
bc1dc4
	/* .dsostrs section header */
bc1dc4
	memcpy(syment->sec[0].sh_name,".dsostrs",8);
bc1dc4
	mdso_obj_write_long(syment->sec[0].sh_size_of_raw_data,symlen+1);
bc1dc4
	mdso_obj_write_long(syment->sec[0].sh_ptr_to_raw_data,stroff);
bc1dc4
	mdso_obj_write_long(syment->sec[0].sh_characteristics,sattr);
bc1dc4
bc1dc4
	/* .dsosyms section header */
bc1dc4
	memcpy(syment->sec[1].sh_name,".dsosyms",8);
bc1dc4
	mdso_obj_write_long(syment->sec[1].sh_size_of_raw_data,2*relrva);
bc1dc4
	mdso_obj_write_long(syment->sec[1].sh_ptr_to_raw_data,refoff);
bc1dc4
	mdso_obj_write_long(syment->sec[1].sh_ptr_to_relocs,reloff);
bc1dc4
	mdso_obj_write_short(syment->sec[1].sh_num_of_relocs,2);
bc1dc4
	mdso_obj_write_long(syment->sec[1].sh_characteristics,rattr);
bc1dc4
bc1dc4
	/* .dsosyms relocation record: .symstr */
bc1dc4
	mdso_obj_write_long(syment->rel[0].rel_sym,6);
bc1dc4
	mdso_obj_write_long(syment->rel[0].rel_rva,0);
bc1dc4
	mdso_obj_write_short(syment->rel[0].rel_type,reltype);
bc1dc4
bc1dc4
	/* .dsosyms relocation record: .dsometa_libname */
bc1dc4
	mdso_obj_write_long(syment->rel[1].rel_sym,8);
bc1dc4
	mdso_obj_write_long(syment->rel[1].rel_rva,relrva);
bc1dc4
	mdso_obj_write_short(syment->rel[1].rel_type,reltype);
bc1dc4
bc1dc4
	/* coff string table */
bc1dc4
	mdso_obj_write_long(syment->cst.cst_size,cstlen);
bc1dc4
bc1dc4
	/* coff symbol table */
bc1dc4
	symrec = syment->sym;
bc1dc4
	mark   = syment->cst.cst_data;
bc1dc4
bc1dc4
	/* coff symbol: .file */
bc1dc4
	symrec[0].cs_storage_class[0] = PE_IMAGE_SYM_CLASS_FILE;
bc1dc4
	symrec[0].cs_num_of_aux_symbols[0] = 1;
bc1dc4
bc1dc4
	mdso_obj_write_short(&symrec[0].cs_section_number[0],PE_IMAGE_SYM_DEBUG);
bc1dc4
	mdso_obj_write_long(&symrec[1].cs_name[4],cstoff+datoff);
bc1dc4
bc1dc4
	memcpy(symrec[0].cs_name,".file",5);
c0e12c
	memcpy(&mark[0],".",1);
c0e12c
	memcpy(&mark[1],sym,symlen);
c0e12c
	memcpy(&mark[1+symlen],"_symentry.s",11);
bc1dc4
c0e12c
	datoff += 13 + symlen;
c0e12c
	mark   += 13 + symlen;
bc1dc4
	symrec += 2;
bc1dc4
bc1dc4
	/* coff symbol: .dsostrs */
bc1dc4
	symrec[0].cs_storage_class[0] = PE_IMAGE_SYM_CLASS_STATIC;
bc1dc4
	symrec[0].cs_num_of_aux_symbols[0] = 1;
bc1dc4
bc1dc4
	mdso_obj_write_short(symrec[0].cs_section_number,1);
bc1dc4
	memcpy(symrec[0].cs_name,".dsostrs",8);
bc1dc4
bbf7fc
	aux = (struct pe_raw_aux_rec_section *)&symrec[1];
bc1dc4
	mdso_obj_write_long(aux->aux_size,symlen+1);
bc1dc4
	mdso_obj_write_short(aux->aux_num_of_relocs,0);
bc1dc4
bc1dc4
	datoff += 0;
bc1dc4
	mark   += 0;
bc1dc4
	symrec += 2;
bc1dc4
bc1dc4
	/* coff symbol: .dsosyms */
bc1dc4
	symrec[0].cs_storage_class[0] = PE_IMAGE_SYM_CLASS_STATIC;
bc1dc4
	symrec[0].cs_num_of_aux_symbols[0] = 1;
bc1dc4
bc1dc4
	mdso_obj_write_short(symrec[0].cs_section_number,2);
bc1dc4
	memcpy(symrec[0].cs_name,".dsosyms",8);
bc1dc4
bbf7fc
	aux = (struct pe_raw_aux_rec_section *)&symrec[1];
bc1dc4
	mdso_obj_write_long(aux->aux_size,2*relrva);
bc1dc4
	mdso_obj_write_short(aux->aux_num_of_relocs,2);
bc1dc4
bc1dc4
	datoff += 0;
bc1dc4
	mark   += 0;
bc1dc4
	symrec += 2;
bc1dc4
bc1dc4
	/* coff symbol: .symstr */
bc1dc4
	symrec[0].cs_storage_class[0] = PE_IMAGE_SYM_CLASS_STATIC;
bc1dc4
	symrec[0].cs_num_of_aux_symbols[0] = 0;
bc1dc4
bc1dc4
	mdso_obj_write_short(symrec[0].cs_section_number,1);
bc1dc4
	memcpy(symrec[0].cs_name,".symstr",7);
bc1dc4
bc1dc4
	datoff += 0;
bc1dc4
	mark   += 0;
bc1dc4
	symrec += 1;
bc1dc4
bc1dc4
	/* coff symbol: __imp_sym */
9c013b
	mapsym = mark;
bc1dc4
	symrec[0].cs_storage_class[0] = PE_IMAGE_SYM_CLASS_EXTERNAL;
bc1dc4
	symrec[0].cs_num_of_aux_symbols[0] = 0;
bc1dc4
bc1dc4
	mdso_obj_write_short(symrec[0].cs_section_number,2);
bc1dc4
	mdso_obj_write_long(&symrec[0].cs_name[4],cstoff+datoff);
bc1dc4
bc1dc4
	if (dctx->cctx->drvflags & MDSO_DRIVER_QUAD_PTR) {
bc1dc4
		memcpy(&mark[0],"__imp_",6);
bc1dc4
		memcpy(&mark[6],sym,symlen);
bc1dc4
	} else {
bc1dc4
		memcpy(&mark[0],"__imp_",6);
bc1dc4
		memcpy(&mark[7],sym,symlen);
bc1dc4
		mark[6] = '_';
bc1dc4
		datoff++;
bc1dc4
		mark++;
bc1dc4
	}
bc1dc4
bc1dc4
	datoff += 7 + symlen;
bc1dc4
	mark   += 7 + symlen;
bc1dc4
	symrec += 1;
bc1dc4
9c013b
	/* archive symbol map */
a3e2cf
	if (vobj->mapstrs)
9c013b
		memcpy(vobj->mapstrs,mapsym,mark-mapsym);
9c013b
bc1dc4
	/* coff symbol: .dsometa_libname */
bc1dc4
	symrec[0].cs_storage_class[0] = PE_IMAGE_SYM_CLASS_EXTERNAL;
bc1dc4
	symrec[0].cs_num_of_aux_symbols[0] = 0;
bc1dc4
bc1dc4
	mdso_obj_write_short(symrec[0].cs_section_number,0);
bc1dc4
	mdso_obj_write_long(&symrec[0].cs_name[4],cstoff+datoff);
bc1dc4
bc1dc4
	memcpy(&mark[0],".dsometa_",9);
bc1dc4
	memcpy(&mark[9],dctx->cctx->libname,liblen);
bc1dc4
bc1dc4
	/* .symstr */
bc1dc4
	mark = syment->hdr.cfh_machine;
bc1dc4
	memcpy(&mark[stroff],sym,symlen);
bc1dc4
a3e2cf
	/* fs object unmap */
a3e2cf
	if (!addr)
a3e2cf
		munmap(vobj->addr,vobj->size);
bc1dc4
a3e2cf
	/* tada */
bc1dc4
	return 0;
bc1dc4
}