Commit Graph

277 Commits

Author SHA1 Message Date
Nikolay Nikolov
0be3fe9ab4 - removed TWasmObjData.AddFuncType, instead call directly [F]FuncTypes.AddOrGetFuncType 2023-12-31 15:52:27 +02:00
Nikolay Nikolov
eb046b07d0 * TWasmFuncTypeTable.AddFuncType renamed .AddOrGetFuncType 2023-12-31 15:49:26 +02:00
Nikolay Nikolov
7266981c6c + Extracted the func list maintenance functionality from the WebAssembly object
writer to a new TWasmFuncTypeTable class. This will allow for it to be reused
  in the WebAssembly exe writer as well.
2023-12-31 15:39:54 +02:00
Nikolay Nikolov
d6c3c74b46 + create relocation objects for R_WASM_FUNCTION_OFFSET_I32 relocations in the wasm object reader 2023-12-28 21:26:12 +02:00
Nikolay Nikolov
73895d3cbc + create relocation objects for R_WASM_TYPE_INDEX_LEB relocations in the wasm object reader 2023-12-28 21:23:08 +02:00
Nikolay Nikolov
ae93f8de91 + create relocation objects for R_WASM_MEMORY_ADDR_I32 relocations in the wasm object reader 2023-12-28 21:15:59 +02:00
Nikolay Nikolov
35f2fb1e8d * fix warning 2023-12-28 21:11:26 +02:00
Nikolay Nikolov
bd606c6b77 + create relocation objects for R_WASM_MEMORY_ADDR_SLEB relocations in the wasm object reader 2023-12-28 21:07:41 +02:00
Nikolay Nikolov
3e14d47b92 + create relocation objects for R_WASM_MEMORY_ADDR_LEB relocations in the wasm object reader 2023-12-28 21:04:13 +02:00
Nikolay Nikolov
118e093c97 + create relocation objects for R_WASM_TABLE_INDEX_I32 relocations in the wasm object reader 2023-12-28 20:59:32 +02:00
Nikolay Nikolov
c605da6ab3 + create relocation objects for R_WASM_TABLE_INDEX_SLEB relocations in the wasm object reader 2023-12-28 20:57:04 +02:00
Nikolay Nikolov
aa296af787 + create relocation objects for R_WASM_FUNCTION_INDEX_LEB relocations in the wasm object reader 2023-12-28 20:52:17 +02:00
Nikolay Nikolov
bc798695fe + find the code and data segment of each relocation 2023-12-28 20:07:24 +02:00
Nikolay Nikolov
268a148d3a + store the parsed relocation table in an array 2023-12-28 16:47:00 +02:00
Nikolay Nikolov
82c52ed127 * disallow (for now) relocations for sections other than the code and data sections 2023-12-28 16:37:58 +02:00
Nikolay Nikolov
649438efa6 + keep track of section indexes. Save the code and data section indexes. 2023-12-28 16:35:11 +02:00
Nikolay Nikolov
a1bdf11d99 + check for supported relocation types, report an error if an unsupported type is found 2023-12-28 16:09:03 +02:00
Nikolay Nikolov
881ca2c52f + implemented parsing of the relocation section 2023-12-28 16:02:27 +02:00
Nikolay Nikolov
7f30a87651 + started implementing the reading of relocation sections in the wasm object reader 2023-12-28 15:53:45 +02:00
Nikolay Nikolov
026781b9a3 * set MaxMemPos in the wasm internal linker 2023-12-28 15:27:53 +02:00
Nikolay Nikolov
eeae32669e + add oso_keep flag to the code sections that contain an export 2023-12-28 14:54:06 +02:00
Nikolay Nikolov
24972d9861 + parse the export table in the wasm internal object reader 2023-12-28 14:50:51 +02:00
Nikolay Nikolov
4b9e1ab30a + wasm object reader: create code sections and symbols 2023-12-28 14:29:59 +02:00
Nikolay Nikolov
4b179ce4fa + check the code entries count in the wasm object reader 2023-12-28 14:14:30 +02:00
Nikolay Nikolov
87a3bfe99d + fully implemented reading the code section 2023-12-28 13:25:44 +02:00
Nikolay Nikolov
3d5a100f79 + wasm object reader: implemented parsing of the global section 2023-12-28 12:42:48 +02:00
Nikolay Nikolov
4ba064d894 + include mod name in symbols 2023-12-27 15:15:54 +02:00
Nikolay Nikolov
c293d0b729 + partially implemented the creation of symbols for SYMTAB_FUNCTION entries 2023-12-27 15:14:21 +02:00
Nikolay Nikolov
a6d4374f10 + wasm object reader: range check the symbol indices of SYMTAB_FUNCTION entries 2023-12-27 14:52:37 +02:00
Nikolay Nikolov
efc4ab7928 + store in the FuncTypes array the func types, parsed in the function section 2023-12-27 14:47:45 +02:00
Nikolay Nikolov
a0df7c27af + wasm obj reader: store the parsed import data from the import section in local arrays and structures 2023-12-27 14:42:01 +02:00
Nikolay Nikolov
ced7e40c43 + support local binding when reading data symbols in the internal wasm object reader 2023-12-27 13:36:18 +02:00
Nikolay Nikolov
ee4d77b119 + create data symbols in the wasm object reader 2023-12-27 13:14:27 +02:00
Nikolay Nikolov
e569863a1a + check bounds for SymIndex of SYMTAB_DATA symbols 2023-12-27 13:01:49 +02:00
Nikolay Nikolov
121e793c37 + wasm object reader: store the symbol table in a dynamic array, so it can be processed later 2023-12-27 11:52:17 +02:00
Nikolay Nikolov
c6e054dbb9 + implemented parsing of the symbol table in the wasm object reader 2023-12-27 10:59:02 +02:00
Nikolay Nikolov
272db1b872 + symbol table subsection duplicate check 2023-12-27 10:31:24 +02:00
Nikolay Nikolov
b641687da2 * attempt to avoid warning, treated as error, causing CI failure 2023-12-26 20:46:25 +02:00
Nikolay Nikolov
fc7ed302e7 + check and report an error if a passive data segment is encountered by the wasm object reader 2023-12-26 20:42:46 +02:00
Nikolay Nikolov
8606f13b24 + check for memory index other than 0 in the wasm object reader and report an error, since that is not supported 2023-12-26 20:41:40 +02:00
Nikolay Nikolov
23fa8c80b6 + read the section content in the wasm object reader 2023-12-26 20:38:58 +02:00
Nikolay Nikolov
56fe6cadc8 * fill the data in the DataSegments array when parsing the WASM_SEGMENT_INFO subsection 2023-12-26 20:24:52 +02:00
Nikolay Nikolov
50f76a2ffb * moved another local var to avoid bug 2023-12-26 20:22:47 +02:00
Nikolay Nikolov
2ab16a5a8f - removed debug writeln 2023-12-26 20:21:25 +02:00
Nikolay Nikolov
e754937ad3 + verify the segment count when parsing the WASM_SEGMENT_INFO subsection
* moved local vars to avoid bug
2023-12-26 20:19:58 +02:00
Nikolay Nikolov
bbb564437d + parse the data section in the wasm object reader 2023-12-26 17:42:01 +02:00
Nikolay Nikolov
818b6e6983 * check that the data count section preceeds the data section and report an error otherwise 2023-12-26 16:47:10 +02:00
Nikolay Nikolov
e4600f5e38 + read the data count from the data section. Set size of the DataSegments
array, if the data count section hasn't been encountered. Compare the count
  with the value from the data count section, in case the data count section
  has been encountered, and issue an error, if they differ.
