* Flatten a mapping into a memory buffer. */
| 678 | * Flatten a mapping into a memory buffer. |
| 679 | */ |
| 680 | void flattenMapping(const Binary *B, uint8_t *buf, const Mapping *mapping, |
| 681 | uint8_t fill) |
| 682 | { |
| 683 | memset(buf, fill, mapping->size); |
| 684 | |
| 685 | auto iend = Allocator::end(); |
| 686 | for (; mapping != nullptr; mapping = mapping->merged) |
| 687 | { |
| 688 | auto i = mapping->i; |
| 689 | const Alloc *a = *i; |
| 690 | const size_t SIZE = mapping->size; |
| 691 | const intptr_t BASE = mapping->base; |
| 692 | const intptr_t END = BASE + SIZE; |
| 693 | for (; i != iend; ++i) |
| 694 | { |
| 695 | a = *i; |
| 696 | if (a->lb >= END) |
| 697 | break; |
| 698 | if (a->bytes == nullptr) |
| 699 | continue; |
| 700 | |
| 701 | intptr_t lb = a->lb, ub = a->ub; |
| 702 | off_t offset = (lb < BASE? BASE - lb: 0); |
| 703 | lb = (lb < BASE? BASE: lb); |
| 704 | ub = (ub > END? END: ub); |
| 705 | memcpy(buf + (lb - BASE), a->bytes + offset, (ub - lb)); |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Get the virtual bounds of a mapping. |