summaryrefslogtreecommitdiff
path: root/src/binary-reader-linker.c
blob: 89e308111e2132f38ab7cb8ae3dd7707f426c160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright 2017 WebAssembly Community Group participants
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "binary-reader-linker.h"

#include "binary-reader.h"
#include "wasm-link.h"

#define RELOC_SIZE 5

typedef struct Context {
  WasmAllocator* allocator;
  WasmLinkerInputBinary* binary;

  WasmSection* reloc_section;

  WasmStringSlice import_name;
  WasmSection* current_section;
} Context;

static WasmResult on_reloc_count(uint32_t count,
                                 WasmBinarySection section_code,
                                 WasmStringSlice section_name,
                                 void* user_data) {
  Context* ctx = user_data;
  WasmLinkerInputBinary* binary = ctx->binary;
  if (section_code == WASM_BINARY_SECTION_CUSTOM) {
    WASM_FATAL("relocation for custom sections not yet supported\n");
  }

  uint32_t i;
  for (i = 0; i < binary->sections.size; i++) {
    WasmSection* sec = &binary->sections.data[i];
    if (sec->section_code != section_code)
      continue;
    ctx->reloc_section = sec;
    return WASM_OK;
  }

  WASM_FATAL("section not found: %d\n", section_code);
  return WASM_ERROR;
}

static WasmResult on_reloc(WasmRelocType type,
                           uint32_t offset,
                           void* user_data) {
  Context* ctx = user_data;

  if (offset + RELOC_SIZE > ctx->reloc_section->size) {
    WASM_FATAL("invalid relocation offset: %#x\n", offset);
  }

  WasmReloc* reloc =
      wasm_append_reloc(ctx->allocator, &ctx->reloc_section->relocations);
  reloc->type = type;
  reloc->offset = offset;

  return WASM_OK;
}

static WasmResult on_import(uint32_t index,
                            WasmStringSlice module_name,
                            WasmStringSlice field_name,
                            void* user_data) {
  Context* ctx = user_data;
  if (!wasm_string_slice_eq_cstr(&module_name, WASM_LINK_MODULE_NAME)) {
    WASM_FATAL("unsupported import module: " PRIstringslice,
               WASM_PRINTF_STRING_SLICE_ARG(module_name));
  }
  ctx->import_name = field_name;
  return WASM_OK;
}

static WasmResult on_import_func(uint32_t import_index,
                                 uint32_t global_index,
                                 uint32_t sig_index,
                                 void* user_data) {
  Context* ctx = user_data;
  WasmFunctionImport* import = wasm_append_function_import(
      ctx->allocator, &ctx->binary->function_imports);
  import->name = ctx->import_name;
  import->sig_index = sig_index;
  import->active = WASM_TRUE;
  ctx->binary->active_function_imports++;
  return WASM_OK;
}

static WasmResult on_import_global(uint32_t import_index,
                                   uint32_t global_index,
                                   WasmType type,
                                   WasmBool mutable,
                                   void* user_data) {
  Context* ctx = user_data;
  WasmGlobalImport* import =
      wasm_append_global_import(ctx->allocator, &ctx->binary->global_imports);
  import->name = ctx->import_name;
  import->type = type;
  import->mutable = mutable;
  ctx->binary->active_global_imports++;
  return WASM_OK;
}

static WasmResult begin_section(WasmBinaryReaderContext* ctx,
                                WasmBinarySection section_code,
                                uint32_t size) {
  Context* context = ctx->user_data;
  WasmLinkerInputBinary* binary = context->binary;
  WasmSection* sec = wasm_append_section(context->allocator, &binary->sections);
  context->current_section = sec;
  sec->section_code = section_code;
  sec->size = size;
  sec->offset = ctx->offset;
  sec->binary = binary;

  if (sec->section_code != WASM_BINARY_SECTION_CUSTOM &&
      sec->section_code != WASM_BINARY_SECTION_START) {
    size_t bytes_read = wasm_read_u32_leb128(
        &binary->data[sec->offset], &binary->data[binary->size], &sec->count);
    if (bytes_read == 0)
      WASM_FATAL("error reading section element count\n");
    sec->payload_offset = sec->offset + bytes_read;
    sec->payload_size = sec->size - bytes_read;
  }
  return WASM_OK;
}

static WasmResult begin_custom_section(WasmBinaryReaderContext* ctx,
                                       uint32_t size,
                                       WasmStringSlice section_name) {
  Context* context = ctx->user_data;
  WasmLinkerInputBinary* binary = context->binary;
  WasmSection* sec = context->current_section;
  sec->data_custom.name = section_name;

  /* Modify section size and offset to not include the name itself. */
  size_t delta = ctx->offset - sec->offset;
  sec->offset = sec->offset + delta;
  sec->size = sec->size - delta;
  sec->payload_offset = sec->offset;
  sec->payload_size = sec->size;

  /* Special handling for certain CUSTOM sections */
  if (wasm_string_slice_eq_cstr(&section_name, "name")) {
    size_t bytes_read = wasm_read_u32_leb128(
        &binary->data[sec->offset], &binary->data[binary->size], &sec->count);
    sec->payload_offset += bytes_read;
    sec->payload_size -= bytes_read;

    /* We don't currently support merging name sections unless they contain
     * a name for every function. */
    size_t i;
    uint32_t total_funcs = binary->function_imports.size;
    for (i = 0; i < binary->sections.size; i++) {
      if (binary->sections.data[i].section_code ==
          WASM_BINARY_SECTION_FUNCTION) {
        total_funcs += binary->sections.data[i].count;
        break;
      }
    }
    if (total_funcs != sec->count) {
      WASM_FATAL("name section count (%d) does not match function count (%d)\n",
                 sec->count, total_funcs);
    }
  }

  return WASM_OK;
}