2023-12-26 16:44:34 +02:00
Nikolay Nikolov
10b201c506 + check for duplicated data sections 2023-12-26 16:39:29 +02:00
Nikolay Nikolov
6f7c1b5b4e + added DataSegments dynarray of record, that will store data segment info 2023-12-26 16:32:30 +02:00
Nikolay Nikolov
e3c5d016e0 + check for duplicated WASM_SEGMENT_INFO subsections 2023-12-26 16:26:37 +02:00
Nikolay Nikolov
3121ee1367 + parse the WASM_SEGMENT_INFO subsection 2023-12-26 16:24:05 +02:00
Nikolay Nikolov
8a7743b566 * set FReader and InputFileName in the beginning of TWasmObjInput.ReadObjData 2023-12-26 16:07:34 +02:00
Nikolay Nikolov
e7f6da4cb0 * the 'linking' section is a section, not a subsection. Fix error messages. 2023-12-26 15:07:42 +02:00
Nikolay Nikolov
9063c3c0ed + parse the linking custom section by splitting it into subsections and call subfunctions (dummy for now) for each subsection 2023-12-26 15:06:03 +02:00
Nikolay Nikolov
8802c7d489 + parse and check the version of the 'linking' section 2023-12-26 14:42:51 +02:00
Nikolay Nikolov
5a5abe2419 + started parsing of custom sections - known custom sections are dispatched to
a specialized parsing function for each of them, while unknown custom sections
  (currently the ones, not produced by FPC) produce an error
