[comp.sys.ibm.pc.misc] Finding the offset of the DATA SEGMENT in a TurboC executable...

cs00chs@unccvax.uncc.edu (charles spell) (10/22/90)

I have previously posted a question on finding the offset of the data segment
of an executable...

Most people were thinking that I wanted to find this out after the executable
has been loaded...what I want to do is find the DS file offset in
the executable file itself BEFORE it's loaded...

Specifically, let's say I had a global variable 'int count;' compiled
in TEST.EXE.  If I knew where the start of my initialized data was I could
write the following program that simply increments whatever 'count' is
initialized to (in TEST.EXE)...
/*
 * incrcnt.c -> incrcnt.exe - a program mto increment 'count' in file
 * TEST.EXE
 */
main()
{
    FILE    *fp;
    long    fp_offset;          /* file offset to initialized data segment */
    int     hold;

fp = fopen("TEST.EXE", "r+b");  /* in use i would test for (FILE *) NULL    */
fp_offset = find_dseg(fp);      /* find_dseg() - HOW DO I DO THIS???        */
fp_offset +=find_var(fp, "count");  /* find variable "count" in TEST.EXE    */
fseek(fp, fp_offset, 0);        /* should always fseek before fread()       */
fread(&hold, sizeof(hold), 1, fp);
hold++;                         /* increment variable... */
fseek(fp, fp_offset, 0);        /* should always fseek before fwrite()!!    */
fwrite(&hold, sizeof(hold), 1, fp);
fclose(fp);
}

find_var() would be similar to UNIX nlist(3).
-- 
 .--------------------------.  ...  |On the border of your mind lies a place
 |uunet!mcnc!unccvax!cs00chs|  (")  |where dreams and reality are one...I will 
 `--------------------------'-w-U-w-|take you there, for I am the subject...
 \%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\|the subject of your imagination. -Aldo Nova