Technique:Swift metadata sections/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 swiftlang/swift docs/ABI/Mangling.rst.
Prefixes
Swift 5 and later symbols start with $s; Swift 4.2 used $S, Swift 4.0 _T0 (Mangling.rst). On Mach-O the C-level symbol gets one more underscore, so symbol tables show _$s... (Unverified: this follows from the Mach-O convention of prefixing C names with an underscore; Mangling.rst does not list _$s). $e marks the unstable Embedded Swift mangling, and @__swiftmacro_ prefixes the file names generated for macro expansions. The async entry point is named async_Main.
Reading a name
The grammar is postfix: identifiers come first, the operator describing them last. $s4Test3FooC is class Foo in module Test: each identifier is a decimal length plus text (4Test, 3Foo), and the final letter is the kind.
Kind letters: C class, V struct, O enum, P protocol, a typealias after a name.
Entity suffixes: F function (name, labels and signature before it), v variable followed by an accessor code (g getter, s setter; others for willSet, didSet, read, modify), i subscript plus accessor, fC allocating initializer, fc initializer, fD deallocating deinit, fd deinit, fU explicit closure, fu implicit closure, fA default-argument generator, fi initializer of a non-local variable.
Common global suffixes: N type metadata, Mf full metadata, Ma metadata accessor function, Mn nominal type descriptor, MP metadata pattern, WV value witness table, WP protocol witness table, TW protocol witness thunk, Tj dispatch thunk, TD dynamic dispatch thunk, Td direct method reference, To / TO Objective-C bridging thunks, Tm merged function, Tu async function pointer, TA partial application forwarder.
Standard substitutions
S plus a letter abbreviates standard library types: Si Int, Sb Bool, SS String, Sa Array, SD Dictionary, Sh Set, Sq Optional, Sd Double, Sf Float. Sc plus a letter covers concurrency types (Actor, TaskGroup and similar). Example: $sSS is String itself; $sSi is Int.
Compression
- Back-references:
Aplus an index (26 and above), orAfollowed by lowercase letters ending in an uppercase letter for indexes below 26; repeats can have a count prefix. - Word substitution: an identifier starting with
0uses word references; words are split at underscores and at an uppercase letter that follows a non-uppercase letter, labelleda-z, the last reference in uppercase (max 26 words). Spec example:02Myac1_Bexpands toMyAbcGHI_Def. - Non-ASCII identifiers use
00, the encoded length and a Punycode string (delimiter_, digits 0-9 written A-J). Operators are an identifier followed byoand a fixity (pprefix,Ppostfix,iinfix) with characters mapped to letters. - Generic signatures end in
l; for several parametersr, per-depth counts, thenl.zis depth 0 index 0. Requirement codes:Rconformance,Rbbase class,Rssame type,Rllayout,Rhsame shape.
Symbolic references
In metadata (not in symbol tables) a mangled name may contain control bytes 0x01-0x17 followed by a 4-byte relative reference, and 0x18-0x1F followed by a pointer-sized absolute reference. This is why strings in __swift5_typeref (Technique:Swift metadata sections) cannot be demangled as plain text; resolve the reference first. The spec warns not to resolve them from untrusted input.
Demangling
The Swift toolchain ships swift demangle (reads names as arguments or stdin) and libswiftDemangle; LLVM and binutils c++filt do not decode $s. Unverified: the exact swift demangle options. Many disassembler plugins demangle automatically.
Parent: Technique:Swift metadata sections. Related: Platform:Itanium C++ ABI/Name mangling, Tool:Rustc/Symbol mangling.