Setup symbols from loaded binary. See "include/loader/loader.h".
| 155 | |
| 156 | // Setup symbols from loaded binary. See "include/loader/loader.h". |
| 157 | Expect<void> Loader::loadExecutable(AST::Module &Mod, |
| 158 | std::shared_ptr<Executable> Exec) { |
| 159 | auto &SubTypes = Mod.getTypeSection().getContent(); |
| 160 | size_t Offset = 0; |
| 161 | for (const auto &ImpDesc : Mod.getImportSection().getContent()) { |
| 162 | if (ImpDesc.getExternalType() == ExternalType::Function) { |
| 163 | ++Offset; |
| 164 | } |
| 165 | } |
| 166 | auto &CodeSegs = Mod.getCodeSection().getContent(); |
| 167 | |
| 168 | // Check the symbols. |
| 169 | auto FuncTypeSymbols = Exec->getTypes(SubTypes.size()); |
| 170 | auto CodeSymbols = Exec->getCodes(Offset, CodeSegs.size()); |
| 171 | auto IntrinsicsSymbol = Exec->getIntrinsics(); |
| 172 | if (unlikely(FuncTypeSymbols.size() != SubTypes.size())) { |
| 173 | spdlog::error(" AOT section -- number of types not matching:{} {}, " |
| 174 | "use interpreter mode instead.", |
| 175 | FuncTypeSymbols.size(), SubTypes.size()); |
| 176 | return Unexpect(ErrCode::Value::IllegalGrammar); |
| 177 | } |
| 178 | if (unlikely(CodeSymbols.size() != CodeSegs.size())) { |
| 179 | spdlog::error(" AOT section -- number of codes not matching:{} {}, " |
| 180 | "use interpreter mode instead.", |
| 181 | CodeSymbols.size(), CodeSegs.size()); |
| 182 | return Unexpect(ErrCode::Value::IllegalGrammar); |
| 183 | } |
| 184 | if (unlikely(!IntrinsicsSymbol)) { |
| 185 | spdlog::error(" AOT section -- intrinsics table symbol not found, use " |
| 186 | "interpreter mode instead."); |
| 187 | return Unexpect(ErrCode::Value::IllegalGrammar); |
| 188 | } |
| 189 | |
| 190 | // Set the symbols in the module. |
| 191 | uint32_t FuncTypeIdx = 0; |
| 192 | for (auto &SubType : SubTypes) { |
| 193 | if (SubType.getCompositeType().isFunc()) { |
| 194 | SubType.getCompositeType().getFuncType().setSymbol( |
| 195 | std::move(FuncTypeSymbols[FuncTypeIdx])); |
| 196 | } |
| 197 | FuncTypeIdx++; |
| 198 | } |
| 199 | for (size_t I = 0; I < CodeSegs.size(); ++I) { |
| 200 | CodeSegs[I].setSymbol(std::move(CodeSymbols[I])); |
| 201 | } |
| 202 | Mod.setSymbol(std::move(IntrinsicsSymbol)); |
| 203 | // loadExecutable is reached only when native code is being prepared (AOT |
| 204 | // load or JIT compile). Patch the intrinsics-table pointer embedded in |
| 205 | // the produced executable so the native code can call back into the |
| 206 | // runtime. |
| 207 | if (auto &Symbol = Mod.getSymbol()) { |
| 208 | *Symbol = IntrinsicsTable; |
| 209 | } |
| 210 | |
| 211 | return {}; |
| 212 | } |
| 213 | |
| 214 | Expect<void> Loader::loadUniversalWASM(AST::Module &Mod) { |
no test coverage detected