Blame src/output/pe_output_image_symbols.c

f779e3
/***************************************************************/
f779e3
/*  perk: PE Resource Kit                                      */
f779e3
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
f779e3
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
f779e3
/***************************************************************/
f779e3
f779e3
#include <stdio.h>
f779e3
#include <string.h>
f779e3
f779e3
#include <perk/perk.h>
f779e3
#include <perk/perk_output.h>
f779e3
#include "perk_reader_impl.h"
f779e3
#include "perk_errinfo_impl.h"
f779e3
f779e3
int pe_output_image_symbols(
f779e3
	const struct pe_driver_ctx *	dctx,
f779e3
	const struct pe_image_meta *	meta,
f779e3
	FILE *				fout)
f779e3
{
f779e3
	unsigned			i,bias;
f779e3
	uint32_t			roffset;
f779e3
	struct pe_coff_sym_entry *	symtbl;
f779e3
	char				buf[24];
f779e3
	char *				mark;
f779e3
	const char *			name;
f779e3
	const char * 			dash = "";
f779e3
f779e3
	if (!fout)
f779e3
		fout = stdout;
f779e3
f779e3
	if (dctx->cctx->fmtflags & PERK_PRETTY_YAML) {
f779e3
		if (fputs("symbols:\n",fout) < 0)
f779e3
			return PERK_FILE_ERROR(dctx);
f779e3
f779e3
		dash = "- ";
f779e3
	}
f779e3
f779e3
	mark   = (char *)meta->image.addr;
f779e3
	symtbl = (struct pe_coff_sym_entry *)(mark + meta->coff.ptr_to_sym_tbl);
f779e3
f779e3
	for (i=0,bias=0; i<meta->coff.num_of_syms; i++,bias=0) {
f779e3
		if (symtbl[i].storage_class[0] == PE_IMAGE_SYM_CLASS_FILE)
f779e3
			if (symtbl[i].num_of_aux_symbols[0])
f779e3
				if (!symtbl[i+1].value[0])
f779e3
					bias = 1;
f779e3
f779e3
		i += bias;
f779e3
f779e3
		if (!bias && (symtbl[i].storage_class[0] == PE_IMAGE_SYM_CLASS_FILE)
f779e3
				&& symtbl[i].num_of_aux_symbols[0]) {
f779e3
			memset(buf,0,sizeof(buf));
f779e3
			memcpy(buf,symtbl[i+1].name,sizeof(*symtbl));
f779e3
			name = buf;
f779e3
f779e3
		} else if (symtbl[i].name[0]) {
f779e3
			memset(buf,0,sizeof(buf));
f779e3
			memcpy(buf,symtbl[i].name,sizeof(symtbl->name));
f779e3
			name = buf;
f779e3
f779e3
		} else if (!symtbl[i].name[1] && !symtbl[i].name[2] && !symtbl[i].name[3]) {
f779e3
			roffset = pe_read_long(&symtbl[i].name[4]);
f779e3
			name    = (roffset < meta->coff.size_of_string_tbl)
f779e3
					? mark + meta->coff.ptr_to_string_tbl + roffset
f779e3
					: 0;
f779e3
		} else {
f779e3
			name = 0;
f779e3
		}
f779e3
f779e3
		i -= bias;
f779e3
		i += symtbl[i].num_of_aux_symbols[0];
f779e3
f779e3
f779e3
		if (name) {
f779e3
			if (fprintf(fout,"%s%s\n",dash,name) < 0)
f779e3
				return PERK_FILE_ERROR(dctx);
f779e3
		}
f779e3
	}
f779e3
f779e3
	return 0;
f779e3
}