Next: , Previous: gnatdll Example, Up: Using gnatdll

G.11.7.2 gnatdll behind the Scenes

This section details the steps involved in creating a DLL. gnatdll does these steps for you. Unless you are interested in understanding what goes on behind the scenes, you should skip this section.

We use the previous example of a DLL containing the Ada package API, to illustrate the steps necessary to build a DLL. The starting point is a set of objects that will make up the DLL and the corresponding ALI files. In the case of this example this means that api.o and api.ali are available. To build a relocatable DLL, gnatdll does the following:

  1. gnatdll builds the base file (api.base). A base file gives the information necessary to generate relocation information for the DLL.
              $ gnatbind -n api
              $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base

    In addition to the base file, the gnatlink command generates an output file api.jnk which can be discarded. The -mdll switch asks gnatlink to generate the routines DllMain and DllMainCRTStartup that are called by the Windows loader when the DLL is loaded into memory.

  2. gnatdll uses dlltool (see Using dlltool) to build the export table (api.exp). The export table contains the relocation information in a form which can be used during the final link to ensure that the Windows loader is able to place the DLL anywhere in memory.
              $ dlltool --dllname api.dll --def api.def --base-file api.base \
                        --output-exp api.exp
  3. gnatdll builds the base file using the new export table. Note that gnatbind must be called once again since the binder generated file has been deleted during the previous call to gnatlink.
              $ gnatbind -n api
              $ gnatlink api -o api.jnk api.exp -mdll
  4. gnatdll builds the new export table using the new base file and generates the DLL import library libAPI.dll.a.
              $ dlltool --dllname api.dll --def api.def --base-file api.base \
                        --output-exp api.exp --output-lib libAPI.a
  5. Finally gnatdll builds the relocatable DLL using the final export table.
              $ gnatbind -n api
              $ gnatlink api api.exp -o api.dll -mdll