Line 155... |
Line 155... |
|
|
void addlabel(char *label, unsigned long freemem)
|
void addlabel(char *label, unsigned long freemem)
|
{
|
{
|
struct label_entry **tmp;
|
struct label_entry **tmp;
|
|
|
printf("adding label %s at 0x%x\n", label, freemem);
|
debug("Adding label %s at 0x%x\n", label, freemem);
|
tmp = &mem[freemem].label;
|
tmp = &mem[freemem].label;
|
for (; *tmp; tmp = &((*tmp)->next));
|
for (; *tmp; tmp = &((*tmp)->next));
|
*tmp = malloc(sizeof(**tmp));
|
*tmp = malloc(sizeof(**tmp));
|
(*tmp)->name = malloc(strlen(label)+1);
|
(*tmp)->name = malloc(strlen(label)+1);
|
strcpy((*tmp)->name, label);
|
strcpy((*tmp)->name, label);
|
Line 402... |
Line 402... |
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 tstart, tsize, dstart, dsize;
|
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;
|
|
|
if (!(inputfs = fopen(filename, "r"))) {
|
if (!(inputfs = fopen(filename, "r"))) {
|
perror("readfile_coff");
|
perror("readfile_coff");
|
exit(1);
|
exit(1);
|
}
|
}
|
Line 427... |
Line 427... |
fclose(inputfs);
|
fclose(inputfs);
|
perror("readfile_coff");
|
perror("readfile_coff");
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
tstart = COFF_LONG_H(coffaouthdr.text_start);
|
|
dstart = COFF_LONG_H(coffaouthdr.data_start);
|
|
tsize = COFF_LONG_H(coffaouthdr.tsize);
|
|
dsize = COFF_LONG_H(coffaouthdr.dsize);
|
|
printf("text_start: %x, ", tstart);
|
|
printf("tsize: %x, ", tsize);
|
|
printf("data_start: %x, ", dstart);
|
|
printf("dsize: %x\n", dsize);
|
|
|
|
while(sections--) {
|
while(sections--) {
|
|
long scnhdr_pos = sizeof(struct COFF_filehdr) + sizeof(coffaouthdr)
|
|
+ sizeof(struct COFF_scnhdr) * firstthree;
|
|
if (fseek(inputfs, scnhdr_pos, SEEK_SET) == -1) {
|
|
fclose(inputfs);
|
|
perror("readfile_coff");
|
|
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(" size: 0x%.4x,", COFF_LONG_H(coffscnhdr.s_size));
|
printf(" vaddr: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_vaddr));
|
printf(" scnptr: 0x%.4x\n", COFF_LONG_H(coffscnhdr.s_scnptr));
|
printf(" size: 0x%.8x,", COFF_LONG_H(coffscnhdr.s_size));
|
|
printf(" scnptr: 0x%.8x\n", COFF_LONG_H(coffscnhdr.s_scnptr));
|
|
|
|
sectsize = COFF_LONG_H(coffscnhdr.s_size);
|
|
/* A couple of sanity checks. */
|
|
if (COFF_LONG_H(coffscnhdr.s_vaddr) < MEMORY_START) {
|
|
printf("Section %s starts out of ", coffscnhdr.s_name);
|
|
printf("memory (at %x)\n", COFF_LONG_H(coffscnhdr.s_vaddr));
|
|
exit(1);
|
|
}
|
|
if (COFF_LONG_H(coffscnhdr.s_vaddr) + sectsize >
|
|
MEMORY_START + MEMORY_LEN) {
|
|
printf("Section %s ends out of ", coffscnhdr.s_name);
|
|
printf("memory.\n");
|
|
exit(1);
|
|
}
|
|
if (++firstthree == 1 && strcmp(coffscnhdr.s_name, ".text") != 0) {
|
|
printf("First section should be .text (%s instead)\n", coffscnhdr.s_name);
|
|
exit(1);
|
|
}
|
|
if (firstthree == 2 && strcmp(coffscnhdr.s_name, ".data") != 0) {
|
|
printf("Second section should be .data (%s instead)\n", coffscnhdr.s_name);
|
|
exit(1);
|
|
}
|
|
if (firstthree == 3 && strcmp(coffscnhdr.s_name, ".bss") != 0) {
|
|
printf("Third section should be .bss (%s instead)\n", coffscnhdr.s_name);
|
|
exit(1);
|
}
|
}
|
|
|
/* loading .text section */
|
/* loading section */
|
while ((len = fread(&inputbuf, sizeof(inputbuf), 1, inputfs))) {
|
freemem = COFF_LONG_H(coffscnhdr.s_vaddr);
|
|
if (fseek(inputfs, COFF_LONG_H(coffscnhdr.s_scnptr), SEEK_SET) == -1) {
|
|
fclose(inputfs);
|
|
perror("readfile_coff");
|
|
exit(1);
|
|
}
|
|
while (sectsize > 0 && (len = fread(&inputbuf, sizeof(inputbuf), 1, inputfs))) {
|
insn = COFF_LONG_H(inputbuf);
|
insn = COFF_LONG_H(inputbuf);
|
len = disassemble_insn(insn);
|
len = disassemble_insn(insn);
|
sprintf(item, "%u", insn);
|
sprintf(item, "%u", insn);
|
adddataword(item);
|
adddataword(item);
|
freemem -= len;
|
freemem -= len;
|
if (len == 2) {
|
if (len == 2) {
|
fseek(inputfs, -2, SEEK_CUR);
|
fseek(inputfs, -2, SEEK_CUR);
|
printf("readfile_coff: %x 0x%x ", tsize, insn >> 16);
|
debug("readfile_coff: %x 0x%x ", sectsize, insn >> 16);
|
}
|
}
|
else
|
else
|
printf("readfile_coff: %x 0x%x ", tsize, insn);
|
debug("readfile_coff: %x 0x%x ", sectsize, insn);
|
printf("%s\n", disassembled);
|
debug("%s\n", disassembled);
|
strtoken(disassembled, item, 1); /* opcode */
|
strtoken(disassembled, item, 1); /* opcode */
|
strtoken(disassembled, item2, 2); /* all the remaining one/two/three operands */
|
strtoken(disassembled, item2, 2); /* all the remaining one/two/three operands */
|
addprogram(item, item2);
|
addprogram(item, item2);
|
tsize -= len;
|
sectsize -= len;
|
if (tsize <= 0)
|
}
|
break;
|
}
|
|
if (firstthree < 3) {
|
|
printf("One or more missing sections. At least");
|
|
printf(" three sections expected (.text, .data, .bss).\n");
|
|
exit(1);
|
|
}
|
|
if (firstthree > 3) {
|
|
printf("Warning: one or more extra sections. These");
|
|
printf(" sections were handled as .data sections.\n");
|
}
|
}
|
|
|
fclose(inputfs);
|
fclose(inputfs);
|
printf("Finished loading COFF.\n");
|
printf("Finished loading COFF.\n");
|
return;
|
return;
|
}
|
}
|
|
|
Line 498... |
Line 537... |
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);
|
}
|
}
|
printf("Symbol: %s,", coffsymhdr.e.e_name);
|
debug("Symbol: %s,", coffsymhdr.e.e_name);
|
printf(" val: 0x%.8x,", COFF_LONG_H(coffsymhdr.e_value));
|
debug(" val: 0x%.8x,", COFF_LONG_H(coffsymhdr.e_value));
|
printf(" auxs: %c\n", coffsymhdr.e_numaux);
|
debug(" auxs: %c\n", coffsymhdr.e_numaux);
|
if (strlen(coffsymhdr.e.e_name))
|
if (strlen(coffsymhdr.e.e_name))
|
addlabel(coffsymhdr.e.e_name, COFF_LONG_H(coffsymhdr.e_value));
|
addlabel(coffsymhdr.e.e_name, COFF_LONG_H(coffsymhdr.e_value));
|
}
|
}
|
|
|
fclose(inputfs);
|
fclose(inputfs);
|