Introduction
On Windows, a .lib file is a library for the linker, made by the tools in
Microsoft Visual Studio and by
compatible ones such as LLVM. It comes in two kinds that share one format, an ar
archive. A static library holds
COFF object files, and the linker copies the
ones a program needs into it. An import library holds no code: it tells the linker
which functions a DLL exports, usually as short import objects.
Other formats that use .lib
Libraries from 16-bit DOS and Windows compilers also use .lib, but they are not archives:
they are a different format that shares only the extension.
- OMF library – a library of OMF object modules from 16-bit compilers, starting with the byte
0xF0, with a dictionary of public names
Variations
| Format | Variation | About | Open |
|---|---|---|---|
.LIB | Windows COFF library | Windows linker libraries: COFF objects or DLL import records in an archive. | Open |
.LIB | Short import object | One member of an import library: a header and two strings. | Open |
Windows COFF library
A static or import library in the Windows form of the ar archive. It starts with
!<arch>\n, and its first two members are both named
/: the first and second linker members, which index the public
symbols.
A Windows-style .a file from MinGW is often a GNU archive instead, with
a single / index, so the first members tell them apart.
Short import object
A member of an import library: a 20-byte header and two strings, the symbol and
the DLL it comes from. It starts with 00 00 FF FF.
Not described
Libraries are archives of object files. The explorer reads COFF libraries and import libraries; the object-file gaps are on the object file overview. The gaps in OMF libraries are on their own page.
Known, not yet covered
- Viewer: the hint or ordinal of a short import is not printed by any LLVM tool, so it was
checked only against the numbers in the
.deffiles that made four test libraries. - Viewer: the link-time-code (anonymous) object members of 9 libraries are not read by
llvm-readobj, so the viewer’s reading of them was not compared. - Viewer: the anonymous-object header layout (32 bytes, 44 for version 2, 56 for bigobj) comes
from mingw-w64’s
winnt.h(read as a raw file), which LLVM’sCOFF.hdoes not have; it also agrees with the sizes of 72 real members. Its meaning for version 2 (whether SizeOfData counts from byte 32 or byte 44) is not stated in any source read. Exactly what the viewer does with a version 2 member: if SizeOfData reaches the end of the member counted from byte 44, it is accepted; if it reaches the end only when counted from byte 32, it is accepted and the member’s own line in the tree says “SizeOfData counted from byte 32” followed by the note (that no source says which of the two it counts from), so the note shows even for a 44-byte member whose data lies inside the header and has no Data node; when there is a Data node it carries the note as well; any other value fits neither reading and is a problem. - Viewer: two display clips remain: the one-line summary of a
.drectvesection in the tree is cut at 200 characters and ends with “…” (the whole section is in its data bytes, and the Linker options list shows every option), and a list of more than 20,000 string-table entries or 8,000 long names ends in a “More strings” or “More names” node that opens to the next entries, chained to the last one. - Viewer: in a library of more than 20,000 members, the members past the first 20,000 are checked when the Members table shows them, and their problems are added to the Problems list then, marked “found after opening”. This works when the library is opened directly, from a folder listing (Choose a folder) or from a dropped folder.
Problems listed since the last check: a short import with name type 5 to 7, with a reserved bit set, with name
type 4 and no third string, or shorter than its 20-byte header and two strings; a COFF symbol or section name that
points past the end of the string table (the problem gives the offset and the table’s size, as LLVM’s
getString does); an anonymous object whose SizeOfData differs from the bytes after its header (for version 2, only when it fits neither reading). Names
of any length, string-table entries and .drectve sections are shown whole.
Compared with LLVM 22 on the 1,193 distinct !<arch> libraries found on this Mac: member
names, sizes and the linker members’ symbol index (llvm-ar, llvm-nm --print-armap), and
for each member, llvm-readobj’s short-import type, name type, DLL, export name and symbols
(54,804 imports) and every COFF symbol (78,072 objects, 2,104,790 symbols). No differences remain. The
comparison found one fault, now fixed: an object whose header states an optional header (old Microsoft import
libraries state 224 bytes) had its section table read from the wrong place.
Links
Software
- Visual Studio – Microsoft’s development environment, with
libto make them anddumpbinto list them (Windows) - LLVM – compiler toolkit with
llvm-lib,llvm-arandllvm-nm(Windows, macOS, Linux) - 7-Zip – free archiver that can list and extract the members (Windows)
Reference
- PE Format – Microsoft’s specification, with sections on the archive and import library formats