static WasmResult on_table(uint32_t index,
                           WasmType elem_type,
                           const WasmLimits* elem_limits,
                           void* user_data) {
  if (elem_limits->has_max && (elem_limits->max != elem_limits->initial))
    WASM_FATAL("Tables with max != initial not supported by wasm-link\n");

  Context* ctx = user_data;
  ctx->binary->table_elem_count = elem_limits->initial;
  return WASM_OK;
}

static WasmResult on_elem_segment_function_index_count(
    WasmBinaryReaderContext* ctx,
    uint32_t index,
    uint32_t count) {
  Context* context = ctx->user_data;
  WasmSection* sec = context->current_section;

  /* Modify the payload to include only the actual function indexes */
  size_t delta = ctx->offset - sec->payload_offset;
  sec->payload_offset += delta;
  sec->payload_size -= delta;
  return WASM_OK;
}

static WasmResult on_memory(uint32_t index,
                            const WasmLimits* page_limits,
                            void* user_data) {
  Context* ctx = user_data;
  WasmSection* sec = ctx->current_section;
  sec->memory_limits = *page_limits;
  ctx->binary->memory_page_count = page_limits->initial;
  return WASM_OK;
}

static WasmResult begin_data_segment(uint32_t index,
                                     uint32_t memory_index,
                                     void* user_data) {
  Context* ctx = user_data;
  WasmSection* sec = ctx->current_section;
  WasmDataSegment* segment =
      wasm_append_data_segment(ctx->allocator, &sec->data_segments);
  segment->memory_index = memory_index;
  return WASM_OK;
}

static WasmResult on_init_expr_i32_const_expr(uint32_t index,
                                              uint32_t value,
                                              void* user_data) {
  Context* ctx = user_data;
  WasmSection* sec = ctx->current_section;
  if (sec->section_code != WASM_BINARY_SECTION_DATA)
    return WASM_OK;
  WasmDataSegment* segment =
      &sec->data_segments.data[sec->data_segments.size - 1];
  segment->offset = value;
  return WASM_OK;
}

static WasmResult on_data_segment_data(uint32_t index,
                                       const void* src_data,
                                       uint32_t size,
                                       void* user_data) {
  Context* ctx = user_data;
  WasmSection* sec = ctx->current_section;
  WasmDataSegment* segment =
      &sec->data_segments.data[sec->data_segments.size - 1];
  segment->data = src_data;
  segment->size = size;
  return WASM_OK;
}

static WasmResult on_export(uint32_t index,
                            WasmExternalKind kind,
                            uint32_t item_index,
                            WasmStringSlice name,
                            void* user_data) {
  Context* ctx = user_data;
  WasmExport* export =
      wasm_append_export(ctx->allocator, &ctx->binary->exports);
  export->name = name;
  export->kind = kind;
  export->index = item_index;
  return WASM_OK;
}

static WasmResult on_function_name(uint32_t index,
                                   WasmStringSlice name,
                                   void* user_data) {
  Context* ctx = user_data;
  wasm_append_string_slice_value(ctx->allocator, &ctx->binary->debug_names,
                                 &name);
  return WASM_OK;
}

static WasmBinaryReader s_binary_reader = {
    .begin_section = begin_section,
    .begin_custom_section = begin_custom_section,

    .on_reloc_count = on_reloc_count,
    .on_reloc = on_reloc,

    .on_import = on_import,
    .on_import_func = on_import_func,
    .on_import_global = on_import_global,

    .on_export = on_export,

    .on_table = on_table,

    .on_memory = on_memory,

    .begin_data_segment = begin_data_segment,
    .on_init_expr_i32_const_expr = on_init_expr_i32_const_expr,
    .on_data_segment_data = on_data_segment_data,

    .on_elem_segment_function_index_count =
        on_elem_segment_function_index_count,

    .on_function_name = on_function_name,
};

WasmResult wasm_read_binary_linker(struct WasmAllocator* allocator,
                                   WasmLinkerInputBinary* input_info) {
  Context context;
  WASM_ZERO_MEMORY(context);
  context.allocator = allocator;
  context.binary = input_info;

  WasmBinaryReader reader;
  WASM_ZERO_MEMORY(reader);
  reader = s_binary_reader;
  reader.user_data = &context;

  WasmReadBinaryOptions read_options = WASM_READ_BINARY_OPTIONS_DEFAULT;
  read_options.read_debug_names = WASM_TRUE;
  return wasm_read_binary(allocator, input_info->data, input_info->size,
                          &reader, 1, &read_options);
}