Mail Archives: djgpp/2001/07/29/20:15:22
Newsgroups: | comp.os.msdos.djgpp
|
From: | dshnv AT www DOT com
|
Subject: | relocating variables in a COFF file
|
Lines: | 58
|
Message-ID: | <0t197.10884$ar1.32744@www.newsranger.com>
|
X-Abuse-Info: | When contacting newsranger.com regarding abuse please
|
X-Abuse-Info: | forward the entire news article including headers or
|
X-Abuse-Info: | else we will not be able to process your request
|
X-Complaints-To: | abuse AT newsranger DOT com
|
NNTP-Posting-Date: | Sun, 29 Jul 2001 20:01:00 EDT
|
Organization: | http://www.newsranger.com
|
Date: | Mon, 30 Jul 2001 00:01:00 GMT
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
Hello,
I've been busy writing a program to support Loadable Modules under DJGPP (I know
there are already other dynamic module loaders out there but I just wanted to
write one myself, for educational reasons). Well anyway, it seems to work quite
well. But there's one major problem I don't understand.
When processing all relocations in a section, I relocate them as
following:(exactly as the COFF docs @ http://www.delorie.com/djgpp/doc/coff
say):
Btw, ``Image'' is the pointer to where the file is loaded in memory (loaded by
read(file, Image, size) (well not exactly but you get the idea :) )
RELOC_ADDR32 relocations:
long relocation_addr;
change = (unsigned long*)(Image + Reloc.r_vaddr);
- get address of symbol referred to (relocation_addr = Image + symbol.e_value)
- add the value currently stored in the location being adjusted.
(relocation_addr += *change)
- Store the value back into the location being adjusted. (*change =
relocation_addr)
RELOC_REL32:
these are mostly imports so they just point to the functions' address in memory
(e.g: if (!strcmp(symbol_name, "_printf") *change = (unsigned long)printf;
Okay, for the most part this works fine. Entry point in the module in this
example is 'init()'.
So when I've got something like this:
int init()
{
printf("hello from module!\n");
printf("bye!\n");
return 42;
}
it all works ok. printf is automatically imported 'n sutff, and it works fine.
However, when I'm adding variables (especially ARRAYS!!, like:
unsigned char list_of_chars[80];) and I want to use them, the contents of these
variables is just rubbish.
things like: list_of_chars[3] = 5; printf("%d", list_of_chars[3]); will output
33734 or something. It just doesn't make any sense. (Btw, these problems really
occur a lot on bigger modules, with a lot of functions, globar vars/local vars,
etc. in it)
Did someone here had the same problem? I've already looked at other dynamic
linking packages but I don't really see what goes wrong. Is there something I'm
missing when relocating variables (I don't treat variables other than
functions), or does somebody know what I'm doing wrong here?
Thanks.
- Raw text -