view ticoff/tables.c @ 71:c15cd3d695c0

tiobjd: successful parsing of the section header table
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 22 Mar 2014 05:53:02 +0000
parents
children 10f3fbff5e97
line wrap: on
line source

/*
 * This C module will contain functions for the initial parsing
 * of the section and symbol tables.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "filestruct.h"
#include "intstruct.h"
#include "globals.h"

extern unsigned get_u16(), get_u32();

static char *
get_secorsym_name(ptr)
	u_char *ptr;
{
	char *alloc;

	if (ptr[0]) {
		if (ptr[7]) {
			alloc = malloc(9);
			if (!alloc) {
				perror("malloc");
				exit(1);
			}
			bcopy(ptr, alloc, 8);
			alloc[8] = '\0';
			return(alloc);
		} else
			return((char *) ptr);
	}
	if (ptr[1] || ptr[2] || ptr[3]) {
		fprintf(stderr, "error: malformed name in %s at offset 0x%x\n",
			objfilename, ptr - filemap);
		exit(1);
	}
	return(filemap + strtab_offset + get_u32(ptr+4));
}

get_int_section_table()
{
	unsigned n;

	sections = malloc(sizeof(struct internal_scnhdr) * nsections);
	if (!sections) {
		perror("malloc");
		exit(1);
	}
	for (n = 0; n < nsections; n++) {
		sections[n].name = get_secorsym_name(sections_raw[n].s_name);
		if (get_u32(sections_raw[n].s_paddr))
			fprintf(stderr,
				"warning: section #%u (%s) has nonzero paddr\n",
				n, sections[n].name);
		if (get_u32(sections_raw[n].s_vaddr))
			fprintf(stderr,
				"warning: section #%u (%s) has nonzero vaddr\n",
				n, sections[n].name);
		sections[n].size = get_u32(sections_raw[n].s_size);
		sections[n].data_offset = get_u32(sections_raw[n].s_scnptr);
		sections[n].reloc_offset = get_u32(sections_raw[n].s_relptr);
		sections[n].line_offset = get_u32(sections_raw[n].s_lnnoptr);
		sections[n].nreloc = get_u32(sections_raw[n].s_nreloc);
		sections[n].nlineent = get_u32(sections_raw[n].s_nlnno);
		sections[n].flags = get_u32(sections_raw[n].s_flags);
		if (get_u16(sections_raw[n].s_reserved))
			fprintf(stderr,
	"warning: section #%u (%s): some nonzero value in s_reserved bytes\n",
				n, sections[n].name);
		if (get_u16(sections_raw[n].s_page))
			fprintf(stderr,
	"warning: section #%u (%s): some nonzero value in s_page bytes\n",
				n, sections[n].name);
	}
}

cmd_sechdr()
{
	unsigned n;
	struct internal_scnhdr *inf;

	get_int_section_table();
	for (n = 0; n < nsections; n++) {
		inf = sections + n;
		printf("#%d: %s size=%u, flags=0x%x\n", n, inf->name,
			inf->size, inf->flags);
		printf("\t%u reloc, %u line entries\n",
			inf->nreloc, inf->nlineent);
	}
	exit(0);
}