Browse Source

+ implemented parsing of the symbol table in the wasm object reader

Nikolay Nikolov 1 year ago
parent
commit
c6e054dbb9
1 changed files with 98 additions and 1 deletions
  1. 98 1
      compiler/ogwasm.pas

+ 98 - 1
compiler/ogwasm.pas

@@ -2345,6 +2345,13 @@ implementation
               end;
 
             function ReadSymbolTable: Boolean;
+              var
+                SymCount, SymFlags, TargetSection, SymIndex, SymOffset,
+                  SymSize: uint32;
+                i: Integer;
+                SymKind: Byte;
+                SymName: ansistring;
+                SymKindName: string;
               begin
                 Result:=False;
                 if SymbolTableSectionRead then
@@ -2353,6 +2360,92 @@ implementation
                     exit;
                   end;
                 SymbolTableSectionRead:=True;
+                if not ReadUleb32(SymCount) then
+                  begin
+                    InputError('Error reading the symbol count from the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
+                    exit;
+                  end;
+                for i:=0 to SymCount-1 do
+                  begin
+                    if not Read(SymKind,1) then
+                      begin
+                        InputError('Error reading symbol type from the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
+                        exit;
+                      end;
+                    if not ReadUleb32(SymFlags) then
+                      begin
+                        InputError('Error reading symbol flags from the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
+                        exit;
+                      end;
+                    case SymKind of
+                      byte(SYMTAB_FUNCTION),
+                      byte(SYMTAB_GLOBAL),
+                      byte(SYMTAB_EVENT),
+                      byte(SYMTAB_TABLE):
+                        begin
+                          WriteStr(SymKindName, TWasmSymbolType(SymKind));
+                          if not ReadUleb32(SymIndex) then
+                            begin
+                              InputError('Error reading the index of a ' + SymKindName + ' symbol');
+                              exit;
+                            end;
+                          if ((SymFlags and WASM_SYM_EXPLICIT_NAME)<>0) or
+                             ((SymFlags and WASM_SYM_UNDEFINED)=0) then
+                            begin
+                              if not ReadName(SymName) then
+                                begin
+                                  InputError('Error reading symbol name of a ' + SymKindName + ' symbol');
+                                  exit;
+                                end;
+                            end;
+                        end;
+                      byte(SYMTAB_DATA):
+                        begin
+                          if not ReadName(SymName) then
+                            begin
+                              InputError('Error reading symbol name of a SYMTAB_DATA symbol');
+                              exit;
+                            end;
+                          if (SymFlags and WASM_SYM_UNDEFINED)=0 then
+                            begin
+                              if not ReadUleb32(SymIndex) then
+                                begin
+                                  InputError('Error reading the data segment index of a SYMTAB_DATA symbol');
+                                  exit;
+                                end;
+                              if not ReadUleb32(SymOffset) then
+                                begin
+                                  InputError('Error reading the offset of a SYMTAB_DATA symbol');
+                                  exit;
+                                end;
+                              if not ReadUleb32(SymSize) then
+                                begin
+                                  InputError('Error reading the size of a SYMTAB_DATA symbol');
+                                  exit;
+                                end;
+                            end;
+                        end;
+                      byte(SYMTAB_SECTION):
+                        begin
+                          if not ReadUleb32(TargetSection) then
+                            begin
+                              InputError('Error reading the target section of a SYMTAB_SECTION symbol');
+                              exit;
+                            end;
+                        end;
+                      else
+                        begin
+                          InputError('Unsupported symbol kind: ' + tostr(SymKind));
+                          exit;
+                        end;
+                    end;
+                  end;
+                if AReader.Pos<>(SectionStart+SectionSize) then
+                  begin
+                    InputError('Unexpected WASM_SYMBOL_TABLE section size');
+                    exit;
+                  end;
+                Result:=True;
               end;
 
             const
@@ -2402,7 +2495,11 @@ implementation
                           exit;
                         end;
                     Byte(WASM_SYMBOL_TABLE):
-                      result:=ReadSymbolTable;
+                      if not ReadSymbolTable then
+                        begin
+                          InputError('Error reading the WASM_SYMBOL_TABLE subsection of the ''linking'' section');
+                          exit;
+                        end;
                     else
                       begin
                         InputError('Unsupported ''linking'' section subsection type ' + tostr(SubsectionType));