2023-12-26 14:32:34 +02:00
Nikolay Nikolov
2ae98a7363 + read the code entries count from the code section 2023-12-26 13:57:55 +02:00
Nikolay Nikolov
7d41f5eb92 + added code section duplicate check in wasm object reader 2023-12-26 13:49:40 +02:00
Nikolay Nikolov
b2861bacdd - removed the repeated section bounds check in the section parsing code,
because now all reads go through the read function, which does section bounds
  checking on every read
2023-12-26 13:42:07 +02:00
Nikolay Nikolov
8591f1218c + added section bounds checking to the read function, used inside TWasmObjInput.ReadObjData.ReadSection 2023-12-26 13:40:26 +02:00
Nikolay Nikolov
16c18e845f - removed the TObjectReader parameter from ReadUleb, ReadUleb32 and ReadName 2023-12-26 13:24:29 +02:00
Nikolay Nikolov
01d97bea00 - rm commented out code 2023-12-26 13:13:42 +02:00
Nikolay Nikolov
36fe4cf43d * the ReadUleb, ReadUleb32 and ReadName methods moved to be local functions inside TWasmObjInput.ReadObjData.ReadSection 2023-12-26 13:12:44 +02:00
Nikolay Nikolov
c9b407508a * use ReadUleb32 in more places 2023-12-26 12:46:06 +02:00
Nikolay Nikolov
621699acca + introduced ReadUleb32 to avoid code repetition of the >high(uint32) checks 2023-12-26 11:43:49 +02:00
Nikolay Nikolov
bcb7c069fb + parse the data count section 2023-12-26 10:25:20 +02:00
Nikolay Nikolov
f87a66f1e6 + parse the rest of the function section 2023-12-24 16:56:28 +02:00
Nikolay Nikolov
c7880e7bd1 + read the functions count from the functions section 2023-12-24 16:53:05 +02:00
Nikolay Nikolov
be56b641d7 + function section duplicate check 2023-12-24 16:51:23 +02:00
Nikolay Nikolov
e81b062e2d + WebAssembly object reader: implemented parsing of the import section 2023-12-24 16:35:39 +02:00
Nikolay Nikolov
e6a3aa3450 + import section duplicate check 2023-12-24 16:35:39 +02:00
Nikolay Nikolov
3bc6340424 - removed TODO comment 2023-12-24 15:28:01 +02:00
Nikolay Nikolov
01dbc63735 + input error message in case the type section size is unexpected/wrong 2023-12-24 15:04:22 +02:00
Nikolay Nikolov
a08c937beb + Wasm internal linker, object reader: implemented parsing of the type section 2023-12-24 14:57:16 +02:00
Nikolay Nikolov
90e7b2be56 + added functions for reading the different types of Wasm object sections 2023-12-24 14:12:53 +02:00
Nikolay Nikolov
54eb8de658 * write a WebAssembly header in TWasmExeOutput.writeData 2023-12-24 12:42:29 +02:00
Nikolay Nikolov
6bead20fc3 * set CObjData in the TWasmExeOutput constructor 2023-12-24 12:35:50 +02:00
Nikolay Nikolov
5a61f51b3c + added TWasmExeOutput class (not implemented, yet) 2023-11-17 00:12:18 +02:00
Nikolay Nikolov
d98d0c5e62 + started implementing TWasmObjInput.ReadObjData 2023-11-15 17:58:15 +02:00
Nikolay Nikolov
b9668a88c6 + implemented TWasmObjInput.CanReadObjData: checks for WASM header and version 2023-11-15 01:55:12 +02:00
Nikolay Nikolov
bc727f9fb2 + added TWasmObjInput class. To be used in the WASM internal linker. Not doing anything, yet. 2023-11-15 01:39:39 +02:00
Nikolay Nikolov
322befbf56 + added mutable globals to the target switches section 2023-06-17 17:00:36 +03:00
Nikolay Nikolov
9b4366e4f6 + support externref and funcref WebAssembly globals in the internal object writer 2023-06-11 10:41:56 +03:00
Nikolay Nikolov
dd13b46088 * use WasmCustomSectionName to obtain the debug section names in MaybeWriteDebugSection and MaybeAddDebugSectionToSymbolTable 2022-08-03 23:11:44 +03:00
Nikolay Nikolov
efd92fba01 + introduced TWasmCustomDebugSectionType 2022-08-03 23:04:53 +03:00
Nikolay Nikolov
cc260eb7b2 * wasm32 obj writer: use Data.ObjSectionList.Find to find the debug section by name in MaybeWriteDebugSection 2022-08-03 22:02:19 +03:00
Nikolay Nikolov
e959422753 * wasm32 obj writer: do the relocations before writing the code, data and debug sections 2022-08-03 20:56:44 +03:00
Nikolay Nikolov
edf5fce7e3 * wasm32 obj writer: determine the section number for the datacount, code, data and debug sections ahead of time (before writing the sections) 2022-08-03 20:51:58 +03:00
Nikolay Nikolov
b5a71086e8 * wasm32 obj writer: moved the writing of the data sections ahead 2022-08-03 20:44:11 +03:00
Nikolay Nikolov
2a196899bc * wasm32 obj writer: writing the debug section symbols to the symbol table moved before the writing of the debug sections themselves 2022-08-03 20:36:50 +03:00
Nikolay Nikolov
65b4425672 + support declaring wasm globals in the WebAssembly internal object writer 2022-07-31 19:28:09 +03:00
Nikolay Nikolov
5604bd951e * an attempt to fix WebAssembly debug info section relocations 2022-07-22 02:26:00 +03:00
Nikolay Nikolov
63c8e458bb * workaround crash when generating WebAssembly DWARF debug info for threadvars, while multithreading is turned off 2022-07-16 19:31:14 +03:00
Nikolay Nikolov
fcadc80163 * don't write R_WASM_TABLE_INDEX_I32 relocations, always use R_WASM_FUNCTION_OFFSET_I32 in debug sections 2022-07-16 19:08:06 +03:00
Nikolay Nikolov
972a9df031 + always (even when multithreading is off) create a "target features" section
in the WebAssembly object writer, since we still require some (very commonly
  supported) extensions, even without multithreading
