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

Wasmリンカのつくりかた

7a4968fbcd56e81f95a4f3c186141b52?s=47 Yuta Saito
December 09, 2020

 Wasmリンカのつくりかた

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

7a4968fbcd56e81f95a4f3c186141b52?s=128

Yuta Saito

December 09, 2020
Tweet

Transcript

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

  2. ࣗݾ঺հ @kateinoigakukun • Πϯλʔϯ at ϝϧϖΠ ΤΩεύʔτνʔϜ • SwiftͷWebAssemblyରԠͳͲ •

    ڵຯ: ݴޠॲཧܥ, Swift 2
  3. ϦϯΧΛ࡞ΔϞνϕʔγϣϯ 3

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

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

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

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

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

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

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

  11. WebAssemblyͷϦϯΧࣄ৘ • ίϯύΠϥͱϦϯΧͷؒʹඪ४ͷ࢓༷͕ແ͍1 • ΦϒδΣΫτϑΝΠϧͷϑΥʔϚοτͳͲ • πʔϧνΣʔϯ͝ͱʹίϯϕϯγϣϯ͕͋Δ • lld (wasm-ld)͕LLVMͷίϯϕϯγϣϯΛຬͨ͢།Ұͷ࣮૷

    ʢଞʹ΋͋Ε͹ڭ͑ͯԼ͍͞ʣ 1 ҰԠυΩϡϝϯτ͸͋Δ https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md 11
  12. ϦϯΧͷ࢓ࣄ • ಉ͡ηΫγϣϯΛϚʔδ • γϯϘϧͷղܾ • ࠶഑ஔ 12

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

    ίϯετϥΫλ (__wasm_call_ctors) 13
  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
  15. ࠶഑ஔ ࠶഑ஔϨίʔυͱγϯϘϧςʔϒϧΛݩʹϦϯΫޙͷγϯϘϧ ͷΞυϨεΛॻ͖ࠐΉ // main.c int add(int a, int b);

    int main(void) { return add(1, -1); // addؔ਺ͷϦϯΫޙͷΞυϨεΛ஌Γ͍ͨ } 15
  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 <env.__stack_pointer> - R_WASM_GLOBAL_INDEX_LEB offset=0x00001b(file=0x0000a4) symbol=1 <env.__stack_pointer> - R_WASM_FUNCTION_INDEX_LEB offset=0x000038(file=0x0000c1) symbol=2 <env.add> - R_WASM_GLOBAL_INDEX_LEB offset=0x00004d(file=0x0000d6) symbol=1 <env.__stack_pointer> - R_WASM_FUNCTION_INDEX_LEB offset=0x00005b(file=0x0000e4) symbol=0 <__original_main> 16
  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 <env.__stack_pointer> - R_WASM_GLOBAL_INDEX_LEB offset=0x00001b(file=0x0000a4) symbol=1 <env.__stack_pointer> - R_WASM_FUNCTION_INDEX_LEB offset=0x000038(file=0x0000c1) symbol=2 <env.add> - R_WASM_GLOBAL_INDEX_LEB offset=0x00004d(file=0x0000d6) symbol=1 <env.__stack_pointer> - R_WASM_FUNCTION_INDEX_LEB offset=0x00005b(file=0x0000e4) symbol=0 <__original_main> 16
  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
  19. ͕͜͜ਏ͍ΑϦϯΧ։ൃ • ίϯύΠϥͱϦϯΧͷࡉʑͨ͠໿ଋ͕໌จԽ͞Ε͍ͯͳ͍ • σʔληάϝϯτͷ։࢝ͱऴΘΓͷΞυϨεΛ__start_/ __stop_{{segment_name}}ͷγϯϘϧͰެ։͠ͳ͍ͱ͍͚ͳ͍ • ؔ਺ςʔϒϧͷ0൪໨͸τϥοϓͷͨΊʹ֬อ͢Δ • etc...

    • σόοά͕େม • ϦϯΫ݁Ռ͕յΕ͍ͯΔͱɺϝϞϦΛഁյ͠ͳ͕Βಈ͍ͪΌ͏ 18
  20. ·ͱΊ • ΫϥΠΞϯταΠυϦϯΧ͕https://pad.swiftwasm.org/ Ͱ࢖ ΘΕͯΔ • ϦϯΧͷ΍Δ͜ͱ͸ҙ֎ͱগͳ͍ • Wasm͸࢓༷͕·ͩখ͍͞ͷͰϦϯΧΛॻ͘ͷʹ͓͢͢Ίʂ 19