Technique:Swift metadata sections
| On this wiki | Facts · Code · Notes · Topics |
|---|
Swift 5 binaries on Apple platforms carry type, protocol and reflection metadata in `__TEXT,__swift5_*` sections (types, protos, proto, fieldmd, reflstr, typeref ...), enabling recovery of type and field names from stripped binaries.
Runtime registration sections (Mach-O, segment __TEXT)
__swift5_types (array of 32-bit relative pointers to nominal type descriptors), __swift5_types2 (additional type references), __swift5_protos (protocol descriptors), __swift5_proto (protocol conformance records), __swift5_replace / __swift5_replac2 (dynamic replacements), __swift5_acfuncs (accessible functions). On ELF the names are not simply the Mach-O names without underscores (swiftlang/swift commit d06df163fb9e1030e218674378689ce7ad887e9c, lib/IRGen/GenDecl.cpp): __swift5_types and __swift5_types2 are swift5_type_metadata and swift5_type_metadata_2 (lines 4781-4787), __swift5_protos is swift5_protocols (4544-4548), __swift5_proto is swift5_protocol_conformances (4695-4699), while swift5_replace, swift5_replac2 and swift5_entry keep their short names; the reflection sections (GenReflection.cpp line 1625) are swift5_<fieldmd|builtin|assocty|capture|reflstr|typeref>. On COFF the names are .sw5<fourcc>$B, for example .sw5tymd$B (types), .sw5prt$B (protocols), .sw5prtc$B (conformances).
Reflection sections
__swift5_fieldmd (field descriptors: field names and mangled type references per struct, class, enum), __swift5_assocty (associated type witnesses), __swift5_builtin, __swift5_capture, __swift5_typeref (mangled type names), __swift5_reflstr (reflection strings: field names). lib/IRGen/GenDecl.cpp (swiftlang/swift commit d06df163fb9e1030e218674378689ce7ad887e9c) line 2315 sets the section __TEXT, __swift5_entry, regular, no_dead_strip (ELF swift5_entry, COFF .sw5entr$B), so __swift5_entry exists. The six reflection names above are exactly those built by getReflectionSectionName in lib/IRGen/GenReflection.cpp lines 1638-1668 (fieldmd, builtin, assocty, capture, reflstr, typeref). Unverified: __swift5_mpenum (multi-payload enums): it does not appear in those two files.
Practical use
Records use relative 32-bit offsets (relative to the field's own address, low bits sometimes flags such as indirect), so parse them by address arithmetic. Type descriptors give kind (struct, class, enum), name, parent, fields. Swift classes visible to Objective-C also appear in Technique:Objective-C runtime metadata (class bits FAST_IS_SWIFT_STABLE / LEGACY). Symbols are Swift-mangled ($s prefix, older _T0); the swift demangle tool decodes them. Source: swiftlang/swift stdlib/public/runtime/ImageInspectionCommon.h and include/swift/RemoteInspection.
See Platform:iOS, Platform:macOS, Format:Mach-O.
Topics
- Name mangling — How Swift symbols are mangled ($s prefix, entity and kind letters, standard substitutions, word substitution) and how to read or demangle them, from…
Code & repositories
- https://github.com/swiftlang/swift @
d06df163fb9e1030e218674378689ce7ad887e9c· license: Apache-2.0
Swift compiler source: docs/ABI/Mangling.rst
Tested on: source read, not run: source read, not run · checked 2026-10-18; last activity 2026-10-18