Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I export the same function multiple times?

Tags:

dll

delphi

When I export a procedure ha from a library multiple times:

library hahaha;

procedure ha; 
begin 
  Writeln('ha') 
end;

exports
  ha, ha, ha;    
end.

Delphi neatly creates a .dll with three procedures: enter image description here

I was surprised that the ambiguous call GetProcAddress(LoadLibrary('hahaha.dll'), 'ha') doesn't fail.

That made me wonder: is there a case where this is useful?

like image 405
Wouter van Nifterick Avatar asked Mar 23 '15 16:03

Wouter van Nifterick


1 Answers

One place where this is useful is when you wish to export the function multiple times with different names. For instance, if we were attempting to re-implement user32 in Delphi we might have exports like this:

exports
  SetWindowTextA,
  SetWindowTextA name 'SetWindowText';

In your case though you are exporting the same function multiple times with the same name and different ordinal values. Why would you want to do that? I can see no good reason for wanting to do that. However, why should the compiler stop you from doing this? It would take effort from the compiler developer to do so, and what would be gained?

My guess therefore, is that the compiler developer did not implement a block on multiple exports with the same name because either:

  1. They did not consider that case, or
  2. They considered it and determined that the effort involved in blocking multiple same-name imports would not provide enough benefit to justify the development cost.

As for what happens when you call GetProcAddress with a function name that has been exported multiple times, the system will return the first exported function whose name matches.

like image 176
David Heffernan Avatar answered Sep 30 '22 15:09

David Heffernan