# How to statically parse AssociatedTypeDescriptors

**URL:** <https://forums.swift.org/t/how-to-statically-parse-associatedtypedescriptors/60080>\
**Category:** Using Swift\
**Tags:** reflection\
**Created:** [September 3, 2022, 8:23pm UTC](https://forums.swift.org/t/how-to-statically-parse-associatedtypedescriptors/60080 "2022-09-03T20:23:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![blacktop](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blacktop/32/25092_2.png) [@blacktop](https://forums.swift.org/u/blacktop)\
**Post date:** [September 3, 2022, 8:23pm UTC](https://forums.swift.org/t/how-to-statically-parse-associatedtypedescriptors/60080/1 "2022-09-03T20:23:35Z")

</div>

Hello! I'm trying to statically parse the Swift runtime data structures in the \_\_swift\_x sections in MachOs for use in reverse engineering tools and I am confused by `AssociatedTypeDescriptor`s. The first field is supposed to be a relative offset that points at the `ConformingTypeName` offset, but when I do the math I always am pointing to non-ascii data i.e. "\x01\xdf"

When I build a little test binary w/ DWARF info it's pointing to bin data, but there is a symbol in the symbol table for that address

![Screenshot 2022-09-03 at 2.15.44 PM](https://global.discourse-cdn.com/swift/original/3X/7/8/7841cd057e81c4f93357680f90868ab0a036cb05.png)

The NEXT rel offset in the data structure is the `ProtocolTypeName` offset, which points to the string "$s4test5StackP" and w/ DWARF also has a symbol of "\_symbolic $s4test5StackP"

So my primary question is how am I supposed to parse the `ConformingTypeName` as I've never seen it point to a string and if I have a stripped binary I won't have any symbols pointing to the offset of `ConformingTypeName` either?

Moving on to the `AssociatedTypeRecord` for the `Name` I get "Element" and for the `SubstitutedTypeName` I just get the letter "x". I was able to figure out that that's a mangled name missing the prefix "\_$s" and when adding that I get:

```swift
❯ xcrun swift-demangle '_$sx'
_$sx ---> A

```

Is there a reason you strip the prefix of the string for this one, but not others?

Many thanks!

---

<div class="post-metadata">

**Author:** ![blacktop](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blacktop/32/25092_2.png) [@blacktop](https://forums.swift.org/u/blacktop)\
**Post date:** [September 3, 2022, 8:27pm UTC](https://forums.swift.org/t/how-to-statically-parse-associatedtypedescriptors/60080/2 "2022-09-03T20:27:06Z")

</div>

Here is the test Swift binary's code I am testing on:

```swift
protocol Stack {
    associatedtype Element // 'Element' is a placeholder type, that will be specified by protocol conforming types (like struct, class, or enumeration).
    var items: [Element] {get set}
    mutating func push(_ item: Element)
    mutating func pop() -> Element
}

struct Inventory<T>: Stack{
    typealias Element = T // Specifying 'String' in place of 'T' type for the Inventory data model.

    // Replacing 'String' in place of 'T' everywhere.
    var items: [T] = []
    mutating func push(_ item: T) {
        items.append(item)
    }
    mutating func pop() -> T {
        return items.removeLast()
    }
}

let computerInventory: Inventory<String> = Inventory.init(items: ["Apple M1"])
print(computerInventory.items) // Prints: ["Apple M1"]

```

---

<div class="post-metadata">

**Author:** ![blacktop](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blacktop/32/25092_2.png) [@blacktop](https://forums.swift.org/u/blacktop)\
**Post date:** [September 3, 2022, 9:54pm UTC](https://forums.swift.org/t/how-to-statically-parse-associatedtypedescriptors/60080/3 "2022-09-03T21:54:34Z")

</div>

Looking at the function `makeSymbolicMangledNameStringRef` it looks like "\x01" = "\x1F" are 'symbolic references'? So I think it is acting like it's supposed to

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [September 3, 2022, 9:56pm UTC](https://forums.swift.org/t/how-to-statically-parse-associatedtypedescriptors/60080/4 "2022-09-03T21:56:41Z")

</div>

> [@blacktop](#):
>
> The first field is supposed to be a relative offset that points at the `ConformingTypeName` offset, but when I do the math I always am pointing to non-ascii data i.e. "\x01\xdf"

Take a look at [https://github.com/apple/swift/blob/main/docs/ABI/Mangling.rst#symbolic-references](https://github.com/apple/swift/blob/main/docs/ABI/Mangling.rst#symbolic-references). This is a symbolic reference pointing at the nominal type descriptor of `test.Inventory<T>`

> [@blacktop](#):
>
> Moving on to the `AssociatedTypeRecord` for the `Name` I get "Element" and for the `SubstitutedTypeName` I just get the letter "x".

"x" in this case refers to the first generic argument of a type. When you use Swift's runtime demangler with a combination of this mangled string and a fully realized generic environment, it'll return the actual type of the first generic arg. E.g. in this case `test.Inventory<Int>`, "x" refers to `Int`.

From the same mangling document:

> type ::= 'x' // generic param, depth=0, idx=0  
> [https://github.com/apple/swift/blob/main/docs/ABI/Mangling.rst#types](https://github.com/apple/swift/blob/main/docs/ABI/Mangling.rst#types)

---

<div class="post-metadata">

**Author:** ![blacktop](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blacktop/32/25092_2.png) [@blacktop](https://forums.swift.org/u/blacktop)\
**Post date:** [September 4, 2022, 4:11am UTC](https://forums.swift.org/t/how-to-statically-parse-associatedtypedescriptors/60080/5 "2022-09-04T04:11:01Z")

</div>

Many thanks!!