2022-07-16 07:42:53 +03:00
Nikolay Nikolov
bf17a84dd2 + implemented the R_WASM_FUNCTION_OFFSET_I32 relocation in the WebAssembly object writer 2022-07-16 04:34:15 +03:00
Nikolay Nikolov
6a94d2683b + emit WASM DWARF debug info relocations 2022-07-16 04:19:50 +03:00
Nikolay Nikolov
90305b6032 + write the actual DWARF debug sections in the WebAssembly object writer. The relocations aren't ready, yet, though. 2022-07-16 02:13:42 +03:00
Nikolay Nikolov
b82af25e63 * fixed crashes with the WebAssembly internal object writer, when debug info is enabled. The debug info itself is not yet written to the file, though. 2022-07-16 01:40:21 +03:00
Nikolay Nikolov
a9698b6f6e + introduced TWasmObjSection.IsDebug 2022-07-16 00:54:11 +03:00
Nikolay Nikolov
4db653998b + generate proper WebAssembly threadvar access when multithreading is turned on 2022-07-14 02:39:07 +03:00
Nikolay Nikolov
fa5575ea54 + prefix the threadvar sections with '.tbss' when WASM multithreading is enabled 2022-07-13 23:27:30 +03:00
Nikolay Nikolov
2606cef21f + set the TLS flag for threadvar sections when WASM threads are enabled 2022-07-13 23:19:48 +03:00
Nikolay Nikolov
7cd082ba5f + produce a target_features section when compiling with -CTwasmthreads 2022-05-28 04:04:02 +03:00
Nikolay Nikolov
1dd80d596d + added a 'producers' custom section to WebAssembly object modules, containing the FPC version 2022-05-28 03:33:04 +03:00
Nikolay Nikolov
ee89b7c9ec * fixed warning 2022-05-28 00:47:23 +03:00
Nikolay Nikolov
0c6287894b * the WebAssembly internal assembler renamed WASM (previously it was OMF, which was the ID of the MS-DOS .obj writer, which I forgot to rename) 2022-05-26 14:12:10 +03:00
Pierre Muller
7c21b37105 Avoid some range check error and convert internalerror into error messages 2022-05-26 00:28:47 +02:00
Pierre Muller
c9e8bfd118 Add internalerror if ogwasm tries to write unset Func/Global or TagIndex of TWasmObjSymbol 2022-02-11 22:19:43 +00:00
Nikolay Nikolov
cdf1ceca76 * emit a weak symbol for the exception tag, since that's what the LLVM linker
wants to treat them as shared between .o files
2021-09-28 02:52:40 +03:00
Nikolay Nikolov
b463f2a141 + added support for exception tags and for the 'throw' and 'catch' instructions
in the wasm internal assembler and object writer
2021-09-27 21:44:09 +03:00
Nikolay Nikolov
b46c010f82 + also support immutable wasm globals in the wasm assembler writer (both internal and external) 2021-09-27 05:40:26 +03:00
Nikolay Nikolov
7b31549119 * the data section is now only written by the internal wasm object writer, when
there are data segments in the module
2021-09-27 05:17:20 +03:00
Nikolay Nikolov
e85aff2b96 * combined TWasmObjSymbol.ImportIndex and .FuncIndex into just .FuncIndex,
because there's no point in having them separate, and the name ImportIndex
  is misleading, as there are multiple import indices (they are separate for
  functions, globals, tables, memories, etc.)
