| 1339 | } |
| 1340 | |
| 1341 | Error CodeHolder::copy_flattened_data(void* dst, size_t dst_size, CopySectionFlags copy_flags) noexcept { |
| 1342 | size_t end = 0; |
| 1343 | for (Section* section : _sections_by_order) { |
| 1344 | if (section->offset() > dst_size) { |
| 1345 | return make_error(Error::kInvalidArgument); |
| 1346 | } |
| 1347 | |
| 1348 | size_t buffer_size = section->buffer_size(); |
| 1349 | size_t offset = size_t(section->offset()); |
| 1350 | |
| 1351 | if (ASMJIT_UNLIKELY(dst_size - offset < buffer_size)) { |
| 1352 | return make_error(Error::kInvalidArgument); |
| 1353 | } |
| 1354 | |
| 1355 | uint8_t* dst_target = static_cast<uint8_t*>(dst) + offset; |
| 1356 | size_t padding_size = 0; |
| 1357 | memcpy(dst_target, section->data(), buffer_size); |
| 1358 | |
| 1359 | if (Support::test(copy_flags, CopySectionFlags::kPadSectionBuffer) && buffer_size < section->virtual_size()) { |
| 1360 | padding_size = Support::min<size_t>(dst_size - offset, size_t(section->virtual_size())) - buffer_size; |
| 1361 | memset(dst_target + buffer_size, 0, padding_size); |
| 1362 | } |
| 1363 | |
| 1364 | end = Support::max(end, offset + buffer_size + padding_size); |
| 1365 | } |
| 1366 | |
| 1367 | if (end < dst_size && Support::test(copy_flags, CopySectionFlags::kPadTargetBuffer)) { |
| 1368 | memset(static_cast<uint8_t*>(dst) + end, 0, dst_size - end); |
| 1369 | } |
| 1370 | |
| 1371 | return Error::kOk; |
| 1372 | } |
| 1373 | |
| 1374 | // CodeHolder - Tests |
| 1375 | // ================== |