Skip to content

Commit 47f151f

Browse files
authored
Add validation rules for Linking.md (#259)
1 parent 3822c2b commit 47f151f

File tree

1 file changed

+69
-4
lines changed

1 file changed

+69
-4
lines changed

Linking.md

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ The "reloc." custom sections must come after the
6363
["linking"](#linking-metadata-section) custom section in order to validate
6464
relocation indices.
6565

66-
Any LEB128-encoded values should be maximally padded so that they can be
67-
rewritten without affecting the position of any other bytes. For instance, the
68-
function index 3 should be encoded as `0x83 0x80 0x80 0x80 0x00`.
69-
7066
Relocations contain the following fields:
7167

7268
| Field | Type | Description |
@@ -181,6 +177,73 @@ relocations applied to the CODE section, a relocation cannot straddle two
181177
functions, and for the DATA section relocations must lie within a data element's
182178
body.
183179

180+
### Object file validation rules
181+
182+
For a module to be considered a valid object file, additional constraints are
183+
imposed on the data in custom sections related to linking, to ensure that the
184+
linking process will yield a valid module.
185+
186+
Tools that process object files are only required to produce output if source
187+
object files they process are valid object files.
188+
189+
All LEB128-encoded values that are to be relocated must be maximally padded so
190+
that they can be rewritten without affecting the position of any other bytes.
191+
For instance, the function index 3 must be encoded as `0x83 0x80 0x80 0x80 0x00`.
192+
193+
The `offset` part of a `memarg` where `memidx` represents a 32-bit memory may
194+
be treated as either [varuint32], or [varuint64].
195+
196+
If relocation's `index` represents a symbol table entry, constraints are placed
197+
on the relocation based on the symbol type it references:
198+
199+
| Symbol type | Allowed relocation types |
200+
|-------------------|---------------------------|
201+
| `SYMTAB_FUNCTION` | `R_WASM_FUNCTION_IDX_*`, `R_WASM_TABLE_IDX_*`, `R_WASM_FUNCTION_OFFSET_*` |
202+
| `SYMTAB_DATA` | `R_WASM_MEMORY_ADDR_*` |
203+
| `SYMTAB_GLOBAL` | `R_WASM_GLOBAL_INDEX_*` |
204+
| `SYMTAB_SECTION` | `R_WASM_SECTION_OFFSET_*` |
205+
| `SYMTAB_EVENT` | `R_WASM_EVENT_INDEX_*` |
206+
| `SYMTAB_TABLE` | `R_WASM_TABLE_NUMBER_*` |
207+
208+
Constraints are placed on relocations based on the data encoding of the value
209+
to be relocated:
210+
211+
| Data encoding | Allowed relocation types |
212+
|---------------|--------------------------|
213+
| [uint32] | `R_WASM_*_I32` |
214+
| [uint64] | `R_WASM_*_I64` |
215+
| [varint32] | `R_WASM_*_SLEB` |
216+
| [varint64] | `R_WASM_*_SLEB64` |
217+
| [varuint32] | `R_WASM_*_LEB` |
218+
| [varuint64] | `R_WASM_*_LEB64` |
219+
220+
If a data encoding for the relocation cannot be determined (i.e. there isn't a
221+
known grammar construct at the relocation offset), the data encoding constraints
222+
aren't applied. For example, this is the case for unknown custom sections and
223+
data segments.
224+
225+
In the CODE section, only certain grammar constructs are allowed to be targeted
226+
by relocations:
227+
228+
- For the constant operand of `i*.const` instructions, only
229+
`R_WASM_TABLE_INDEX_*` and `R_WASM_MEMORY_ADDR_*` relocations are allowed.
230+
- For the `offset` part of a `memarg`, only `R_WASM_MEMORY_ADDR_*` relocations
231+
are allowed.
232+
- For `funcidx`, only `R_WASM_FUNCTION_INDEX_*` relocations are allowed.
233+
- For `globalidx`, only `R_WASM_GLOBAL_INDEX_*` relocations are allowed.
234+
- For `tagidx`, only `R_WASM_EVENT_INDEX_*` relocations are allowed.
235+
- For `tableidx`, only `R_WASM_TABLE_NUMBER_*` relocations are allowed.
236+
237+
For `R_WASM_*_OFFSET_I*` relocations, the following condidions must hold for
238+
the addend:
239+
240+
- If `index` references the CODE section, the addend must represent an offset
241+
of an instruction boundary.
242+
- If `index` references the DATA section, the addend must represent a valid
243+
offset into a data segment's data area.
244+
- If `index` references the custom section, the addend must represent a valid
245+
offset into that custom section's data area.
246+
184247
## Linking Metadata Section
185248

186249
A linking metadata section is a user-defined section with the name
@@ -322,6 +385,8 @@ For section symbols:
322385
| ------------ | -------------- | ------------------------------------------- |
323386
| section | `varuint32` | the index of the target section |
324387

388+
Section symbols may only reference the CODE section, the DATA section, or custom sections.
389+
325390
The current set of valid flags for symbols are:
326391

327392
- `1 / WASM_SYM_BINDING_WEAK` - Indicating that this is a weak symbol. When

0 commit comments

Comments
 (0)