2021-09-27 04:53:35 +03:00
Nikolay Nikolov
2bce4d36db + proper support for WASM globals in the internal object writer - emit
relocations, generate a Global section, if they are declared in the object
  file, etc.
2021-09-27 04:24:49 +03:00
Nikolay Nikolov
5ad2d47e23 * track wasm global types in the internal object writer 2021-09-27 03:03:57 +03:00
Nikolay Nikolov
0bb781c928 * omit writing the export section if there are no exported functions from
the module. This reduces object file size slightly for modules without
  exports.
2021-09-26 23:14:52 +03:00
Nikolay Nikolov
d37117c460 * use an incrementing counter variable to determine the numbers for the code
and data section, so they can be used in the relocation section, without
  hardcoding them as constants. This will automatically adjust the numbers
  when we add more sections, or when we make writing some of the sections
  optional.
2021-09-26 23:10:55 +03:00
Nikolay Nikolov
77b898fe45 * use objsym.size to determine the code size of the function (instead of
writing until the end of the section) in the wasm internal object writer.
  This removes the extra padding of 'unreachable' instructions at the end of
  each function (which are zeroes, added for alignment - they are harmless,
  but are unnecessary and cause a slight increase of the size of the
  resulting binaries), after the final 'return' instruction of the function.
2021-09-26 22:21:34 +03:00
Nikolay Nikolov
2102fbdc97 - removed debug writelns from the wasm internal object writer 2021-09-26 20:13:35 +03:00
Nikolay Nikolov
99c84da071 + generate the export section in the wasm internal obj writer 2021-09-26 20:02:48 +03:00
Nikolay Nikolov
abf831c430 + fixed the addend in the relocations that point to data 2021-09-26 11:12:08 +03:00
Nikolay Nikolov
c881d05793 * fixed bug in the code, generated for the RELOC_FUNCTION_INDEX_LEB
relocation
2021-09-26 10:29:17 +03:00
Nikolay Nikolov
07461d0fc3 * made the constructor of TObjSymbol virtual, so it can be overriden 2021-09-26 10:17:19 +03:00
Nikolay Nikolov
efd5831742 * generate an internal error if an attempt is made to emit a
R_WASM_MEMORY_ADDR_LEB relocation, that points to code, instead of
  data
