Metal Library Archive
MetalLibraryArchive is a product of reverse-engineering Apple's metallib file format.
You can use MetalLibraryArchive to get the library type, target platform, Metal functions, etc., from a metallib file.
The extracted information of a Metal function includes:
- Function name.
- Function type - vertex, fragment, kernel, extern, etc.
- Metal Shading Language version of the function.
- Bitcode of the function which can be converted into human-readable LLVM assembly language using llvm-dis.
- Source code of the function if the
metallib is configured to include source code.
🎈 Usage
Web App
Available at: https://yuao.github.io/MetalLibraryExplorer
Learn more
Explorer App
An executable target called "Explorer" is included in the package. "Explorer" is a GUI app that can open, unpack and disassemble (with the help of llvm-dis) metallib files.
Note llvm-dis is not included, you can get a copy of the binary at https://github.com/llvm/llvm-project/releases
Use the "Disassembler" menu in the app to locate the llvm-dis executable file.

Library
You can also use MetalLibraryArchive as a library:
import MetalLibraryArchive
let archive = try Archive(data: Data(contentsOf: metallibURL))
let libraryType = archive.libraryType
let functions = archive.functions
🚧 Metal Library Archive Binary Layout
Header
| Byte Range |
Type |
Content |
| 0...3 |
FourCharCode |
MTLB |
| 4...5 |
UInt16 |
Target platform |
| 6...9 |
(UInt16, UInt16) |
Version of the metallib file (major, minor) |
| 10 |
UInt8 |
Type of the metallib file |
| 11 |
UInt8 |
Target OS |
| 12...15 |
(UInt16, UInt16) |
Version of the target OS (major, minor) |
| 16...23 |
UInt64 |
Size of the metallib file |
| 24...39 |
(UInt64, UInt64) |
Offset and size of the function list |
| 40...55 |
(UInt64, UInt64) |
Offset and size of the public metadata section |
| 56...71 |
(UInt64, UInt64) |
Offset and size of the private metadata section |
| 72...87 |
(UInt64, UInt64) |
Offset and size of the bitcode section |
| Target Platform |
Value |
| macOS |
0x8001 (0x01,0x80) |
| iOS |
0x0001 (0x01,0x00) |
| metallib Type |
Value |
| Executable |
0x00 |
| Core Image |
0x01 |
| Dynamic |
0x02 |
| Symbol Companion |
0x03 |
| Target OS |
Value |
| Unknown |
0x00 |
| macOS |
0x81 |
| iOS |
0x82 |
| tvOS |
0x83 |
| watchOS |
0x84 |
| bridgeOS (Probably) |
0x85 |
| macCatalyst |
0x86 |
| iOS Simulator |
0x87 |
| tvOS Simulator |
0x88 |
| watchOS Simulator |
0x89 |
Function List
| Byte Range |
Type |
Content |
| 0...3 |
UInt32 |
Entry count (the number of functions) |
| 4... |
Tag Groups |
Each tag group holds some information about a Metal function |
The number of tag groups equals the number of functions.
Tag Group
| Byte Range |
Type |
Content |
| 0...3 |
UInt32 |
Size of the tag group |
| 4... |
Tags |
|
Tag
| Byte Range |
Type |
Content |
| 0...3 |
FourCharCode |
Name of the tag |
| 4...5 |
UInt16 |
Size of the tag |
| 6... |
Bytes |
Content of the tag |
Function Information Tags
| Name |
Content Data Type |
Content |
| NAME |
NULL-terminated C-style string |
Name of the function |
| MDSZ |
UInt64 |
Size of the bitcode |
| TYPE |
UInt8 |
Type of the function |
| HASH |
SHA256 Digest |
Hash of the bitcode data (SHA256) |
| OFFT |
(UInt64, UInt64, UInt64) |
Offsets of the information about this function in the public metadata section, private metadata section, and bitcode section |
| SOFF |
UInt64 |
Offset of the source code archive of the function in the embedded source code section |
| VERS |
(UInt16, UInt16, UInt16, UInt16) |
Bitcode and language versions (air.major, air.minor, language.major, language.minor) |
| LAYR |
UInt8 |
Metal type of the render_target_array_index (for layered rendering) |
| TESS |
UInt8 |
Patch type and number of control points per-patch (for post-tessellation vertex function) |
| ENDT |
|
End of the tag group |
| Function Type |
Value |
Note |
| Vertex |
0x00 |
|
| Fragment |
0x01 |
|
| Kernel |
0x02 |
|
| Unqualified |
0x03 |
Functions in Metal dynamic library |
| Visible |
0x04 |
Functions with [[visible]] or [[stitchable]] attributes |
| Extern |
0x05 |
Extern functions complied with -fcikernel option |
| Intersection |
0x06 |
|
Content of the TESS tag:
// Patch types:
// - triangle: 1
// - quad: 2
let content: UInt8 = controlPointCount << 2 | patchType
Public Metadata
Contains information about function constants, tessellation patches, return types, etc.
Tags: CNST, VATT, VATY, RETR, ARGR, etc.
Private Metadata
Contains paths to the shader source (DEBI tag) and .air (DEPF tag) files.
Header Extension
Only exists if FunctionListOffset + FunctionListSize + 4 != PublicMetadataOffset
| Byte Range |
Type |
Content |
FunctionListOffset + FunctionListSize + 4... |
Tags |
Header extension tags |
Header Extension Tags
| Name |
Type |
Content |
| HDYN |
(UInt64, UInt64) |
Offset and size of the dynamic header section |
| VLST |
(UInt64, UInt64) |
Offset and size of the exported variable list |
| ILST |
(UInt64, UInt64) |
Offset and size of the imported symbol list |
| HSRD/HSRC |
(UInt64, UInt64) |
Offset and size of the embedded source code section |
| UUID |
UUID |
UUID of the Metal library. |
| ENDT |
|
End of the header extension |
Dynamic Header Section Tags
| Name |
Content Data Type |
Content |
| NAME |
NULL-terminated C-style string |
Install name of the library |
| DYNL |
NULL-terminated C-style string |
Linked dynamic library |
Variable List & Imported Symbol List
Variable list and imported symbol list have structures that are similar to that of the function list.
Embedded Source Code Section
Only exists if the metallib build process is configured to include source code.
| Byte Range |
Type |
Content |
| 0...1 |
UInt16 |
Number of items in this section |
| 2...n |
NULL-terminated C-style string |
Link options of the metallib file |
| n...m |
NULL-terminated C-style string |
Working directory |
| m... |
Tag Group |
SARC tag |
Note "Working directory" only exists in HSRD.
Note SARC tag uses 4-bytes (UInt32) content size.
Content of the SARC tag:
| Byte Range |
Type |
Content |
| 0...n |
NULL-terminated C-style string |
ID of the source code archive |
| n... |
BZh |
Bzip2 compressed source code archive |
Metal Data Type Table
| Value |
Type |
Value |
Type |
| 0x00 |
None |
0x01 |
Struct |
| 0x02 |
Array |
0x03 |
Float |
| 0x04 |
Float2 |
0x05 |
Float3 |
| 0x06 |
Float4 |
0x07 |
Float2x2 |
| 0x08 |
Float2x3 |
0x09 |
Float2x4 |
| 0x0A |
Float3x2 |
0x0B |
Float3x3 |
| 0x0C |
Float3x4 |
0x0D |
Float4x2 |
| 0x0E |
Float4x3 |
0x0F |
Float4x4 |
| 0x10 |
Half |
0x11 |
Half2 |
| 0x12 |
Half3 |
0x13 |
Half4 |
| 0x14 |
Half2x2 |
0x15 |
Half2x3 |
| 0x16 |
Half2x4 |
0x17 |
Half3x2 |
| 0x18 |
Half3x3 |
0x19 |
Half3x4 |
| 0x1A |
Half4x2 |
0x1B |
Half4x3 |
| 0x1C |
Half4x4 |
0x1D |
Int |
| 0x1E |
Int2 |
0x1F |
Int3 |
| 0x20 |
Int4 |
0x21 |
UInt |
| 0x22 |
UInt2 |
0x23 |
UInt3 |
| 0x24 |
UInt4 |
0x25 |
Short |
| 0x26 |
Short2 |
0x27 |
Short3 |
| 0x28 |
Short4 |
0x29 |
UShort |
| 0x2A |
UShort2 |
0x2B |
UShort3 |
| 0x2C |
UShort4 |
0x2D |
Char |
| 0x2E |
Char2 |
0x2F |
Char3 |
| 0x30 |
Char4 |
0x31 |
UChar |
| 0x32 |
UChar2 |
0x33 |
UChar3 |
| 0x34 |
UChar4 |
0x35 |
Bool |
| 0x36 |
Bool2 |
0x37 |
Bool3 |
| 0x38 |
Bool4 |
0x3A |
Texture |
| 0x3B |
Sampler |
0x3C |
Pointer |
| 0x3E |
R8Unorm |
0x3F |
R8Snorm |
| 0x40 |
R16Unorm |
0x41 |
R16Snorm |
| 0x42 |
RG8Unorm |
0x43 |
RG8Snorm |
| 0x44 |
RG16Unorm |
0x45 |
RG16Snorm |
| 0x46 |
RGBA8Unorm |
0x47 |
RGBA8Unorm_sRGB |
| 0x48 |
RGBA8Snorm |
0x49 |
RGBA16Unorm |
| 0x4A |
RGBA16Snorm |
0x4B |
RGB10A2Unorm |
| 0x4C |
RG11B10Float |
0x4D |
RGB9E5Float |
| 0x4E |
RenderPipeline |
0x4F |
ComputePipeline |
| 0x50 |
IndirectCommandBuffer |
0x51 |
Long |
| 0x52 |
Long2 |
0x53 |
Long3 |
| 0x54 |
Long4 |
0x55 |
ULong |
| 0x56 |
ULong2 |
0x57 |
ULong3 |
| 0x58 |
ULong4 |
0x59 |
Double |
| 0x5A |
Double2 |
0x5B |
Double3 |
| 0x5C |
Double4 |
0x5D |
Float8 |
| 0x5E |
Float16 |
0x5F |
Half8 |
| 0x60 |
Half16 |
0x61 |
Int8 |
| 0x62 |
Int16 |
0x63 |
UInt8 |
| 0x64 |
UInt16 |
0x65 |
Short8 |
| 0x66 |
Short16 |
0x67 |
UShort8 |
| 0x68 |
UShort16 |
0x69 |
Char8 |
| 0x6A |
Char16 |
0x6B |
UChar8 |
| 0x6C |
UChar16 |
0x6D |
Long8 |
| 0x6E |
Long16 |
0x6F |
ULong8 |
| 0x70 |
ULong16 |
0x71 |
Double8 |
| 0x72 |
Double16 |
0x73 |
VisibleFunctionTable |
| 0x74 |
IntersectionFunctionTable |
0x75 |
PrimitiveAccelerationStructure |
| 0x76 |
InstanceAccelerationStructure |
0x77 |
Bool8 |
| 0x78 |
Bool16 |
|
|
❤️ Contributing