Swift Metadata Sections

Introduction

This document aims to explain all the `swift*_*` sections.

https://github.com/apple/swift/blob/main/docs/ABI/TypeMetadata.rst

Note that section names are like this:

Contents

Sections

Ideally we should focus on the `__swift5` ones, older versions may have different and deprecated structures, better focus on the future

Kind

Class metadata has of kind of 0 unless the class is required to interoperate with Objective-C. If the class is required to interoperate with Objective-C, the kind field is instead an isa pointer to an Objective-C metaclass. Such a pointer can be distinguished from an enumerated metadata kind because it is guaranteed to have a value larger than 2047. Note that this is a more basic sense of interoperation than is meant by the @objc attribute: it is what is required to support Objective-C message sends and retain/release. All classes are required to interoperate with Objective-C on this level when building for an Apple platform.

enum TypeKind {
	Struct = 1
	Enum = 2
	Optional = 3
	OpaqueMetadata = 8 // This is used for compiler Builtin primitives that have no additional runtime information.
	Tuple = 9
	Function = 10
	Protocol = 12 // This is used for protocol types, for protocol compositions, and for the Any type.
	Metatype = 13
	ObjCClassWrapper = 14
	Existentialmetatype = 15
}

Sections

fieldmd

type FieldRecord struct {
    Flags           uint32
    MangledTypeName int32
    FieldName       int32
}

type FieldDescriptor struct {
    MangledTypeName int32
    Superclass      int32
    Kind            uint16
    FieldRecordSize uint16
    NumFields       uint32
    FieldRecords    []FieldRecord
}
0x100001f14  0xffffffea 0x00000000 0x000c0000 0x00000001  ................
0x100001f24  0x00000002 0xffffffdc 0xffffffe2 0xffffffd8  ................
0x100001f34  0x00000000 0x000c0001 0x00000001 0x00000000  ................
0x100001f44  0xffffffc0 0xffffffc8                        ........

assocty

builtin

capture

typeref

Contains a list of mangled type names, referenced from other sections.

reflstr

Contains an array of C strings with the fieldnames used from other sections.

replace

Contains the method swizzling details

type Replacement struct {
    ReplacedFunctionKey int32
    NewFunction         int32
    Replacement         int32
    Flags               uint32
}

type ReplacementScope struct {
    Flags uint32
    NumReplacements uint32

}

type AutomaticReplacements struct {
    Flags            uint32
    NumReplacements  uint32 // hard coded to 1
    Replacements     int32
}

types

This section is an array of pointers to nominal descriptors:

The common fields are:

    Flags                       uint32
    Parent                      int32
    Name                        int32
    AccessFunction              int32
    FieldDescriptor             int32
type EnumDescriptor struct {
    Flags                               uint32
    Parent                              int32
    Name                                int32
    AccessFunction                      int32
    FieldDescriptor                     int32
    NumPayloadCasesAndPayloadSizeOffset uint32
    NumEmptyCases                       uint32
}

type StructDescriptor struct {
    Flags                   uint32
    Parent                  int32
    Name                    int32
    AccessFunction          int32
    FieldDescriptor         int32
    NumFields               uint32
    FieldOffsetVectorOffset uint32
}

type ClassDescriptor struct {
    Flags                       uint32
    Parent                      int32
    Name                        int32
    AccessFunction              int32
    FieldDescriptor             int32
    SuperclassType              int32
    MetadataNegativeSizeInWords uint32
    MetadataPositiveSizeInWords uint32
    NumImmediateMembers         uint32
    NumFields                   uint32
}

replac2

Like `replace` but for opaque methods. and associated types

https://github.com/apple/swift/pull/24781

type Replacement struct {
    Original    int32
    Replacement int32
}

type AutomaticReplacementsSome struct {
    Flags uint32
    NumReplacements uint32
    Replacements    []Replacement
}