ScummVM logo Main website - Forums - BuildBot - Doxygen - Planet
Contact us - Buy Supported Games: GOG.com
Log in curved edge

HOWTO-Dynamic ModulesHOWTO-Dynamic Modules

NOTE: many parts of this HOWTO assume the "gsoc2010-plugins" branch has been integrated into trunk.

Introduction

This page is meant as a HOWTO which roughly outlines the steps needed to implement dynamic engine plugins for a previously unsupported backend (port) of ScummVM. This HOWTO only applies to targets that can use the ELF file-format and will generally only be useful for smaller targets that cannot support POSIX plugins.

I will assume that you are at least roughly familiar with ScummVM, and have a fresh checkout of our source code repository. Note that it's strongly advised to base your work on the current development version of ScummVM, and not on a release version. This will ease integration of your work.

Overview

ScummVM has a PluginManager class that potentially keeps track of multiple plugin providers. As of this writing, a StaticPluginProvider is always added to this PluginManager that keeps track of all statically-linked plugins. Your job will be to add a dynamic plugin provider specific to the backend you're working on that will provide for any dynamic plugins that are enabled on that backend and to tell the backend to use this provider in the case that dynamic modules are enabled.

Step by step

For the rest of this document, I will assume you are attempting to implement loadable modules for the "foobar" port of ScummVM :-).

  1. Create a new backends/plugins subdirectory matching the name of the port you're providing for, i.e. backends/plugins/foobar/.
  2. Create a provider file in this subdirectory, such as foobar-provider.h that contains the class declaration of the provider for your port. This class should subtype ELFPluginProvider. This class should nest a class declaration that subtypes ELFPlugin but is specific to your port (i.e. FOOBARPlugin) and override ELFPluginProvider's "createPlugin" method with its own that returns a new FOOBARPlugin object (See example below).
  3. The FOOBARPlugin class needs to override ELFPlugin's "makeDLObject" method and have it return a new object of an appropriate subclass of DLObject. Currently, the subclasses of DLObject are processor-specific, but depending on the port, you may find you have to subclass one of these processor-specific subclasses with a platform-specific one, such as FOOBARDLObject(). (See "Subclassing DLObject" below).
  4. Find where scummvm_main(argc, argv) is invoked in your backend's main and somewhere shortly before it add the FOOBARPluginProvider to the PluginManager, i.e. PluginManager::instance().addPluginProvider(new FOOBARPluginProvider(); (Be sure to guard this call with #ifdef DYNAMIC_MODULES). This, of course, will require that you include the FOOBARPluginProvider header at the top of the file.
  5. 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 scummvm.elf (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 backends/plugins/foobar/ directory you made earlier (See "Making the plugin linker script for your backend" below).
  6. Modify the build system for your backend (See "Necessary Build Modifications" below).
  7. Some miscellaneous things: You should add a platform-specific call to a function that flushes the data cache into static void flushDataCache() in backends/plugins/elf-loader.cpp. You will also need to add a check for the proper machine (processor) type into bool DLObject::readElfHeader(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr) in that same file, adding the machine type to elf32.h if it's not already there.
  8. 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

#if defined(DYNAMIC_MODULES) && defined(FOOBAR)

#include "backends/plugins/elf-provider.h"
#include "backends/plugins/foobarprocessor-loader.h"

class FOOBARPluginProvider : public ELFPluginProvider {
	class FOOBARPlugin : public ELFPlugin {
	public:
		FOOBARPlugin(const Common::String &filename) : ELFPlugin(filename) {}

		DLObject *makeDLObject() { return new FOOBARProcessorDLObject(); }
	};

public:
	Plugin* createPlugin(const Common::FSNode &node) const {
		return new FOOBARPlugin(node.getPath());
	}
};

#endif // defined(DYNAMIC_MODULES) && defined(FOOBAR)

Making the plugin linker script for your backend

To generate this script, first find the linker script used to link a static build of the target platform (usually this is the default script for the linker, which you can dump via the command foobar-compiler-ld --verbose). You'll want to modify this linker in a few key ways.

  • First, you can remove the ENTRY(symbol) command. This command normally tells the linker which instruction should be the first to execute in the program, but since you're generating a plugin that will be linked in and jumped to from the main executable, it is not needed.
  • Next, define an ELF program header for a "plugin" segment to be loaded from the file. Do this by adding the following before the SECTIONS command:
PHDRS
{
   plugin PT_LOAD ;
}
  • Set the start address of the file to be 0 (since this will be linked in at a different address later), usually you can do this by replacing the first line in SECTIONS with . = 0;, though different ld scripts may require different modifications.
  • Place the first section (as listed under the SECTIONS command) into the "plugin" segment you defined in PHDRS by appending :plugin to the first sections-command, i.e .text . : { *(.text) } :plugin. As long as there aren't any other PHDRS, this should ensure the entire linked file is put in the "plugin" segment, since future sections assume they are to be put in the same segment as the previous section unless specified otherwise (via :phdr or :NONE).
  • Find the .ctors and .dtors sections (constructors and destructors) and delete any output-section-commands within them that reference crtbegin or crtend, i.e KEEP (*crtbegin*.o(.dtors)) or KEEP (*(EXCLUDE_FILE (*crtend*.o ) .dtors)). Put ___plugin_ctors = .; as the first output-section-command in the .ctors section and ___plugin_ctors_end = .; as the last output-section-command. Do the same for the .dtors section but with ___plugin_dtors, etc. This gives these symbols values that our run-time loader can use. In the end, .ctors and .dtors should look something like this:
.ctors          :
{
  ___plugin_ctors = .;
  KEEP (*(SORT(.ctors.*)))
  KEEP (*(.ctors))
  ___plugin_ctors_end = .;
}
.dtors          :
{
  ___plugin_dtors = .;
  KEEP (*(SORT(.dtors.*)))
  KEEP (*(.dtors))
  ___plugin_dtors_end = .;
}
  • That's it! If you have trouble with any of these instructions or need to further modify the linker script for something specific to your platform, see [1] for linker script documentation and peruse the various plugin.ld files in the subdirectories of backends/plugins/. TODO: Add stuff about MIPS-specific linker script modifications, namely the "shorts" segment.

Necessary Build Modifications

There will undoubtedly be a number of things specific to your platform to deal with when it comes to modifying the building, but you should definitely add the following, bordered by #ifdef guards to make sure DYNAMIC_MODULES is set to 1 (along with telling the desired engines that you want them to be separate plugin files, i.e. "ENABLE_SCUMM = DYNAMIC_PLUGIN"):

DEFINES += ELF_LOADER_TARGET //also add any other necessary defines
PLUGIN_SUFFIX := .plg //or whatever your suffix is
PLUGIN_EXTRA_DEPS	= $(EXECUTABLE)
PLUGIN_LDFLAGS		+= -nostartfiles -Wl,-q,--just-symbols,$(EXECUTABLE),-T$(srcdir)/backends/plugins/foobar/plugin.ld,--retain-symbols-file,$(srcdir)/backends/plugins/plugin.syms
PRE_OBJS_FLAGS		:= -Wl,--whole-archive
POST_OBJS_FLAGS		:= -Wl,--no-whole-archive

As you can see, these modifications mainly deal with making sure the plugins are dependent on the main executable and use the custom linker script.

You will also need to make sure your main executable is not being overly stripped as the thing doing the stripping may think the executable doesn't use certain symbols simply because they reference portions of code that will be linked in later in a plugin.

TODO: Add stuff about "ONE_PLUGIN_AT_A_TIME" once it is working perfectly.

Subclassing DLObject

At some point, the PluginManager will call getPlugins() 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 loadplugin() and unloadPlugin on any of these FOOBARPlugins, which will involve calls to open(const char *path) on a new object that's a subclass of DLObject (which subclass this will be is determined by how you coded makeDLObject() 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.

If there is already a DLObject subclass that matches the processor for your smaller target (i.e MIPSDLObject, ARMDLObject), 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 bool DLObject::relocate(Common::SeekableReadStream* DLFile, unsigned long offset, unsigned long size, void *relSegment) 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. #define R_ARM_ABS32 2), 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 relocate and relocateRels. You can look to these functions in backends/plugins/arm-loader.cpp and backends/plugins/mips-loader.cpp 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 (a lot of the time for absolute jumps, all you have to do is add the main offset of the plugin). Careful reading and persistence will pay off at this point :-)

 

curved edge   curved edge