| 36 | namespace WasmEdge::Loader { |
| 37 | |
| 38 | Expect<void> AOTSection::load(const AST::AOTSection &AOTSec) noexcept { |
| 39 | BinarySize = 0; |
| 40 | for (const auto &Section : AOTSec.getSections()) { |
| 41 | const auto Offset = std::get<1>(Section); |
| 42 | const auto Size = std::get<2>(Section); |
| 43 | BinarySize = std::max(BinarySize, Offset + Size); |
| 44 | } |
| 45 | BinarySize = roundUpPageBoundary(BinarySize); |
| 46 | |
| 47 | Binary = Allocator::allocate_chunk(BinarySize); |
| 48 | if (unlikely(!Binary)) { |
| 49 | spdlog::error(ErrCode::Value::MemoryOutOfBounds); |
| 50 | return Unexpect(ErrCode::Value::MemoryOutOfBounds); |
| 51 | } |
| 52 | |
| 53 | std::vector<std::pair<uint8_t *, uint64_t>> ExecutableRanges; |
| 54 | for (const auto &Section : AOTSec.getSections()) { |
| 55 | const auto Offset = std::get<1>(Section); |
| 56 | const auto Size = std::get<2>(Section); |
| 57 | const auto &Content = std::get<3>(Section); |
| 58 | if (Size > BinarySize || Offset > BinarySize || |
| 59 | Offset + Size > BinarySize || Content.size() > Size) { |
| 60 | return Unexpect(ErrCode::Value::IntegerTooLarge); |
| 61 | } |
| 62 | std::copy(Content.begin(), Content.end(), Binary + Offset); |
| 63 | switch (std::get<0>(Section)) { |
| 64 | case 1: { // Text |
| 65 | const auto O = roundDownPageBoundary(Offset); |
| 66 | const auto S = roundUpPageBoundary(Size + (Offset - O)); |
| 67 | ExecutableRanges.emplace_back(Binary + O, S); |
| 68 | break; |
| 69 | } |
| 70 | case 2: // Data |
| 71 | break; |
| 72 | case 3: // BSS |
| 73 | break; |
| 74 | #if WASMEDGE_OS_LINUX |
| 75 | case 4: // EHFrame |
| 76 | EHFrameAddress = reinterpret_cast<void *>(Binary + Offset); |
| 77 | break; |
| 78 | #elif WASMEDGE_OS_MACOS |
| 79 | case 4: // EHFrame |
| 80 | EHFrameAddress = reinterpret_cast<uint8_t *>(Binary + Offset); |
| 81 | EHFrameSize = Size; |
| 82 | break; |
| 83 | #elif WASMEDGE_OS_WINDOWS |
| 84 | case 4: // PData |
| 85 | PDataAddress = reinterpret_cast<void *>(Binary + Offset); |
| 86 | PDataSize = |
| 87 | static_cast<uint32_t>(Size / sizeof(winapi::RUNTIME_FUNCTION_)); |
| 88 | break; |
| 89 | #endif |
| 90 | default: |
| 91 | return Unexpect(ErrCode::Value::IntegerTooLarge); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | for (const auto &[Pointer, Size] : ExecutableRanges) { |
no test coverage detected