Difference between revisions of "HOWTO-Dynamic Modules"

Jump to navigation Jump to search
2,557 bytes added ,  08:17, 16 August 2010
Filled out "subclassing DLObject"
m
(Filled out "subclassing DLObject")
Line 21: Line 21:
# Now we need to get the backend generating custom engine plugin files that can be loaded/unloaded to/from memory at will during runtime. To make the runtime linking (loading) of plugin files easier to handle, we let ld do some of the work for us at compile-time. First, we build the non-relocatable main executable <tt>scummvm.elf</tt> (Manually implementing Dynamic Modules on a smaller target that doesn't allow the main executable to be non-relocatable has not yet been successfully done and, as such, is not covered by this HOWTO). Then we link the plugins files together with a custom ld linker script; ld uses the absolute addresses from the main executable to resolve messy jumps from the plugin back to the main code and thus does much of the more complicated dynamic work for us. I would suggest adding the necessary custom linker script into the <tt>backends/plugins/foobar/</tt> directory you made earlier (See "Making the plugin linker script for your backend" below).
# Now we need to get the backend generating custom engine plugin files that can be loaded/unloaded to/from memory at will during runtime. To make the runtime linking (loading) of plugin files easier to handle, we let ld do some of the work for us at compile-time. First, we build the non-relocatable main executable <tt>scummvm.elf</tt> (Manually implementing Dynamic Modules on a smaller target that doesn't allow the main executable to be non-relocatable has not yet been successfully done and, as such, is not covered by this HOWTO). Then we link the plugins files together with a custom ld linker script; ld uses the absolute addresses from the main executable to resolve messy jumps from the plugin back to the main code and thus does much of the more complicated dynamic work for us. I would suggest adding the necessary custom linker script into the <tt>backends/plugins/foobar/</tt> directory you made earlier (See "Making the plugin linker script for your backend" below).
# Modify the build system for your backend (See "Necessary Build Modifications" below).
# Modify the build system for your backend (See "Necessary Build Modifications" below).
# Cross your fingers and be prepared to work at things for a while if it doesn't immediately function ;-)
# Some miscellaneous things: You should add a platform-specific call to a function that flushes the data cache into <code>static void flushDataCache()</code> in <tt>backends/plugins/elf-loader.cpp</tt>. You will also need to add a check for the proper machine (processor) type into <code>bool DLObject::readElfHeader(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr)</code> in that same file, adding the machine type to elf32.h if it's not already there.
# That's the gist of it; Cross your fingers and be prepared to work at things for a while if it doesn't immediately function ;-).


==== Example of foobar-provider.h====
==== Example of foobar-provider.h====
Line 92: Line 93:


==== Subclassing DLObject ====
==== Subclassing DLObject ====
At some point, the PluginManager will call <code>getPlugins()</code> on the FOOBARProvider, which will trigger a search for plugin files in your backend's file system and return a list of FOOBARPlugins based on the results of this search. The PluginManager will then be able to call <code>loadplugin()</code> and <code>unloadPlugin</code>, and  on any of these FOOBARPlugins, which will involve calls to <code>open(const char *path)</code> on a new object that's a subclass of DLObject (which subclass is determined by how you coded <code>makeDLObject()</code> in the FOOBARPlugin class) with the path to the plugin file the FOOBARPlugin represents as its argument (The multiple levels of abstraction here can get confusing :-)). A subclass of DLObject, then, is ultimately responsible for opening the plugin files and loading them/unloading them from memory.
At some point, the PluginManager will call <code>getPlugins()</code> on the FOOBARProvider, which will trigger a search for plugin files in your backend's file system and return a list of FOOBARPlugins based on the results of this search. The PluginManager will then be able to call <code>loadplugin()</code> and <code>unloadPlugin</code> on any of these FOOBARPlugins, which will involve calls to <code>open(const char *path)</code> on a new object that's a subclass of DLObject (which subclass this will be is determined by how you coded <code>makeDLObject()</code> in your FOOBARPlugin class) with the path to the plugin file the FOOBARPlugin represents as its argument (The multiple levels of abstraction here can get confusing, I know :-)). A subclass of DLObject, then, is ultimately responsible for opening the plugin files and loading them/unloading them from memory.


TODO: Finish explaining what the DLObject class does and how to subclass it effectively.
If there is already a DLObject subclass that matches the processor for your smaller target (i.e <tt>MIPSDLObject</tt>, <tt>ARMDLObject</tt>), use it! Assuming that class is in use for another working backend with dynamic modules implemented, most everything should work correctly. The one function where one should expect problems is <code>bool DLObject::relocate(Common::SeekableReadStream* DLFile, unsigned long offset, unsigned long size, void *relSegment)</code> in that your platform may generate plugins that use relocation types as of yet unsupported by that subclass of DLObject. If this is the case, you will hopefully get a helpful message at runtime, such as "Unknown relocation type 12" or something like that. You should then look up which relocation type this number refers to in the ABI for your processor (relocation types are processor-specific), add a define for the type into elf32.h next to the other relocation types for your processor (i.e. <code>#define R_ARM_ABS32 2</code>), and then add code to handle it into the relocate function (again, the ABI for your processor and pre-existing relocation code will be your friend when trying to figure this out).
 
If there isn't already a DLObject subclass that matches the processor for your smaller target, you will have to make one that overrides <tt>relocate</tt> and <tt>relocateRels</tt>. You can look to these functions in <tt>backends/plugins/arm-loader.cpp</tt> and <tt>backends/plugins/mips-loader.cpp</tt> for a template of how to code for them. Again, the hardest part will be the code for the actual relocations. You'll want to first detect what these relocations are (you can do this using objdump on generated plugin files), then look up how they are supposed to be handled in the ABI for your processor. Since we pre-link to let ld do some of this relocation work for us, you should make builds that don't do this pre-linking with the plugins and ones that do and compare to see exactly what this work is. That way, you won't write relocation code for difficult things the linker already does for you. Careful reading and persistence will pay off at this point :-)
27

edits

Navigation menu