Blame src/reader/pe_read_coff_symbol.c

edaa40
/***************************************************************/
edaa40
/*  perk: PE Resource Kit                                      */
425fb8
/*  Copyright (C) 2015--2021  SysDeer Technologies, LLC        */
edaa40
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
edaa40
/***************************************************************/
edaa40
edaa40
#include <string.h>
edaa40
edaa40
#include <perk/perk.h>
d6e02b
#include <perk/perk_consts.h>
d6e02b
#include <perk/perk_structs.h>
edaa40
#include "perk_endian_impl.h"
edaa40
#include "perk_reader_impl.h"
edaa40
edaa40
int pe_read_coff_symbol(
e75572
	const struct pe_raw_coff_symbol *	p,
edaa40
	struct pe_meta_coff_symbol *		m,
edaa40
	const struct pe_meta_coff_file_hdr *	coff,
edaa40
	void *					base)
edaa40
{
edaa40
	uint32_t	roffset;
edaa40
	char *		mark;
edaa40
	unsigned	bias = 0;
edaa40
0c1d83
	m->cs_long_name           = 0;
0c1d83
	m->cs_value               = pe_read_long(p->cs_value);
0c1d83
	m->cs_section_number      = pe_read_short(p->cs_section_number);
0c1d83
	m->cs_type                = pe_read_short(p->cs_type);
0c1d83
	m->cs_storage_class       = p->cs_storage_class[0];
0c1d83
	m->cs_num_of_aux_symbols  = p->cs_num_of_aux_symbols[0];
0c1d83
0c1d83
	memset(m->cs_name,0,sizeof(m->cs_name));
0c1d83
0c1d83
	if (p->cs_storage_class[0] == PE_IMAGE_SYM_CLASS_FILE)
0c1d83
		if (p->cs_num_of_aux_symbols[0])
0c1d83
			if (!p[1].cs_value[0])
edaa40
				bias = 1;
edaa40
edaa40
	p += bias;
edaa40
0c1d83
	if (!bias && (p->cs_storage_class[0] == PE_IMAGE_SYM_CLASS_FILE)
0c1d83
			&& p->cs_num_of_aux_symbols[0]) {
0c1d83
		memcpy(m->cs_name,p[1].cs_name,sizeof(*p));
edaa40
0c1d83
	} else if (p->cs_name[0]) {
0c1d83
		memcpy(m->cs_name,p->cs_name,sizeof(p->cs_name));
edaa40
0c1d83
	} else if (!p->cs_name[1] && !p->cs_name[2] && !p->cs_name[3]) {
edaa40
		mark	= (char *)base;
0c1d83
		roffset	= pe_read_long(&p->cs_name[4]);
edaa40
da15f0
		if (roffset < coff->cfh_size_of_str_tbl)
0c1d83
			m->cs_long_name = mark + coff->cfh_ptr_to_str_tbl + roffset;
edaa40
	}
edaa40
edaa40
	return 0;
edaa40
}