1#include <inttypes.h>
2#include <mach-o/loader.h>
3#include <mach/thread_status.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <string>
8#include <sys/errno.h>
9#include <uuid/uuid.h>
10#include <vector>
11
12#define CPU_TYPE_RISCV 24
13#define CPU_SUBTYPE_RISCV_ALL 0
14#define RV32_THREAD_STATE 2
15// x0-x31 + pc, all 32-bit
16#define RV32_THREAD_STATE_COUNT 33
17
18union uint32_buf {
19 uint8_t bytebuf[4];
20 uint32_t val;
21};
22
23union uint64_buf {
24 uint8_t bytebuf[8];
25 uint64_t val;
26};
27
28void add_uint64(std::vector<uint8_t> &buf, uint64_t val) {
29 uint64_buf conv;
30 conv.val = val;
31 for (int i = 0; i < 8; i++)
32 buf.push_back(x: conv.bytebuf[i]);
33}
34
35void add_uint32(std::vector<uint8_t> &buf, uint32_t val) {
36 uint32_buf conv;
37 conv.val = val;
38 for (int i = 0; i < 4; i++)
39 buf.push_back(x: conv.bytebuf[i]);
40}
41
42std::vector<uint8_t> lc_thread_load_command() {
43 std::vector<uint8_t> data;
44 add_uint32(data, LC_THREAD); // thread_command.cmd
45 add_uint32(buf&: data, val: 4 + 4 + 4 + 4 +
46 (RV32_THREAD_STATE_COUNT * 4)); // thread_command.cmdsize
47 add_uint32(buf&: data, RV32_THREAD_STATE); // thread_command.flavor
48 add_uint32(buf&: data, RV32_THREAD_STATE_COUNT); // thread_command.count
49 for (int i = 0; i < RV32_THREAD_STATE_COUNT; i++) {
50 add_uint32(buf&: data, val: i | (i << 8) | (i << 16) | (i << 24));
51 }
52 return data;
53}
54
55int main(int argc, char **argv) {
56 if (argc != 2) {
57 fprintf(stderr,
58 format: "usage: create-empty-riscv-corefile output-corefile-name\n");
59 exit(status: 1);
60 }
61
62 cpu_type_t cputype = CPU_TYPE_RISCV;
63 cpu_subtype_t cpusubtype = CPU_SUBTYPE_RISCV_ALL;
64
65 // An array of load commands (in the form of byte arrays)
66 std::vector<std::vector<uint8_t>> load_commands;
67
68 // An array of corefile contents (page data, lc_note data, etc)
69 std::vector<uint8_t> payload;
70
71 // First add all the load commands / payload so we can figure out how large
72 // the load commands will actually be.
73 load_commands.push_back(x: lc_thread_load_command());
74
75 int size_of_load_commands = 0;
76 for (const auto &lc : load_commands)
77 size_of_load_commands += lc.size();
78
79 int header_and_load_cmd_room =
80 sizeof(struct mach_header_64) + size_of_load_commands;
81
82 // Erase the load commands / payload now that we know how much space is
83 // needed, redo it.
84 load_commands.clear();
85 payload.clear();
86
87 load_commands.push_back(x: lc_thread_load_command());
88
89 struct mach_header mh;
90 mh.magic = MH_MAGIC;
91 mh.cputype = cputype;
92
93 mh.cpusubtype = cpusubtype;
94 mh.filetype = MH_CORE;
95 mh.ncmds = load_commands.size();
96 mh.sizeofcmds = size_of_load_commands;
97 mh.flags = 0;
98
99 FILE *f = fopen(filename: argv[1], modes: "w");
100
101 if (f == nullptr) {
102 fprintf(stderr, format: "Unable to open file %s for writing\n", argv[1]);
103 exit(status: 1);
104 }
105
106 fwrite(&mh, sizeof(struct mach_header), 1, f);
107
108 for (const auto &lc : load_commands)
109 fwrite(ptr: lc.data(), size: lc.size(), n: 1, s: f);
110
111 fseek(stream: f, off: header_and_load_cmd_room, SEEK_SET);
112
113 fwrite(ptr: payload.data(), size: payload.size(), n: 1, s: f);
114
115 fclose(stream: f);
116}
117

source code of lldb/test/API/macosx/riscv32-corefile/create-empty-riscv-corefile.cpp