[comp.sys.ibm.pc] Overlays & Function Pointers

cramer@optilink.UUCP (Clayton Cramer) (10/05/88)

Consider this a warning.

I was having trouble getting overlays to work with the Microsoft C V5.1
compiler, and after talking to Microsoft Customer Support, and eventually
to someone in the Microsoft Languages Group, my boss pointed out the
source of the problem.  This is an obscure enough problem that even the
engineer in the Languages Group needed it explained in detail to 
understand it.

If you directly call a function which is overlayable, the Microsoft
linker will correctly replace the call with an INT 3FH ... instruction
to invoke the Overlay Manager.  If you call a function indirectly
(i.e. the function pointer is stored in a table, and you call through
that function pointer) the Microsoft linker can't replace that
function pointer with an INT 3FH call, because there isn't a CALL
instruction there -- just an address.  As a consequence, the overlay
manager isn't invoked, and whatever code happens to be in the overlay
area when the indirect call is made will be executed.  (This is almost
never the code you really want).
-- 
Clayton E. Cramer
..!ames!pyramid!kontron!optilin!cramer

johnl@ima.ima.isc.com (John R. Levine) (10/05/88)

In article <542@optilink.UUCP> cramer@optilink.UUCP (Clayton Cramer) writes:
>Consider this a warning.
>
>[the MS overlay manager fails when you call functions indirectly because
> it works by fudging the call instruction]

Phoenix's PLINK linker has an overlay manager that works just fine when
you call functions indirectly.  Rather than fooling with the code at the
call point, it replaces references to a function's address by references
to a thunk that makes sure the overlay is loaded, then jumps to the actual
code.  Since the thunk has a real address, you can call it any way you want.
-- 
John R. Levine, IECC, PO Box 349, Cambridge MA 02238-0349, +1 617 492 3869
{ bbn | think | decvax | harvard | yale }!ima!johnl, Levine@YALE.something
Rome fell, Babylon fell, Scarsdale will have its turn.  -G. B. Shaw

cramer@optilink.UUCP (Clayton Cramer) (10/06/88)

In article <2735@ima.ima.isc.com>, johnl@ima.ima.isc.com (John R. Levine) writes:
> In article <542@optilink.UUCP> cramer@optilink.UUCP (Clayton Cramer) writes:
> Phoenix's PLINK linker has an overlay manager that works just fine when
> you call functions indirectly.  Rather than fooling with the code at the
> call point, it replaces references to a function's address by references
> to a thunk that makes sure the overlay is loaded, then jumps to the actual
> code.  Since the thunk has a real address, you can call it any way you want.
> -- 
> John R. Levine, IECC, PO Box 349, Cambridge MA 02238-0349, +1 617 492 3869

Very true, and I know that it works because we used PLINK on my last
project, and we used indirect function calls and never had to confront
this problem.  But there is a price -- doesn't every function end up
having a stub in the root, whether it is overlaid or not?



-- 
Clayton E. Cramer
..!ames!pyramid!kontron!optilin!cramer

johnl@ima.ima.isc.com (John R. Levine) (10/06/88)

In article <545@optilink.UUCP> cramer@optilink.UUCP (Clayton Cramer) writes:
>In article <2735@ima.ima.isc.com>, [I wrote]:
>> Phoenix's PLINK linker has an overlay manager that works just fine when
>> you call functions indirectly.  ...
>> It replaces references to a function's address by references
>> to a thunk that makes sure the overlay is loaded, then jumps to the actual
>> code.  ...
>...  But there is a price -- doesn't every function end up
>having a stub in the root, whether it is overlaid or not?

No, PLINK is smart enough only to put in stubs for routines that are
actually called from other overlays.  In practice the cost appears to be
small -- in a program with about 600K of executable code in over a dozen
overlays the overlay table was less than 7K.  The fact that Phoenix's
debugger understands the overlays and lets you put breakpoints in overlays
that aren't loaded yet is a help, too.
-- 
John R. Levine, IECC, PO Box 349, Cambridge MA 02238-0349, +1 617 492 3869
{ bbn | think | decvax | harvard | yale }!ima!johnl, Levine@YALE.something
Rome fell, Babylon fell, Scarsdale will have its turn.  -G. B. Shaw