TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Bill Birrell
from: George White
date: 1998-09-29 23:00:02
subject: music

Hi Bill,

BB> > tacking data to the end of dos exe files:

BB> > BB>     Seems reasonable, but it's Dos, so there are umpteen
ways, I gues
BB> > BB> :-) I haven't tried to tack stuff on to the end of an
executable file
BB> > BB> Would the relocation not just overwrite it?

BB> > It gets "ignored" you have to read from disk as it
BB> > doesn't get loaded into
BB> > memory, unless it's a .com format file in which case
BB> > there's no relocation.

BB>    You lost me, Jasen. A file is a file. You append to it, it gets longer.
BB>If it's an exe file you are extending, the code segment of
BB>the exe file is relocated by the loader, which loads the
BB>entire extended file from disk. That uses the header for
BB>relocation doesn't it? It knows nothing about the appended
BB>data. Ergo the question arose. I'm not sure you've answered
BB>it or even understood what I was getting at. My comments
BB>were a bit terse and telegraphic. :-) When I get time, I'll
BB>try it for myself, but the busy season is off with a bang.
BB>:-)

You're missing the point. The program loader loads the header (.EXE
load module), and that tells it how big the relocation tables are (in
16 byte paragraphs as a short int at offset 0x000A), how big the file
is (including the header, in 512 byte pages as a short int at 0x0006)
and how much of the last page is used (in bytes as an short int at
0x0004). The loader then skips over the relocation tables and loads the
program segments, then applies the relocations

If you look at an .exe file with debug information included it is
appended after the code.

As any data appended to the program is ignored by the loader, you can
put whatever you like there (provided you don't want debug information
if you have Borland - I haven't checked the other compilers). You can
find it's offset by reading the information in 0x0004 and 0x0002,
calculating the offset in the exe file and seeking to it and reading
from there.

From one of my .exe files.

According to DIR: File size = 453122

From the file header:

Offset   Value
0x0002 = 0x0002
0x0004 = 0x0376

0x0376 = 886 pages (= 885 _full_ pages)
885 * 512 = 453120
+ the 0x0002 bytes used on the last page = 453122

Here's a test file, compile and link it with no debug information.
Then copy it + a suitable text file to a new .exe file (using the /b
copy option).
Then run the new .exe file, it'll display up to the first 79 characters
of the added text file.

/* Copyright 1998, G. White */
/* Simple demo code for Bill Birrell */
/* Free for anyone to use at your own risk */

#include 
#include 

int main (int argv, char *argc[])
{
FILE *exe_file;
unsigned short last_page,paras;
int     data_read;
long data_offset;
char text[80];

printf ("\n");

if (NULL == (exe_file = fopen (argc[0],"rb")))
    {
    fprintf (stderr,"Unable to open exe file %s!!!\n",argc[0]);
    return 1;
    }
if (1 != fread (&last_page,sizeof (short),1,exe_file)) /* Get rid of MZ */
    {
    fprintf (stderr,"Unable to read MZ marker!!!\n");
    return 2;
    }
if (1 != fread (&last_page,sizeof (short),1,exe_file))
    {
    fprintf (stderr,"Unable to read last page use!!!\n");
    return 3;
    }
if (1 != fread (¶s,sizeof (short),1,exe_file))
    {
    fprintf (stderr,"Unable to read number of pages used!!!\n");
    return 4;
    }

data_offset = ((long) paras - 1L) * 512L + (long) last_page;
/* I know, there are redundant casts there, only the first */
/* cast is essential, after that they are redundant.       */
if (fseek (exe_file,data_offset,SEEK_SET))
    {
    fprintf (stderr,"Unable to seek to display text!!!\n");
    return 5;
    }

if (0 == (data_read = fread (text,sizeof (char),79,exe_file)))
    printf ("No data found to display");
else
    {
    text[data_read] = 0;
    printf ("Text found is:\n");
    printf (text);
    }
printf ("\n");

return 0;
}

/* End of code */

George

 * SLMR 2.1a * Computers eliminate spare time.

--- Maximus/2 3.01
* Origin: DoNoR/2,Woking UK (44-1483-717904) (2:440/4)
SEEN-BY: 396/1 632/0 371 633/260 267 270 371 634/397 635/506 728 810 639/252
SEEN-BY: 670/218
@PATH: 440/4 255/1 251/25 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.