Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Wasmリンカのつくりかた

Yuta Saito
December 09, 2020

 Wasmリンカのつくりかた

WebAssembly night #10
イベント: https://emsn.connpass.com/event/192221/
自作したWasmリンカ: https://github.com/kateinoigakukun/chibi-link

Yuta Saito

December 09, 2020
Tweet

More Decks by Yuta Saito

Other Decks in Technology

Transcript

  1. WasmϦϯΧͷͭ͘Γ͔ͨ
    @kateinoigakukun
    WebAssembly Night #10
    1

    View full-size slide

  2. ࣗݾ঺հ
    @kateinoigakukun
    • Πϯλʔϯ at ϝϧϖΠ ΤΩεύʔτνʔϜ
    • SwiftͷWebAssemblyରԠͳͲ
    • ڵຯ: ݴޠॲཧܥ, Swift
    2

    View full-size slide

  3. ϦϯΧΛ࡞ΔϞνϕʔγϣϯ
    3

    View full-size slide

  4. SwiftWasmϓϩδΣΫτ
    https://swiftwasm.org
    Swift΍LLVMΛϑΥʔΫͯ͠SwiftͷWebAssemblyରԠΛਐΊΔ
    ϓϩδΣΫτɻ
    πʔϧνΣʔϯͷ഑෍΍JavaScriptͱͷ࿈ܞϥΠϒϥϦͷ։ൃͳ
    Ͳ΋΍ͬͯΔɻ
    4

    View full-size slide

  5. ϒϥ΢βͰࢼͤ·͢ https://pad.swiftwasm.org/
    5

    View full-size slide

  6. ҎલͷSwiftWasm PadͷΞʔΩςΫνϟ
    6

    View full-size slide

  7. ໰୊఺
    • Swiftͷඪ४ϥΠϒϥϦ͸α
    Πζ͕େ͖͍
    • αʔόͰඪ४ϥΠϒϥϦ͕
    ϦϯΫ͞Ε͍ͯΔͷͰμ΢
    ϯϩʔυαΠζ͕େ͖͘ͳ
    Δ
    7

    View full-size slide

  8. ݱࡏͷSwiftWasm PadͷΞʔΩςΫνϟ
    8

    View full-size slide

  9. WasmͷಈతϦϯΫ
    ٞ࿦͸WebAssembly/module-linking ͰਐΜͰ͍Δ͕ɺStable
    ͳAPI͸·ͩແ͍ɻ
    EmscriptenͰ͸ಈతϦϯΫͰ͖ΔΒ͍͕͠Swift͸LLVMͷWasm
    όοΫΤϯυΛ࢖͍ͬͯΔͷͰͦͷ··࢖͑ͳ͍ɻ
    9

    View full-size slide

  10. ϒϥ΢βͰಈ͘WasmϦϯΧΛ࡞Ζ͏ʂ
    10

    View full-size slide

  11. WebAssemblyͷϦϯΧࣄ৘
    • ίϯύΠϥͱϦϯΧͷؒʹඪ४ͷ࢓༷͕ແ͍1
    • ΦϒδΣΫτϑΝΠϧͷϑΥʔϚοτͳͲ
    • πʔϧνΣʔϯ͝ͱʹίϯϕϯγϣϯ͕͋Δ
    • lld (wasm-ld)͕LLVMͷίϯϕϯγϣϯΛຬͨ͢།Ұͷ࣮૷
    ʢଞʹ΋͋Ε͹ڭ͑ͯԼ͍͞ʣ
    1 ҰԠυΩϡϝϯτ͸͋Δ https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
    11

    View full-size slide

  12. ϦϯΧͷ࢓ࣄ
    • ಉ͡ηΫγϣϯΛϚʔδ
    • γϯϘϧͷղܾ
    • ࠶഑ஔ
    12

    View full-size slide

  13. ಉ͡ηΫγϣϯΛϚʔδ
    • ࣈͷ͝ͱ͘Ϛʔδ͢Δ
    • ϦϯΧ͕ੜ੒͢Δؔ਺΍
    σʔλྖҬ΋͓ࠞͥͯ͘
    • ελοΫྖҬ
    (__stack_pointer)
    • ίϯετϥΫλ
    (__wasm_call_ctors)
    13

    View full-size slide

  14. γϯϘϧͷղܾ
    // lib.c
    int add(int a, int b) {
    return a + b;
    }
    // main.c
    int add(int a, int b);
    int main(void) {
    return add(1, -1);
    }
    14

    View full-size slide

  15. ࠶഑ஔ
    ࠶഑ஔϨίʔυͱγϯϘϧςʔϒϧΛݩʹϦϯΫޙͷγϯϘϧ
    ͷΞυϨεΛॻ͖ࠐΉ
    // main.c
    int add(int a, int b);
    int main(void) {
    return add(1, -1); // addؔ਺ͷϦϯΫޙͷΞυϨεΛ஌Γ͍ͨ
    }
    15

    View full-size slide

  16. $ wasm-objdump --details --section=reloc.CODE main.o
    main.o: file format wasm 0x1
    Section Details:
    Custom:
    - name: "reloc.CODE"
    - relocations for section: 3 (Code) [5]
    - R_WASM_GLOBAL_INDEX_LEB offset=0x000006(file=0x00008f) symbol=1
    - R_WASM_GLOBAL_INDEX_LEB offset=0x00001b(file=0x0000a4) symbol=1
    - R_WASM_FUNCTION_INDEX_LEB offset=0x000038(file=0x0000c1) symbol=2
    - R_WASM_GLOBAL_INDEX_LEB offset=0x00004d(file=0x0000d6) symbol=1
    - R_WASM_FUNCTION_INDEX_LEB offset=0x00005b(file=0x0000e4) symbol=0 <__original_main>
    16

    View full-size slide

  17. $ wasm-objdump --details --section=reloc.CODE main.o
    main.o: file format wasm 0x1
    Section Details:
    Custom:
    - name: "reloc.CODE"
    - relocations for section: 3 (Code) [5]
    - R_WASM_GLOBAL_INDEX_LEB offset=0x000006(file=0x00008f) symbol=1
    - R_WASM_GLOBAL_INDEX_LEB offset=0x00001b(file=0x0000a4) symbol=1
    - R_WASM_FUNCTION_INDEX_LEB offset=0x000038(file=0x0000c1) symbol=2
    - R_WASM_GLOBAL_INDEX_LEB offset=0x00004d(file=0x0000d6) symbol=1
    - R_WASM_FUNCTION_INDEX_LEB offset=0x00005b(file=0x0000e4) symbol=0 <__original_main>
    16

    View full-size slide

  18. kateinoigakukun/chibi-link
    • ࠓճ࡞ͬͨSwift੡WasmϦϯΧ
    • SwiftΛϦϯΫ͢Δͷʹ࠷খݶͳػೳ
    • ϦϯΧຊମ͸3000ߦ͘Β͍Ͱখ͞Ί
    $ wasmtime run --mapdir=/var/tmp::$(pwd) chibi-link.wasm -- \
    /var/tmp/lib.o /var/tmp/main.o -o /var/tmp/output.wasm
    17

    View full-size slide

  19. ͕͜͜ਏ͍ΑϦϯΧ։ൃ
    • ίϯύΠϥͱϦϯΧͷࡉʑͨ͠໿ଋ͕໌จԽ͞Ε͍ͯͳ͍
    • σʔληάϝϯτͷ։࢝ͱऴΘΓͷΞυϨεΛ__start_/
    __stop_{{segment_name}}ͷγϯϘϧͰެ։͠ͳ͍ͱ͍͚ͳ͍
    • ؔ਺ςʔϒϧͷ0൪໨͸τϥοϓͷͨΊʹ֬อ͢Δ
    • etc...
    • σόοά͕େม
    • ϦϯΫ݁Ռ͕յΕ͍ͯΔͱɺϝϞϦΛഁյ͠ͳ͕Βಈ͍ͪΌ͏
    18

    View full-size slide

  20. ·ͱΊ
    • ΫϥΠΞϯταΠυϦϯΧ͕https://pad.swiftwasm.org/ Ͱ࢖
    ΘΕͯΔ
    • ϦϯΧͷ΍Δ͜ͱ͸ҙ֎ͱগͳ͍
    • Wasm͸࢓༷͕·ͩখ͍͞ͷͰϦϯΧΛॻ͘ͷʹ͓͢͢Ίʂ
    19

    View full-size slide