forked from ZigEmbeddedGroup/microzig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_linker_script.zig
More file actions
279 lines (242 loc) · 8.04 KB
/
generate_linker_script.zig
File metadata and controls
279 lines (242 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const std = @import("std");
const microzig = @import("build-internals");
const MemoryRegion = microzig.MemoryRegion;
const GenerateOptions = microzig.LinkerScript.GenerateOptions;
pub const Args = struct {
cpu_name: []const u8,
cpu_arch: std.Target.Cpu.Arch,
chip_name: []const u8,
memory_regions: []const MemoryRegion,
generate: GenerateOptions,
ram_image: bool,
};
pub fn main() !void {
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
defer _ = debug_allocator.deinit();
var arena = std.heap.ArenaAllocator.init(debug_allocator.allocator());
defer arena.deinit();
const allocator = arena.allocator();
const args = try std.process.argsAlloc(allocator);
if (args.len < 3 or args.len > 4) {
return error.UsageError;
}
const json_args = args[1];
const output_path = args[2];
const parsed_args = try std.json.parseFromSliceLeaky(Args, allocator, json_args, .{});
const maybe_user_linker_script = if (args.len == 4)
try std.fs.cwd().readFileAlloc(allocator, args[3], 100 * 1024 * 1024)
else
null;
const file = try std.fs.cwd().createFile(output_path, .{});
defer file.close();
const writer = file.writer();
try writer.print(
\\/*
\\ * Target CPU: {[cpu]s}
\\ * Target Chip: {[chip]s}
\\ */
\\
\\
, .{
.cpu = parsed_args.cpu_name,
.chip = parsed_args.chip_name,
});
// name all unnamed regions
const region_names: [][]const u8 = try allocator.alloc([]const u8, parsed_args.memory_regions.len);
{
var counters: [5]usize = @splat(0);
for (region_names, parsed_args.memory_regions) |*region_name, region| {
if (region.name) |name| {
region_name.* = try allocator.dupe(u8, name);
} else {
region_name.* = try std.fmt.allocPrint(allocator, "{s}{}", .{
@tagName(region.tag),
counters[@intFromEnum(region.tag)],
});
}
counters[@intFromEnum(region.tag)] += 1;
}
}
if (parsed_args.generate != .none) {
try writer.writeAll(
\\/*
\\ * This section was auto-generated by microzig.
\\ */
\\MEMORY
\\{
\\
);
for (region_names, parsed_args.memory_regions) |region_name, region| {
// flash (rx!w) : ORIGIN = 0x00000000, LENGTH = 512k
try writer.print(" {s} (", .{region_name});
if (region.access.read) try writer.writeAll("r");
if (region.access.write) try writer.writeAll("w");
if (region.access.execute) try writer.writeAll("x");
if (!region.access.read or !region.access.write or !region.access.execute) {
try writer.writeAll("!");
if (!region.access.read) try writer.writeAll("r");
if (!region.access.write) try writer.writeAll("w");
if (!region.access.execute) try writer.writeAll("x");
}
try writer.writeAll(")");
try writer.print(" : ORIGIN = 0x{X:0>8}, LENGTH = 0x{X:0>8}\n", .{ region.offset, region.length });
}
try writer.writeAll("}\n");
}
if (parsed_args.generate == .memory_regions_and_sections) {
const flash_region_name = for (region_names, parsed_args.memory_regions) |region_name, region| {
if (region.tag == .flash) {
break region_name;
}
} else return error.NoFlashRegion;
const ram_region_name, const ram_region = for (region_names, parsed_args.memory_regions) |region_name, region| {
if (region.tag == .ram) {
break .{ region_name, region };
}
} else return error.NoRamRegion;
if (parsed_args.ram_image and !ram_region.access.execute) {
return error.RamRegionNotExecutableInRamImage;
}
const options = parsed_args.generate.memory_regions_and_sections;
try writer.writeAll(
\\SECTIONS
\\{
\\
);
if (!parsed_args.ram_image) {
try writer.print(
\\ .flash_start :
\\ {{
\\ KEEP(*(microzig_flash_start))
\\ }} > {s}
\\
,
.{flash_region_name},
);
} else {
try writer.print(
\\ .ram_start :
\\ {{
\\ KEEP(*(microzig_ram_start))
\\ }} > {s}
\\
,
.{ram_region_name},
);
}
try writer.writeAll(
\\
\\ .text :
\\ {
\\ *(.text*)
\\
);
if (parsed_args.ram_image) {
try writer.writeAll(
\\ *(.ram_text*)
\\
);
}
if (options.rodata_location == .flash and !parsed_args.ram_image) {
try writer.writeAll(
\\ *(.srodata*)
\\ *(.rodata*)
\\
);
}
try writer.print(
\\ }} > {s}
\\
, .{if (!parsed_args.ram_image) flash_region_name else ram_region_name});
switch (parsed_args.cpu_arch) {
.arm, .thumb => try writer.print(
\\
\\ .ARM.extab : {{
\\ *(.ARM.extab* .gnu.linkonce.armextab.*)
\\ }} > {[flash]s}
\\
\\ .ARM.exidx : {{
\\ *(.ARM.exidx* .gnu.linkonce.armexidx.*)
\\ }} > {[flash]s}
\\
, .{ .flash = if (!parsed_args.ram_image) flash_region_name else ram_region_name }),
else => {},
}
try writer.writeAll(
\\
\\ .data :
\\ {
\\ microzig_data_start = .;
\\ *(.sdata*)
\\ *(.data*)
\\
);
if (options.rodata_location == .ram or parsed_args.ram_image) {
try writer.writeAll(
\\ *(.srodata*)
\\ *(.rodata*)
\\
);
}
if (ram_region.access.execute and !parsed_args.ram_image) {
try writer.writeAll(
// NOTE: should this be `microzig_ram_text`?
\\ KEEP(*(.ram_text))
\\
);
}
try writer.print(
\\ microzig_data_end = .;
\\ }} > {s}
\\
\\ .bss (NOLOAD):
\\ {{
\\ microzig_bss_start = .;
\\ *(.sbss*)
\\ *(.bss*)
\\ microzig_bss_end = .;
\\ }} > {s}
\\
, .{
if (!parsed_args.ram_image)
try std.fmt.allocPrint(allocator, "{s} AT> {s}", .{ ram_region_name, flash_region_name })
else
ram_region_name,
ram_region_name,
});
if (!parsed_args.ram_image) {
try writer.print(
\\
\\ .flash_end :
\\ {{
\\ microzig_flash_end = .;
\\ }} > {s}
\\
, .{flash_region_name});
}
try writer.writeAll(
\\
\\ microzig_data_load_start = LOADADDR(.data);
\\
);
switch (parsed_args.cpu_arch) {
.riscv32, .riscv64 => try writer.writeAll(
\\ PROVIDE(__global_pointer$ = microzig_data_start + 0x800);
\\
),
else => {},
}
try writer.writeAll("}\n");
}
if (parsed_args.generate != .none) {
try writer.writeAll(
\\/*
\\ * End of auto-generated section.
\\ */
\\
);
}
if (maybe_user_linker_script) |user_linker_script| {
try writer.writeAll(user_linker_script);
}
}