/* parce.c -- Architecture independent load
|
/* parce.c -- Architecture independent load
|
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
|
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
|
|
|
This file is part of OpenRISC 1000 Architectural Simulator.
|
This file is part of OpenRISC 1000 Architectural Simulator.
|
|
|
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
the Free Software Foundation; either version 2 of the License, or
|
the Free Software Foundation; either version 2 of the License, or
|
(at your option) any later version.
|
(at your option) any later version.
|
|
|
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU General Public License for more details.
|
GNU General Public License for more details.
|
|
|
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
along with this program; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
|
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <ctype.h>
|
#include <ctype.h>
|
#include <string.h>
|
#include <string.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
|
|
#include "abstract.h"
|
#include "abstract.h"
|
#include "arch.h"
|
#include "arch.h"
|
#include "dmmu.h"
|
#include "dmmu.h"
|
#include "coff.h"
|
#include "coff.h"
|
#include "debug_unit.h"
|
#include "debug_unit.h"
|
#include "opcode/or32.h"
|
#include "opcode/or32.h"
|
#include "parse.h"
|
#include "parse.h"
|
#include "sim-config.h"
|
#include "sim-config.h"
|
|
|
#define MEMORY_LEN 0x100000000
|
#define MEMORY_LEN 0x100000000
|
#define MAXLINE_LEN 18000
|
#define MAXLINE_LEN 18000
|
|
|
extern char *disassembled;
|
extern char *disassembled;
|
|
|
/* Unused mem memory marker. It is used when allocating program and data memory
|
/* Unused mem memory marker. It is used when allocating program and data memory
|
during parsing */
|
during parsing */
|
unsigned int freemem;
|
unsigned int freemem;
|
|
|
/* Translation table provided by microkernel. Only used if simulating microkernel. */
|
/* Translation table provided by microkernel. Only used if simulating microkernel. */
|
static unsigned long transl_table;
|
static unsigned long transl_table;
|
|
|
/* Used to signal whether during loading of programs a translation fault occured. */
|
/* Used to signal whether during loading of programs a translation fault occured. */
|
static unsigned long transl_error;
|
static unsigned long transl_error;
|
|
|
|
|
char *strtoken(char *in, char *out, int which)
|
char *strtoken(char *in, char *out, int which)
|
{
|
{
|
char *super;
|
char *super;
|
char *sub;
|
char *sub;
|
char *newline;
|
char *newline;
|
|
|
super = strdup(in);
|
super = strdup(in);
|
sub = strtok(super, " \t");
|
sub = strtok(super, " \t");
|
while (sub && --which)
|
while (sub && --which)
|
sub = strtok(NULL, " \t");
|
sub = strtok(NULL, " \t");
|
if (sub && !which) {
|
if (sub && !which) {
|
if ((newline = strchr(sub, '\n')))
|
if ((newline = strchr(sub, '\n')))
|
newline[0] = '\0';
|
newline[0] = '\0';
|
strcpy(out, sub);
|
strcpy(out, sub);
|
} else
|
} else
|
out[0] = '\0';
|
out[0] = '\0';
|
free(super);
|
free(super);
|
if ((newline = strchr(out, '\r'))) /* get rid of CR */
|
if ((newline = strchr(out, '\r'))) /* get rid of CR */
|
newline[0] = '\0';
|
newline[0] = '\0';
|
return(out);
|
return(out);
|
}
|
}
|
|
|
char *
|
char *
|
stripwhite (string)
|
stripwhite (string)
|
char *string;
|
char *string;
|
{
|
{
|
register char *s, *t;
|
register char *s, *t;
|
|
|
for (s = string; whitespace (*s); s++)
|
for (s = string; whitespace (*s); s++)
|
;
|
;
|
|
|
if (*s == 0)
|
if (*s == 0)
|
return (s);
|
return (s);
|
|
|
t = s + strlen (s) - 1;
|
t = s + strlen (s) - 1;
|
while (t > s && whitespace (*t))
|
while (t > s && whitespace (*t))
|
t--;
|
t--;
|
*++t = '\0';
|
*++t = '\0';
|
|
|
return s;
|
return s;
|
}
|
}
|
|
|
char *
|
char *
|
dupstr (s)
|
dupstr (s)
|
char *s;
|
char *s;
|
{
|
{
|
char *r;
|
char *r;
|
|
|
r = (char *)malloc (strlen (s) + 1);
|
r = (char *)malloc (strlen (s) + 1);
|
strcpy (r, s);
|
strcpy (r, s);
|
return (r);
|
return (r);
|
}
|
}
|
|
|
/* Used only by the simulator loader to translate logical addresses into physical.
|
/* Used only by the simulator loader to translate logical addresses into physical.
|
If loadcode() is called with valid virtphy_transl pointer to a table of
|
If loadcode() is called with valid virtphy_transl pointer to a table of
|
translations then translate() performs translation otherwise phy address is
|
translations then translate() performs translation otherwise phy address is
|
equal to logical. */
|
equal to logical. */
|
static unsigned int translate(unsigned int laddr,int* breakpoint)
|
static unsigned int translate(unsigned int laddr,int* breakpoint)
|
{
|
{
|
int i;
|
int i;
|
|
|
/* No translation (i.e. when loading kernel into simulator)
|
/* No translation (i.e. when loading kernel into simulator)
|
/* printf("transl_table=%x laddr=%x\n", transl_table, laddr);
|
/* printf("transl_table=%x laddr=%x\n", transl_table, laddr);
|
printf("laddr=%x\n", laddr);*/
|
printf("laddr=%x\n", laddr);*/
|
if (transl_table == 0)
|
if (transl_table == 0)
|
return laddr;
|
return laddr;
|
|
|
/* Try to find our translation in the table. */
|
/* Try to find our translation in the table. */
|
for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
|
for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
|
if ((laddr & ~(PAGE_SIZE - 1)) == evalsim_mem32(transl_table + i)) {
|
if ((laddr & ~(PAGE_SIZE - 1)) == evalsim_mem32(transl_table + i)) {
|
setsim_mem32(transl_table + i + 8, -2); /* Page modified */
|
setsim_mem32(transl_table + i + 8, -2); /* Page modified */
|
printf("found paddr=%x\n", evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
|
printf("found paddr=%x\n", evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
|
return (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (unsigned long)(PAGE_SIZE - 1));
|
return (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (unsigned long)(PAGE_SIZE - 1));
|
}
|
}
|
|
|
/* Allocate new phy page for us. */
|
/* Allocate new phy page for us. */
|
for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
|
for(i = 0; i < (MEMORY_LEN / PAGE_SIZE) * 16; i += 16)
|
if (evalsim_mem32(transl_table + i + 8) == 0) {
|
if (evalsim_mem32(transl_table + i + 8) == 0) {
|
setsim_mem32(transl_table + i, laddr & ~(PAGE_SIZE - 1)); /* VPN */
|
setsim_mem32(transl_table + i, laddr & ~(PAGE_SIZE - 1)); /* VPN */
|
setsim_mem32(transl_table + i + 4, (i/16) * PAGE_SIZE); /* PPN */
|
setsim_mem32(transl_table + i + 4, (i/16) * PAGE_SIZE); /* PPN */
|
setsim_mem32(transl_table + i + 8, -2); /* Page modified */
|
setsim_mem32(transl_table + i + 8, -2); /* Page modified */
|
printf("newly allocated ppn=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4));
|
printf("newly allocated ppn=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4));
|
printf("newly allocated .ppn=%x\n", (unsigned long)transl_table + i + 4);
|
printf("newly allocated .ppn=%x\n", (unsigned long)transl_table + i + 4);
|
printf("newly allocated ofs=%x\n", (unsigned long)(laddr & (PAGE_SIZE - 1)));
|
printf("newly allocated ofs=%x\n", (unsigned long)(laddr & (PAGE_SIZE - 1)));
|
printf("newly allocated paddr=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
|
printf("newly allocated paddr=%x\n", (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (PAGE_SIZE - 1)));
|
return (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (unsigned long)(PAGE_SIZE - 1));
|
return (unsigned long)evalsim_mem32(transl_table + i + 4) | (laddr & (unsigned long)(PAGE_SIZE - 1));
|
}
|
}
|
/* If we come this far then all phy memory is used and we can't find our page
|
/* If we come this far then all phy memory is used and we can't find our page
|
nor allocate new page. */
|
nor allocate new page. */
|
transl_error = 1;
|
transl_error = 1;
|
|
|
printf("can't translate\n", laddr);
|
printf("can't translate\n", laddr);
|
exit(1);
|
exit(1);
|
return -1;
|
return -1;
|
}
|
}
|
|
|
char null_str[1] = "\0";
|
char null_str[1] = "\0";
|
|
|
/* Modified by CZ 26/05/01 */
|
/* Modified by CZ 26/05/01 */
|
/* Replaced several calls to translate(freemem) with vaddr */
|
/* Replaced several calls to translate(freemem) with vaddr */
|
/* Added new mode execution code */
|
/* Added new mode execution code */
|
/* Changed parameters so address can be passed as argument */
|
/* Changed parameters so address can be passed as argument */
|
void addprogram(unsigned long address, unsigned long insn, int* breakpoint)
|
void addprogram(unsigned long address, unsigned long insn, int* breakpoint)
|
{
|
{
|
char insn_first2_char[3];
|
char insn_first2_char[3];
|
int vaddr = (!config.filename) ? translate(address,breakpoint) : translate(freemem,breakpoint);
|
int vaddr = (!runtime.sim.filename) ? translate(address,breakpoint) : translate(freemem,breakpoint);
|
|
|
debug(9,"addprogram 1\n");
|
debug(9,"addprogram 1\n");
|
|
|
setsim_mem32 (vaddr, insn);
|
setsim_mem32 (vaddr, insn);
|
|
|
if(config.filename)
|
if(runtime.sim.filename)
|
freemem += insn_len (insn_decode (insn));
|
freemem += insn_len (insn_decode (insn));
|
}
|
}
|
|
|
/* Load big-endian COFF file. At the moment it doesn't load symbols yet. */
|
/* Load big-endian COFF file. At the moment it doesn't load symbols yet. */
|
|
|
void readfile_coff(char *filename, short sections)
|
void readfile_coff(char *filename, short sections)
|
{
|
{
|
FILE *inputfs;
|
FILE *inputfs;
|
char inputbuf[4];
|
char inputbuf[4];
|
unsigned long insn;
|
unsigned long insn;
|
signed long sectsize;
|
signed long sectsize;
|
COFF_AOUTHDR coffaouthdr;
|
COFF_AOUTHDR coffaouthdr;
|
struct COFF_scnhdr coffscnhdr;
|
struct COFF_scnhdr coffscnhdr;
|
int len;
|
int len;
|
char item[MAXLINE_LEN];
|
char item[MAXLINE_LEN];
|
char item2[MAXLINE_LEN];
|
char item2[MAXLINE_LEN];
|
int firstthree = 0;
|
int firstthree = 0;
|
int breakpoint = 0;
|
int breakpoint = 0;
|
|
|
if (!(inputfs = fopen(filename, "r"))) {
|
if (!(inputfs = fopen(filename, "r"))) {
|
perror("readfile_coff");
|
perror("readfile_coff");
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
if (fseek(inputfs, sizeof(struct COFF_filehdr), SEEK_SET) == -1) {
|
if (fseek(inputfs, sizeof(struct COFF_filehdr), SEEK_SET) == -1) {
|
fclose(inputfs);
|
fclose(inputfs);
|
perror("readfile_coff");
|
perror("readfile_coff");
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
if (fread(&coffaouthdr, sizeof(coffaouthdr), 1, inputfs) != 1) {
|
if (fread(&coffaouthdr, sizeof(coffaouthdr), 1, inputfs) != 1) {
|
fclose(inputfs);
|
fclose(inputfs);
|
perror("readfile_coff");
|
perror("readfile_coff");
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
while(sections--) {
|
while(sections--) {
|
long scnhdr_pos = sizeof(struct COFF_filehdr) + sizeof(coffaouthdr)
|
long scnhdr_pos = sizeof(struct COFF_filehdr) + sizeof(coffaouthdr)
|
+ sizeof(struct COFF_scnhdr) * firstthree;
|
+ sizeof(struct COFF_scnhdr) * firstthree;
|
if (fseek(inputfs, scnhdr_pos, SEEK_SET) == -1) {
|
if (fseek(inputfs, scnhdr_pos, SEEK_SET) == -1) {
|
fclose(inputfs);
|
fclose(inputfs);
|
perror("readfile_coff");
|
perror("readfile_coff");
|
exit(1);
|
exit(1);
|
}
|
}
|
if (fread(&coffscnhdr, sizeof(struct COFF_scnhdr), 1, inputfs) != 1) {
|
if (fread(&coffscnhdr, sizeof(struct COFF_scnhdr), 1, inputfs) != 1) {
|
fclose(inputfs);
|
fclose(inputfs);
|
perror("readfile_coff");
|
perror("readfile_coff");
|
exit(1);
|
exit(1);
|
}
|
}
|
printf("Section: %s,", coffscnhdr.s_name);
|
printf("Section: %s,", coffscnhdr.s_name);
|
printf(" paddr: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_paddr));
|
printf(" paddr: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_paddr));
|
printf(" vaddr: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_vaddr));
|
printf(" vaddr: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_vaddr));
|
printf(" size: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_size));
|
printf(" size: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_size));
|
printf(" scnptr: 0x%.8x\n", COFF_LONG_H(coffscnhdr.s_scnptr));
|
printf(" scnptr: 0x%.8x\n", COFF_LONG_H(coffscnhdr.s_scnptr));
|
|
|
sectsize = COFF_LONG_H(coffscnhdr.s_size);
|
sectsize = COFF_LONG_H(coffscnhdr.s_size);
|
#if 0
|
#if 0
|
/* A couple of sanity checks. */
|
/* A couple of sanity checks. */
|
if (translate(COFF_LONG_H(coffscnhdr.s_vaddr),&breakpoint) < MEMORY_START) {
|
if (translate(COFF_LONG_H(coffscnhdr.s_vaddr),&breakpoint) < MEMORY_START) {
|
printf("Section %s starts out of ", coffscnhdr.s_name);
|
printf("Section %s starts out of ", coffscnhdr.s_name);
|
printf("memory (at %x)\n", COFF_LONG_H(coffscnhdr.s_vaddr));
|
printf("memory (at %x)\n", COFF_LONG_H(coffscnhdr.s_vaddr));
|
exit(1);
|
exit(1);
|
}
|
}
|
if (translate(COFF_LONG_H(coffscnhdr.s_vaddr) + sectsize,&breakpoint) >
|
if (translate(COFF_LONG_H(coffscnhdr.s_vaddr) + sectsize,&breakpoint) >
|
MEMORY_START + MEMORY_LEN) {
|
MEMORY_START + MEMORY_LEN) {
|
printf("Section %s ends out of ", coffscnhdr.s_name);
|
printf("Section %s ends out of ", coffscnhdr.s_name);
|
printf("memory.\n");
|
printf("memory.\n");
|
exit(1);
|
exit(1);
|
}
|
}
|
#endif
|
#endif
|
#if 0
|
#if 0
|
if (++firstthree == 1 && strcmp(coffscnhdr.s_name, ".text") != 0) {
|
if (++firstthree == 1 && strcmp(coffscnhdr.s_name, ".text") != 0) {
|
printf("First section should be .text (%s instead)\n", coffscnhdr.s_name);
|
printf("First section should be .text (%s instead)\n", coffscnhdr.s_name);
|
exit(1);
|
exit(1);
|
}
|
}
|
if (firstthree == 2 && strcmp(coffscnhdr.s_name, ".data") != 0) {
|
if (firstthree == 2 && strcmp(coffscnhdr.s_name, ".data") != 0) {
|
printf("Second section should be .data (%s instead)\n", coffscnhdr.s_name);
|
printf("Second section should be .data (%s instead)\n", coffscnhdr.s_name);
|
exit(1);
|
exit(1);
|
}
|
}
|
if (firstthree == 3 && strcmp(coffscnhdr.s_name, ".bss") != 0) {
|
if (firstthree == 3 && strcmp(coffscnhdr.s_name, ".bss") != 0) {
|
printf("Third section should be .bss (%s instead)\n", coffscnhdr.s_name);
|
printf("Third section should be .bss (%s instead)\n", coffscnhdr.s_name);
|
exit(1);
|
exit(1);
|
}
|
}
|
#else
|
#else
|
++firstthree;
|
++firstthree;
|
#endif
|
#endif
|
|
|
/* loading section */
|
/* loading section */
|
freemem = COFF_LONG_H(coffscnhdr.s_paddr);
|
freemem = COFF_LONG_H(coffscnhdr.s_paddr);
|
debug(2,"Starting to load at 0x%x\n", freemem);
|
debug(2,"Starting to load at 0x%x\n", freemem);
|
if (fseek(inputfs, COFF_LONG_H(coffscnhdr.s_scnptr), SEEK_SET) == -1) {
|
if (fseek(inputfs, COFF_LONG_H(coffscnhdr.s_scnptr), SEEK_SET) == -1) {
|
fclose(inputfs);
|
fclose(inputfs);
|
perror("readfile_coff");
|
perror("readfile_coff");
|
exit(1);
|
exit(1);
|
}
|
}
|
while (sectsize > 0 && (len = fread(&inputbuf, sizeof(inputbuf), 1, inputfs))) {
|
while (sectsize > 0 && (len = fread(&inputbuf, sizeof(inputbuf), 1, inputfs))) {
|
insn = COFF_LONG_H(inputbuf);
|
insn = COFF_LONG_H(inputbuf);
|
len = insn_len (insn_decode (insn));
|
len = insn_len (insn_decode (insn));
|
if (len == 2)
|
if (len == 2)
|
{
|
{
|
fseek(inputfs, -2, SEEK_CUR);
|
fseek(inputfs, -2, SEEK_CUR);
|
debug(8,"readfile_coff: %x 0x%x \n", sectsize, insn >> 16);
|
debug(8,"readfile_coff: %x 0x%x \n", sectsize, insn >> 16);
|
}
|
}
|
else
|
else
|
debug(8,"readfile_coff: %x 0x%x \n", sectsize, insn);
|
debug(8,"readfile_coff: %x 0x%x \n", sectsize, insn);
|
addprogram (freemem, insn, &breakpoint);
|
addprogram (freemem, insn, &breakpoint);
|
sectsize -= len;
|
sectsize -= len;
|
}
|
}
|
}
|
}
|
if (firstthree < 3) {
|
if (firstthree < 3) {
|
printf("One or more missing sections. At least");
|
printf("One or more missing sections. At least");
|
printf(" three sections expected (.text, .data, .bss).\n");
|
printf(" three sections expected (.text, .data, .bss).\n");
|
exit(1);
|
exit(1);
|
}
|
}
|
if (firstthree > 3) {
|
if (firstthree > 3) {
|
printf("Warning: one or more extra sections. These");
|
printf("Warning: one or more extra sections. These");
|
printf(" sections were handled as .data sections.\n");
|
printf(" sections were handled as .data sections.\n");
|
}
|
}
|
|
|
fclose(inputfs);
|
fclose(inputfs);
|
printf("Finished loading COFF.\n");
|
printf("Finished loading COFF.\n");
|
return;
|
return;
|
}
|
}
|
|
|
/* Load symbols from big-endian COFF file. */
|
/* Load symbols from big-endian COFF file. */
|
|
|
void readsyms_coff(char *filename, unsigned long symptr, long syms)
|
void readsyms_coff(char *filename, unsigned long symptr, long syms)
|
{
|
{
|
FILE *inputfs;
|
FILE *inputfs;
|
struct COFF_syment coffsymhdr;
|
struct COFF_syment coffsymhdr;
|
int breakpoint = 0;
|
int breakpoint = 0;
|
int count = 0;
|
int count = 0;
|
long nsyms = syms;
|
long nsyms = syms;
|
if (!(inputfs = fopen(filename, "r"))) {
|
if (!(inputfs = fopen(filename, "r"))) {
|
perror("readsyms_coff");
|
perror("readsyms_coff");
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
if (fseek(inputfs, symptr, SEEK_SET) == -1) {
|
if (fseek(inputfs, symptr, SEEK_SET) == -1) {
|
fclose(inputfs);
|
fclose(inputfs);
|
perror("readsyms_coff");
|
perror("readsyms_coff");
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
while(syms--) {
|
while(syms--) {
|
int i, n;
|
int i, n;
|
if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
|
if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
|
fclose(inputfs);
|
fclose(inputfs);
|
perror("readsyms_coff");
|
perror("readsyms_coff");
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
n = (unsigned char)coffsymhdr.e_numaux[0];
|
n = (unsigned char)coffsymhdr.e_numaux[0];
|
/* If not important or not in text, skip. */
|
/* If not important or not in text, skip. */
|
if (COFF_SHORT_H(coffsymhdr.e_type) & COFF_N_TMASK & COFF_STYP_TEXT) {
|
if (COFF_SHORT_H(coffsymhdr.e_type) & COFF_N_TMASK & COFF_STYP_TEXT) {
|
|
|
if (*((unsigned long *)coffsymhdr.e.e.e_zeroes)) {
|
if (*((unsigned long *)coffsymhdr.e.e.e_zeroes)) {
|
if (strlen(coffsymhdr.e.e_name) && strlen(coffsymhdr.e.e_name) < 9)
|
if (strlen(coffsymhdr.e.e_name) && strlen(coffsymhdr.e.e_name) < 9)
|
add_label(COFF_LONG_H(coffsymhdr.e_value), coffsymhdr.e.e_name);
|
add_label(COFF_LONG_H(coffsymhdr.e_value), coffsymhdr.e.e_name);
|
debug(8, "[%i] Symbol: %s,", count++, coffsymhdr.e.e_name);
|
debug(8, "[%i] Symbol: %s,", count++, coffsymhdr.e.e_name);
|
} else {
|
} else {
|
long fpos = ftell (inputfs);
|
long fpos = ftell (inputfs);
|
|
|
if (fseek(inputfs, symptr + nsyms * COFF_SYMESZ + COFF_LONG_H(coffsymhdr.e.e.e_offset), SEEK_SET) == 0) {
|
if (fseek(inputfs, symptr + nsyms * COFF_SYMESZ + COFF_LONG_H(coffsymhdr.e.e.e_offset), SEEK_SET) == 0) {
|
char tmp[33], *s = &tmp[0];
|
char tmp[33], *s = &tmp[0];
|
while (s != &tmp[32])
|
while (s != &tmp[32])
|
if ((*(s++) = fgetc(inputfs)) == 0) break;
|
if ((*(s++) = fgetc(inputfs)) == 0) break;
|
tmp[32] = 0;
|
tmp[32] = 0;
|
add_label(COFF_LONG_H(coffsymhdr.e_value), &tmp[0]);
|
add_label(COFF_LONG_H(coffsymhdr.e_value), &tmp[0]);
|
debug(8, "[%i] Symbol: %s,", count++, &tmp[0]);
|
debug(8, "[%i] Symbol: %s,", count++, &tmp[0]);
|
}
|
}
|
fseek(inputfs, fpos, SEEK_SET);
|
fseek(inputfs, fpos, SEEK_SET);
|
}
|
}
|
|
|
debug(9, " val: 0x%.8x,", COFF_LONG_H(coffsymhdr.e_value));
|
debug(9, " val: 0x%.8x,", COFF_LONG_H(coffsymhdr.e_value));
|
debug(9, " type: %x, %x, auxs: %i\n", COFF_SHORT_H(coffsymhdr.e_type), *((unsigned short *)coffsymhdr.e_type), n);
|
debug(9, " type: %x, %x, auxs: %i\n", COFF_SHORT_H(coffsymhdr.e_type), *((unsigned short *)coffsymhdr.e_type), n);
|
}
|
}
|
|
|
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
|
if (fread(&coffsymhdr, COFF_SYMESZ, 1, inputfs) != 1) {
|
fclose(inputfs);
|
fclose(inputfs);
|
perror("readsyms_coff3");
|
perror("readsyms_coff3");
|
exit(1);
|
exit(1);
|
}
|
}
|
syms -= n;
|
syms -= n;
|
count += n;
|
count += n;
|
}
|
}
|
|
|
fclose(inputfs);
|
fclose(inputfs);
|
printf("Finished loading symbols.\n");
|
printf("Finished loading symbols.\n");
|
return;
|
return;
|
}
|
}
|
|
|
/* Identify file type and call appropriate readfile_X routine. It only
|
/* Identify file type and call appropriate readfile_X routine. It only
|
handles orX-coff-big executables at the moment. */
|
handles orX-coff-big executables at the moment. */
|
|
|
void identifyfile(char *filename)
|
void identifyfile(char *filename)
|
{
|
{
|
FILE *inputfs;
|
FILE *inputfs;
|
struct COFF_filehdr coffhdr;
|
struct COFF_filehdr coffhdr;
|
size_t len;
|
size_t len;
|
|
|
if (!(inputfs = fopen(filename, "r"))) {
|
if (!(inputfs = fopen(filename, "r"))) {
|
fprintf(stderr, "xx %s", filename);
|
fprintf(stderr, "xx %s", filename);
|
perror("identifyfile1");
|
perror("identifyfile1");
|
fflush(stdout);
|
fflush(stdout);
|
fflush(stderr);
|
fflush(stderr);
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
if (fread(&coffhdr, sizeof(coffhdr), 1, inputfs) == 1) {
|
if (fread(&coffhdr, sizeof(coffhdr), 1, inputfs) == 1) {
|
if (COFF_SHORT_H(coffhdr.f_magic) == 0x17a) {
|
if (COFF_SHORT_H(coffhdr.f_magic) == 0x17a) {
|
unsigned long opthdr_size;
|
unsigned long opthdr_size;
|
printf("COFF magic: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_magic));
|
printf("COFF magic: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_magic));
|
printf("COFF flags: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_flags));
|
printf("COFF flags: 0x%.4x\n", COFF_SHORT_H(coffhdr.f_flags));
|
printf("COFF symptr: 0x%.8x\n", COFF_LONG_H(coffhdr.f_symptr));
|
printf("COFF symptr: 0x%.8x\n", COFF_LONG_H(coffhdr.f_symptr));
|
if ((COFF_SHORT_H(coffhdr.f_flags) & COFF_F_EXEC) != COFF_F_EXEC) {
|
if ((COFF_SHORT_H(coffhdr.f_flags) & COFF_F_EXEC) != COFF_F_EXEC) {
|
printf("This COFF is not an executable.\n");
|
printf("This COFF is not an executable.\n");
|
exit(1);
|
exit(1);
|
}
|
}
|
opthdr_size = COFF_SHORT_H(coffhdr.f_opthdr);
|
opthdr_size = COFF_SHORT_H(coffhdr.f_opthdr);
|
if (opthdr_size != sizeof(COFF_AOUTHDR)) {
|
if (opthdr_size != sizeof(COFF_AOUTHDR)) {
|
printf("COFF optional header is missing or not recognized.\n");
|
printf("COFF optional header is missing or not recognized.\n");
|
printf("COFF f_opthdr: 0x%.2x\n", opthdr_size);
|
printf("COFF f_opthdr: 0x%.2x\n", opthdr_size);
|
exit(1);
|
exit(1);
|
}
|
}
|
fclose(inputfs);
|
fclose(inputfs);
|
readfile_coff(filename, COFF_SHORT_H(coffhdr.f_nscns));
|
readfile_coff(filename, COFF_SHORT_H(coffhdr.f_nscns));
|
readsyms_coff(filename, COFF_LONG_H(coffhdr.f_symptr), COFF_LONG_H(coffhdr.f_nsyms));
|
readsyms_coff(filename, COFF_LONG_H(coffhdr.f_symptr), COFF_LONG_H(coffhdr.f_nsyms));
|
return;
|
return;
|
}
|
}
|
else {
|
else {
|
printf("Not COFF, quiting.\n");
|
printf("Not COFF, quiting.\n");
|
fclose(inputfs);
|
fclose(inputfs);
|
exit (1);
|
exit (1);
|
}
|
}
|
}
|
}
|
else {
|
else {
|
printf("yy %s", filename);
|
printf("yy %s", filename);
|
perror("identifyfile2");
|
perror("identifyfile2");
|
}
|
}
|
|
|
fclose(inputfs);
|
fclose(inputfs);
|
|
|
return;
|
return;
|
}
|
}
|
|
|
|
|
/* Loads file to memory starting at address startaddr and returns freemem. */
|
/* Loads file to memory starting at address startaddr and returns freemem. */
|
unsigned long loadcode(char *filename, unsigned long startaddr, unsigned long virtphy_transl)
|
unsigned long loadcode(char *filename, unsigned long startaddr, unsigned long virtphy_transl)
|
{
|
{
|
int breakpoint = 0;
|
int breakpoint = 0;
|
|
|
transl_error = 0;
|
transl_error = 0;
|
transl_table = virtphy_transl;
|
transl_table = virtphy_transl;
|
freemem = startaddr;
|
freemem = startaddr;
|
printf("loadcode: filename %s startaddr=%x virtphy_transl=%x", filename, startaddr, virtphy_transl);
|
printf("loadcode: filename %s startaddr=%x virtphy_transl=%x", filename, startaddr, virtphy_transl);
|
identifyfile(filename);
|
identifyfile(filename);
|
if (transl_error)
|
if (transl_error)
|
return -1;
|
return -1;
|
else
|
else
|
return translate(freemem,&breakpoint);
|
return translate(freemem,&breakpoint);
|
}
|
}
|
|
|