2021-09-26 09:42:12 +03:00
Nikolay Nikolov
392ca81c20 + add threadvar symbols to the symbol table 2021-09-26 09:40:28 +03:00
Nikolay Nikolov
d7b141d70e + implemented the if, loop and else instructions 2021-09-26 07:45:14 +03:00
Nikolay Nikolov
ea401ccc82 + support writing R_WASM_MEMORY_ADDR_I32 relocations 2021-09-26 06:27:02 +03:00
Nikolay Nikolov
7f5250bf46 + implemented support for R_WASM_TABLE_INDEX_SLEB relocations 2021-09-26 06:22:41 +03:00
Nikolay Nikolov
9fa2e2934b + support the call_indirect instruction and the R_WASM_TYPE_INDEX_LEB
relocation that it requires in the internal wasm object writer
2021-09-26 06:03:18 +03:00
Nikolay Nikolov
bd6bbb6cf4 + support writing imports from a different module in the wasm binary object writer 2021-09-26 05:36:51 +03:00
Nikolay Nikolov
238d09d875 + support writing RELOC_FUNCTION_INDEX_LEB relocations to non-external functions 2021-09-26 05:15:50 +03:00
Nikolay Nikolov
bf5048cb5e + support generating R_WASM_TABLE_INDEX_I32 relocations 2021-09-26 04:46:46 +03:00
Nikolay Nikolov
acdb65c2a3 + support tai_symbolpair in the wasm internal obj writer, so that the
PASCALMAIN alias of main can be created
2021-09-26 04:19:16 +03:00
Nikolay Nikolov
6e7e632682 * fixed another warning 2021-09-26 03:16:01 +03:00
Nikolay Nikolov
4ebf857ffc * fixed warning 2021-09-26 03:05:00 +03:00
Nikolay Nikolov
b913133152 + implemented leb and sleb relocations to data symbols 2021-09-26 02:23:31 +03:00
Nikolay Nikolov
67cbb7032e + fully implemented the RELOC_FUNCTION_INDEX_LEB relocations 2021-09-26 00:45:52 +03:00
Nikolay Nikolov
4e4910cb84 + write the relocation tables for the code and data sections (empty for now) 2021-09-25 23:47:30 +03:00
Nikolay Nikolov
217b3ea3cc + write the segment info (names and alignment) to the linking custom section 2021-09-25 23:30:43 +03:00
Nikolay Nikolov
5066a7eb24 + also write the data symbols to the symbol table 2021-09-25 23:20:15 +03:00
Nikolay Nikolov
ad8ed4b48d + write the non-external functions to the symbol table as well 2021-09-25 23:03:09 +03:00
Nikolay Nikolov
2d2906205b + write the externals to the symbol table 2021-09-25 22:45:47 +03:00
Nikolay Nikolov
8c57d20abc + write the linking section (empty for now) 2021-09-25 19:19:12 +03:00
Nikolay Nikolov
12496a638b + add relocation objects for the RELOC_FUNCTION_INDEX_LEB relocation to
the relocation list. They are not written to the object file, yet.
2021-09-25 18:37:10 +03:00
Nikolay Nikolov
a5d1ce26a0 + partial implementation of emitting RELOC_FUNCTION_INDEX_LEB relocations 2021-09-25 18:24:27 +03:00
Nikolay Nikolov
cbf444bbbe + write the actual function code in the code section 2021-09-25 18:08:01 +03:00
Nikolay Nikolov
dbb6f95ea7 * replaced TWasmObjData(Data) with FData in TWasmObjOutput.writeData 2021-09-25 17:49:19 +03:00
Nikolay Nikolov
6d1df899e6 + write the code section (still, without the actual function code, but with
the correct locals)
2021-09-25 17:41:42 +03:00