Dynamic runtime code loading

Dynamic runtime code loading

Post by Shane McDanie » Sun, 18 Nov 2001 03:55:51



We have a program that we would like to be able to dynamically load
functions into.  It is c++ running on sparc solaris 8.

The scenario we have is that our application is given the command string
"foo", it looks to see if function "foo" currently exists in the system,
if not it looks in a directory to see if there is a file named "foo.o" (or
"foo.so" not sure what works best), if so it loads in the file then calls
the function foo.

My questions are.

1. Is there a way to dynamically load code at runtime.

and if so

2. Is there a way to find out the address of a function residing in the
above dynamically loaded code so that it can actually be executed.

-shane

 
 
 

Dynamic runtime code loading

Post by David Schwart » Sun, 18 Nov 2001 03:59:23



> My questions are.

> 1. Is there a way to dynamically load code at runtime.

        'man dlsym'

Quote:> and if so

> 2. Is there a way to find out the address of a function residing in the
> above dynamically loaded code so that it can actually be executed.

        'man dlsym'

     The following example shows how one can  use   dlopen()  and
     dlsym() to access either function or data objects.  For sim-
     plicity, error checking has been omitted.

     void       *handle;
     int        *iptr, (*fptr)(int);

     /* open the needed object */
     handle = dlopen("/usr/home/me/libfoo.so.1", RTLD_LAZY);
      /* find the address of function and data objects */
     fptr = (int (*)(int))dlsym(handle, "my_function");
     iptr = (int *)dlsym(handle, "my_object");

     /* invoke function, passing value of integer as a parameter */
     (*fptr)(*iptr);

        DS

 
 
 

Dynamic runtime code loading

Post by Ron Natali » Sun, 18 Nov 2001 04:05:37


Please remove comp.lang.C++ from this discussion.  It is not a C++ issue.

Quote:

> 1. Is there a way to dynamically load code at runtime.

Look up dlopen()

Quote:> 2. Is there a way to find out the address of a function residing in the
> above dynamically loaded code so that it can actually be executed.

Look up dlsym()
 
 
 

Dynamic runtime code loading

Post by Shane McDanie » Sun, 18 Nov 2001 05:36:16




> Please remove comp.lang.C++ from this discussion.  It is not a C++ issue.

> > 1. Is there a way to dynamically load code at runtime.

> Look up dlopen()

> > 2. Is there a way to find out the address of a function residing in the
> > above dynamically loaded code so that it can actually be executed.

> Look up dlsym()

Thanks to David and Ron.
 
 
 

1. Q: Loading code at runtime ?

Hello and thanks for reading this message !

Is there a way to load object files at runtime and use it ? Yes, DLL
do this as far as I know, but I can not have two different versions
of the same dll at the same time, can I ?

I'm speaking of something similar to the module mechanism the kernel
uses. I think of a function that loads an object file into memory,
performs relocation of the code (as far as necessary ?) and then it
should be possible to convert a (function) name contained in that
object file to a valid pointer. Is there such a mechanism ?

best regards,

Andre Holzner

----------------------------------------------------------------------


2. Trouble mounting internal IDE Zip disk

3. Loading and Executing Object Code at Runtime

4. extracting information

5. Q: dynamic loading of C++ code to Python --- how to call global constructors?

6. How to lock Terminal ?

7. dynamic loading: linker bug w.r.t. --export-dynamic ?

8. where is boot.b

9. dynamic loading vs. dynamic linking

10. How to do dynamic linking at runtime ?

11. Runtime dynamic linking.

12. editres2: Loading resources (i.e. new language) at runtime!

13. Loading libraries run-time: dlopen, dlsym, dlclose