LinuxCommandLibrary

dlltool

Create Windows DLL import libraries

SYNOPSIS

dlltool [options] [objfile...]

PARAMETERS

-l, --output-lib
    Specify the name of the generated import library file (e.g., mylib.lib).

-z, --output-def
    Specify the name of the generated definition file (e.g., my.def).

-d, --dllname
    Specify the name of the DLL that this import library is for (e.g., mydll.dll).

-e, --export-all-symbols
    Export all global symbols found in the input object files. Useful for quick DLL creation.

-U, --add-underscore
    Add an underscore prefix to all exported symbols. Common in some Windows API conventions.

-k, --kill-at
    Remove trailing '@N' from symbols, where N is the number of bytes of parameters. Used for stdcall function names.

-f, --no-default-nodefs
    Do not automatically generate a default .def file based on input objects; an input .def file might be provided instead.

--input-def
    Read symbol definitions from an existing .def file. This allows precise control over exports.

--add-stdcall-alias
    For stdcall functions, add an alias that doesn't include the '@N' suffix.

DESCRIPTION

The dlltool command, part of the GNU Binutils suite, is a crucial utility for developing applications targeting Microsoft Windows from non-Windows platforms, primarily Linux. It is instrumental in the cross-compilation process, especially when using toolchains like MinGW (Minimalist GNU for Windows).

Unlike standard Unix-like shared libraries (.so files), Windows Dynamic Link Libraries (.dll files) require specific mechanisms for symbol exporting and importing. dlltool bridges this gap by processing COFF (Common Object File Format) object files to generate the necessary components for linking against DLLs.

Its primary outputs are import libraries (.lib files) and definition files (.def files). The .lib file is a static library that contains stubs for the functions exported by the DLL, allowing the linker to resolve references at compile time. The .def file describes the DLL's exports and is used by the linker to create the actual DLL. By automating the creation of these files, dlltool enables seamless integration of Windows DLLs into cross-compiled projects.

CAVEATS

dlltool is specifically designed for creating Windows DLLs and import libraries. It does not directly create Linux shared libraries (.so files); for those, the ld linker is used with appropriate options. It expects input object files to be in a format compatible with Windows (typically COFF or PE), usually generated by cross-compilers like those provided by MinGW. Its usage is highly specialized for cross-platform development.

ROLE IN MINGW TOOLCHAIN

In a MinGW (Minimalist GNU for Windows) cross-compilation toolchain, dlltool plays a vital role. After source code is compiled into COFF object files by the cross-compiler (e.g., i686-w64-mingw32-gcc), dlltool is used to generate the necessary .lib and .def files. These files are then used by the cross-linker (i686-w64-mingw32-ld) to create the final Windows DLLs or executables that link against those DLLs. This makes the entire process of building Windows applications on Linux seamless and efficient.

IMPORT LIBRARIES VS. SHARED LIBRARIES

It's important to understand the distinction dlltool manages. On Linux, a shared library (.so) is directly linked against, and symbols are resolved at runtime or load time using the ELF dynamic linking mechanism. On Windows, when linking against a DLL, the linker typically uses an import library (.lib file). This .lib file is a small static library containing 'stubs' or 'thunks' that redirect calls to the actual functions within the DLL at runtime. dlltool's primary function is to generate these crucial .lib files from the object files that comprise the DLL, along with optional .def files that define which symbols should be exported by the DLL.

HISTORY

dlltool is an integral part of the GNU Binutils collection, a set of programming tools for creating and managing binary programs, libraries, and object files. Its development arose from the need to support cross-compilation environments, particularly for targeting Microsoft Windows from Unix-like systems. This became increasingly important with the rise of projects like Cygwin and later MinGW, which aimed to provide a GNU development environment for Windows. dlltool specifically addresses the unique requirements of Windows's DLL linking model, which differs significantly from the ELF-based shared library model prevalent on Linux and other Unix systems. It has been a stable component of Binutils for many years, facilitating countless cross-platform projects.

SEE ALSO

ld(1), ar(1), objdump(1), readelf(1), windres(1), gcc(1)

Copied to clipboard