NO

Author Topic: Wrong RVA generation for DLL double declaration  (Read 6999 times)

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Wrong RVA generation for DLL double declaration
« on: January 17, 2005, 01:41:23 PM »
Hello Pelle,
Happy new year to you and all friends of this community.
Hoping to be helpful to perfectionate your compiler here I give you a little more work to do. Using version 2.90.1 (but I think this problem is present also in the previous versions) I find a bug in RVA generation in the calling module toward external DLL functions.
Consider a simple program that call a function in an external module (i.e. test_function) you can declare this function trough prototypes as:
Code: [Select]
void test_function(void);
or (more efficently) as:
Code: [Select]
__cdecl(dllimport) void test_function(void);
and everything works fine, but somthing wrong happen if for some mistake both declarations are applied in the same module.
When the executable try to call the external function the program hangs with an access violation.
After some checks I found that the error is due to a wrong RVA generation from the export address table that wrongly points to the JMP instuction code (byte code 0xFF25) in the jump table. Due to this the relocation from the loader modify the first 4 bytes instead of the last 4 of the jump table.
The problem is not easy visible becouse no error nor warning is generated in the compiler when it founds the two declarations.
At your option to add an error or correct the compiler code to always generate the correct RVA address.

Best regards
F.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Wrong RVA generation for DLL double declaration
« Reply #1 on: January 24, 2005, 11:44:30 AM »
:oops: Sorry,
When I wrote:
Code: [Select]
__cdecl(dllimport) void test_function(void);
I was wrong I meant:
Code: [Select]
__declspec(dllimport) void * test_function(void);
As you can easily check the code bombs.
Regards
F.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Wrong RVA generation for DLL double declaration
« Reply #2 on: January 24, 2005, 11:46:28 AM »
:cry:
Wrong again, it really is:
Code: [Select]
__declspec(dllimport) void test_function(void);
bye
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Wrong RVA generation for DLL double declaration
« Reply #3 on: January 24, 2005, 01:45:09 PM »
Hello Frankie,

I will look at it, and try to fix it in v3.0. Thanks for the feedback!

Pelle
/Pelle

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Wrong RVA generation for DLL double declaration
« Reply #4 on: January 26, 2005, 09:01:30 PM »
Hello Frankie,

Yup. The following order will generate bad code...

Code: [Select]

void test_function(void);
__declspec(dllimport) void test_function(void);


...while the following order will always work...

Code: [Select]

__declspec(dllimport) void test_function(void);
void test_function(void);


I will now generate an error - it's the only way to handle it. Will be in the next beta.

Pelle
/Pelle

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Wrong RVA generation for DLL double declaration
« Reply #5 on: January 26, 2005, 09:49:52 PM »
Ok Pelle,
I think that an error generation due to prototype redefinition is the best solution.
Thank-you
Regards
F.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide