swiftcのmanglingとsubstitution

 swiftcのmanglingとsubstitution

3781f49ea2c76d6ecf0c6cda46096d49?s=128

omochimetaru

June 22, 2018
Tweet

Transcript

  1. swiftcͷmanglingͱ Substitution Θ͍Θ͍swiftc #3, 2018/06/22 omochimetaru @ 1

  2. mangling ϓϩάϥϜΛίϯύΠϧͯ͠LLVMόΠφϦʹͳΔͱ͖ɺ ఆ਺΍ؔ਺ͳͲͷ໊લ͕ม׵͞ΕΔɻ ͜ΕΛγϯϘϧ໊ͱݺͿɻ // c.swift func hogehoge() -> Void

    { ... } ; c.ll define hidden swiftcc void @"$S1c8hogehogeyyF"() #0 { ... } 2
  3. γϯϘϧ໊ γϯϘϧ໊͸ίϯύΠϧ͞ΕͨόΠφϦͷதͰͷ໊લɻ ΋ͱ΋ͱͷݴޠͷ໊લ͚ͩͰ͸ͳ͘ɺ ༷ʑͳ৘ใ΋ಉ࣌ʹຒΊࠐ·ΕΔɻ ͜ͷม׵Λmanglingͱ͍͏ɻ 3

  4. manglingͱSwift γϯϘϧ໊ʹ͸SwiftͷγϯϘϧͰ͋Δ͜ͱΛࣔ͢ϔομ͕෇ ͘ɻ $S1c8hogehogeyyF $S: SwiftͷγϯϘϧ 4

  5. manglingͱϞδϡʔϧ໊ γϯϘϧ໊ʹ͸Ϟδϡʔϧؚ໊͕·ΕΔɻ ͜ΕʹΑΓɺผϞδϡʔϧͷಉ໊ΤϯςΟςΟͱিಥ͠ͳ͍ɻ $S1c8hogehogeyyF 1c: "c" ͷ 8hogehoge: "hogehoge" ͜͜Ͱ͸Ϟδϡʔϧ໊͸

    c ਺ࣈ͸Ωʔϫʔυͷ௕͞ϔομ 5
  6. manglingͱΤϯςΟςΟͷछྨ γϯϘϧ໊ʹ͸ΤϯςΟςΟͷछྨ͕͍͍ͭͯΔ $S1c8hogehogeyyF F: ؔ਺ ΤϯςΟςΟͷྫ WV: value witness table

    fC: allocating init fc: non-allocating init s: property setter g: property getter 6
  7. manglingͱܕ৘ใ ؔ਺ͷγϯϘϧ໊ʹ͸Ҿ਺΍ฦΓ஋ͷܕ͕͍͍ͭͯΔ $S1c8hogehogeyyF y: ฦΓ஋͸Void y: Ҿ਺͸ແ͠ ܕͷྫ Si: Swift.Int

    Sf: Swift.Float SS: Swift.String 7
  8. manglingͱܕ৘ใ ؔ਺ͷγϯϘϧ໊ʹ͸ϥϕϧ໊΋͍͍ͭͯΔ func hogehoge(aaa: Int, bbb: Float) -> String $S1c8hogehoge3aaa3bbbSSSi_SftF

    3aaa: "aaa" 3bbb: "bbb" SS: ฦΓ஋͸String Si: ୈ1Ҿ਺͸Int _: Ϧετ͕ଓ͘ Sf: ୈ2Ҿ਺͸Float t: Ϧετ͸ऴΘΓ 8
  9. manglingͱϢʔβఆٛܕ struct Stone { func hogehoge() {} } $S1c5StoneV8hogehogeyyF 1c:

    "c" 5Stone: "Stone" V: struct 8hogehoge: "hogehoge" 9
  10. Swift demangle γϯϘϧ໊͔Βٯม׵Λͯ͠ɺຒΊࠐ·Εͨ৘ใΛಡΊΔɻ $ swift demangle '$S1c5StoneV8hogehogeyyF' $S1c5StoneV8hogehogeyyF ---> c.Stone.hogehoge()

    -> () 10
  11. -expandΦϓγϣϯ͕͓͢͢Ί $ swift demangle -expand '$S1c5StoneV8hogehogeyyF' Demangling for $S1c5StoneV8hogehogeyyF kind=Global

    kind=Function kind=Structure kind=Module, text="c" kind=Identifier, text="Stone" kind=Identifier, text="hogehoge" kind=Type kind=FunctionType kind=ArgumentTuple, index=0 kind=Type kind=Tuple kind=ReturnType kind=Type kind=Tuple $S1c5StoneV8hogehogeyyF ---> c.Stone.hogehoge() -> () 11
  12. Substitution 12

  13. manglingͱSubstitution struct Water {} struct Stone { func hogehoge(aaa: Stone,

    bbb: Water, ccc: Stone) -> Water { fatalError() } } $S1c5StoneV8hogehoge3aaa3bbb3cccAA5WaterVAC_AiCtF 13
  14. ʁ expect $S1c5StoneV8hogehoge3aaa3bbb3ccc1c5WaterV1c5StoneV_1c5WaterV1c5StoneVtF 1c5WaterV: struct c.Water 1c5StoneV: struct c.Stone 1c5WaterV:

    struct c.Water 1c5StoneV: struct c.Stone actual $S1c5StoneV8hogehoge3aaa3bbb3cccAA5WaterVAC_AiCtF AA: ?? 5WaterV: struct Water AC: ?? AiC: ?? 14
  15. Substitution ManglingͷաఔͰग़ݱͨ͠จࣈྻ΍ܕ͕഑ྻʹอଘ͞ΕΔɻ 2౓໨Ҏ߱ͷग़ݱ࣌ʹ͸ͦͷ഑ྻͷΠϯσοΫεͱͯ͠Τϯίʔ υ͞Εɺ σίʔυ࣌ʹ͸ͦͷ഑ྻͷཁૉʹஔ׵͞ΕΔɻ ͜ͷػೳΛSubstitution(ஔ׵)ͱ͍͏ɻ 15

  16. Substitutionͷྫ AA A: ஔ׵։࢝ A: [0]൪ʹஔ׵, ஔ׵ऴྃ(େจࣈ) AiC i: [8]൪ʹஔ׵

    C: [2]൪ʹஔ׵, ஔ׵ऴྃ(େจࣈ) A2C 2: ࣍ͷஔ׵Λ2ճ܁Γฦ͢ C: [2]൪ʹஔ׵, ஔ׵ऴྃ(େจࣈ) 16
  17. Substitutionͷྫ $S1c5StoneV8hogehoge3aaa3bbb3cccAA5WaterVAC_AiCtF 1c: [0] = "c" 5Stone: [1] = "Stone"

    V: [2] = struct c.Stone 8hogehoge: [3] = "hogehoge" 3aaa: [4] = "aaa" 3bbb: [5] = "bbb" 3ccc: [6] = "ccc" AA: "c" from [0] 5Water: [7] = "Water" V: [8] = struct c.Water AC: struct c.Stone from [2] Ai: struct c.Water from [8] C: struct c.Stone from [2] 17
  18. ࢿྉ Mangling https://github.com/apple/swift/blob/master/docs/ABI/ Mangling.rst 18

  19. ·ͱΊ • mangling͸໘ന͍ • swift demangle ศར • substitution͸͍͢͝ 19

  20. ͓ΘΓ 20