57 lines · cpp
1//===-- MemoryWriter.cpp --------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "MemoryWriter.h"10#include "CoreSpec.h"11#include "Utility.h"12#include "llvm/BinaryFormat/MachO.h"13 14void create_lc_segment_cmd(const CoreSpec &spec, std::vector<uint8_t> &cmds,15 const MemoryRegion &memory, off_t data_offset) {16 if (spec.wordsize == 8) {17 // Add the bytes for a segment_command_64 from <mach-o/loader.h>18 add_uint32(cmds, llvm::MachO::LC_SEGMENT_64);19 add_uint32(cmds, sizeof(struct llvm::MachO::segment_command_64));20 for (int i = 0; i < 16; i++)21 cmds.push_back(0);22 add_uint64(cmds, memory.addr); // segment_command_64.vmaddr23 add_uint64(cmds, memory.size); // segment_command_64.vmsize24 add_uint64(cmds, data_offset); // segment_command_64.fileoff25 add_uint64(cmds, memory.size); // segment_command_64.filesize26 } else {27 // Add the bytes for a segment_command from <mach-o/loader.h>28 add_uint32(cmds, llvm::MachO::LC_SEGMENT);29 add_uint32(cmds, sizeof(struct llvm::MachO::segment_command));30 for (int i = 0; i < 16; i++)31 cmds.push_back(0);32 add_uint32(cmds, memory.addr); // segment_command_64.vmaddr33 add_uint32(cmds, memory.size); // segment_command_64.vmsize34 add_uint32(cmds, data_offset); // segment_command_64.fileoff35 add_uint32(cmds, memory.size); // segment_command_64.filesize36 }37 add_uint32(cmds, 3); // segment_command_64.maxprot38 add_uint32(cmds, 3); // segment_command_64.initprot39 add_uint32(cmds, 0); // segment_command_64.nsects40 add_uint32(cmds, 0); // segment_command_64.flags41}42 43void create_memory_bytes(const CoreSpec &spec, const MemoryRegion &memory,44 std::vector<uint8_t> &buf) {45 if (memory.type == MemoryType::UInt8)46 for (uint8_t byte : memory.bytes)47 buf.push_back(byte);48 49 if (memory.type == MemoryType::UInt32)50 for (uint32_t word : memory.words)51 add_uint32(buf, word);52 53 if (memory.type == MemoryType::UInt64)54 for (uint64_t word : memory.doublewords)55 add_uint64(buf, word);56}57