[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freedos-dev
Subject:    .exe static relocator
From:       ror4 <ror4 () usa ! net>
Date:       1999-02-18 23:22:45
[Download RAW message or body]

Hi,

I managed to write my own .exe static relocator just a moment ago. However
I have problems uploading it to my home page, so I've decided to include
it here first. Hope it's useful. :)

-----makefile-----
exeflat.com: exeflat.c
        bcc -Md -0 $< -o $@

-----exeflat.c-----
/*
 * NON-PORTABILITY WARNING: Currently this program requires a little-endian
 * architecture to work...
 */

#include <stdio.h>
#include <stdlib.h>

#define BSIZ            8192

typedef unsigned short Word;
typedef unsigned long DWord;

static FILE *inf = 0, *outf = 0;
static Word hdr[14];
static Word buf[BSIZ / 2];
static Word rel_fact;

static void die(msg)
char *msg;
{
  fprintf(stderr, "%s\n", msg);
  exit(1);
}

int main(argc, argv)
int argc;
char **argv;
{
  DWord hdr_len;
  DWord left;
  Word bytes;

  if (argc != 4)
    die("usage: exeflat (src.exe) (dest.sys) (relocation-factor)");
  rel_fact = strtol(argv[3], 0, 0);
  inf = fopen(argv[1], "rb");
  if (!inf)
    die("open error");
  if (fread(hdr, 1, 28, inf) != 28 || *hdr != 0x5a4d)
    die("no .exe header");
  outf = fopen(argv[2], "w+b");
  if (!outf) die("create error");
  hdr_len = (DWord)hdr[4] << 4;
  printf("header len = %lu = 0x%lx\n", hdr_len, hdr_len);
  printf("header len = %lu = 0x%lx\n", hdr_len, hdr_len);
  fseek(inf, hdr_len, SEEK_SET);
  left = ((DWord)(hdr[2] - 1) << 9) + hdr[1] - hdr_len;
  printf("image size (less header) = %lu = 0x%lx\n", left, left);
  while (left) {
    bytes = left < BSIZ ? left : BSIZ;
    fread(buf, 1, bytes, inf);
    fwrite(buf, 1, bytes, outf);
    left -= bytes;
  }
  printf("first relocation offset = %u = 0x%x\n", hdr[13], hdr[13]);
  fseek(inf, (DWord)hdr[12], SEEK_SET);
  while (hdr[3]--) {
    fread(buf, 1, 4, inf);
    printf("relocation at 0x%04x:0x%04x\n", buf[1], buf[0]);
    fseek(outf, ((DWord)buf[1] << 4) + buf[0], SEEK_SET);
    fread(buf, 1, 2, outf);
    fseek(outf, -2l, SEEK_CUR);
    *buf += rel_fact;
    fwrite(buf, 1, 2, outf);
  }
  fclose(inf);
  fclose(outf);
  return 0;
}


____________________________________________________________________
Get free e-mail and a permanent address at http://www.netaddress.com/?N=1

 *** List info, status, FAQ, etc.: <http://www.webmonster.net/lists/>

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic