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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
pub const Section = struct {
value: u64 = 0,
size: u64 = 0,
alignment: Atom.Alignment = .@"1",
entsize: u32 = 0,
name_offset: u32 = 0,
type: u32 = 0,
flags: u64 = 0,
output_section_index: u32 = 0,
bytes: std.ArrayListUnmanaged(u8) = .empty,
table: std.HashMapUnmanaged(
String,
Subsection.Index,
IndexContext,
std.hash_map.default_max_load_percentage,
) = .{},
subsections: std.ArrayListUnmanaged(Subsection) = .empty,
finalized_subsections: std.ArrayListUnmanaged(Subsection.Index) = .empty,
pub fn deinit(msec: *Section, allocator: Allocator) void {
msec.bytes.deinit(allocator);
msec.table.deinit(allocator);
msec.subsections.deinit(allocator);
msec.finalized_subsections.deinit(allocator);
}
pub fn name(msec: Section, elf_file: *Elf) [:0]const u8 {
return elf_file.getShString(msec.name_offset);
}
pub fn address(msec: Section, elf_file: *Elf) i64 {
const shdr = elf_file.sections.items(.shdr)[msec.output_section_index];
return @intCast(shdr.sh_addr + msec.value);
}
const InsertResult = struct {
found_existing: bool,
key: String,
sub: *Subsection.Index,
};
pub fn insert(msec: *Section, allocator: Allocator, string: []const u8) !InsertResult {
const gop = try msec.table.getOrPutContextAdapted(
allocator,
string,
IndexAdapter{ .bytes = msec.bytes.items },
IndexContext{ .bytes = msec.bytes.items },
);
if (!gop.found_existing) {
const index: u32 = @intCast(msec.bytes.items.len);
try msec.bytes.appendSlice(allocator, string);
gop.key_ptr.* = .{ .pos = index, .len = @intCast(string.len) };
}
return .{ .found_existing = gop.found_existing, .key = gop.key_ptr.*, .sub = gop.value_ptr };
}
pub fn insertZ(msec: *Section, allocator: Allocator, string: []const u8) !InsertResult {
const with_null = try allocator.alloc(u8, string.len + 1);
defer allocator.free(with_null);
@memcpy(with_null[0..string.len], string);
with_null[string.len] = 0;
return msec.insert(allocator, with_null);
}
/// Finalizes the merge section and clears hash table.
/// Sorts all owned subsections.
pub fn finalize(msec: *Section, allocator: Allocator) !void {
try msec.finalized_subsections.ensureTotalCapacityPrecise(allocator, msec.subsections.items.len);
var it = msec.table.iterator();
while (it.next()) |entry| {
const msub = msec.mergeSubsection(entry.value_ptr.*);
if (!msub.alive) continue;
msec.finalized_subsections.appendAssumeCapacity(entry.value_ptr.*);
}
msec.table.clearAndFree(allocator);
const sortFn = struct {
pub fn sortFn(ctx: *Section, lhs: Subsection.Index, rhs: Subsection.Index) bool {
const lhs_msub = ctx.mergeSubsection(lhs);
const rhs_msub = ctx.mergeSubsection(rhs);
if (lhs_msub.alignment.compareStrict(.eq, rhs_msub.alignment)) {
if (lhs_msub.size == rhs_msub.size) {
const lhs_string = ctx.bytes.items[lhs_msub.string_index..][0..lhs_msub.size];
const rhs_string = ctx.bytes.items[rhs_msub.string_index..][0..rhs_msub.size];
return mem.order(u8, lhs_string, rhs_string) == .lt;
}
return lhs_msub.size < rhs_msub.size;
}
return lhs_msub.alignment.compareStrict(.lt, rhs_msub.alignment);
}
}.sortFn;
std.mem.sort(Subsection.Index, msec.finalized_subsections.items, msec, sortFn);
}
pub fn updateSize(msec: *Section) void {
// TODO a 'stale' flag would be better here perhaps?
msec.size = 0;
msec.alignment = .@"1";
msec.entsize = 0;
for (msec.finalized_subsections.items) |msub_index| {
const msub = msec.mergeSubsection(msub_index);
assert(msub.alive);
const offset = msub.alignment.forward(msec.size);
const padding = offset - msec.size;
msub.value = @intCast(offset);
msec.size += padding + msub.size;
msec.alignment = msec.alignment.max(msub.alignment);
msec.entsize = if (msec.entsize == 0) msub.entsize else @min(msec.entsize, msub.entsize);
}
}
pub fn initOutputSection(msec: *Section, elf_file: *Elf) !void {
msec.output_section_index = elf_file.sectionByName(msec.name(elf_file)) orelse try elf_file.addSection(.{
.name = msec.name_offset,
.type = msec.type,
.flags = msec.flags,
});
}
pub fn addMergeSubsection(msec: *Section, allocator: Allocator) !Subsection.Index {
const index: Subsection.Index = @intCast(msec.subsections.items.len);
const msub = try msec.subsections.addOne(allocator);
msub.* = .{};
return index;
}
pub fn mergeSubsection(msec: *Section, index: Subsection.Index) *Subsection {
assert(index < msec.subsections.items.len);
return &msec.subsections.items[index];
}
pub const IndexContext = struct {
bytes: []const u8,
pub fn eql(_: @This(), a: String, b: String) bool {
return a.pos == b.pos;
}
pub fn hash(ctx: @This(), key: String) u64 {
const str = ctx.bytes[key.pos..][0..key.len];
return std.hash_map.hashString(str);
}
};
pub const IndexAdapter = struct {
bytes: []const u8,
pub fn eql(ctx: @This(), a: []const u8, b: String) bool {
const str = ctx.bytes[b.pos..][0..b.len];
return mem.eql(u8, a, str);
}
pub fn hash(_: @This(), adapted_key: []const u8) u64 {
return std.hash_map.hashString(adapted_key);
}
};
pub fn fmt(msec: Section, elf_file: *Elf) std.fmt.Alt(Format, Format.default) {
return .{ .data = .{
.msec = msec,
.elf_file = elf_file,
} };
}
const Format = struct {
msec: Section,
elf_file: *Elf,
pub fn default(f: Format, writer: *std.Io.Writer) std.Io.Writer.Error!void {
const msec = f.msec;
const elf_file = f.elf_file;
try writer.print("{s} : @{x} : size({x}) : align({x}) : entsize({x}) : type({x}) : flags({x})\n", .{
msec.name(elf_file),
msec.address(elf_file),
msec.size,
msec.alignment.toByteUnits() orelse 0,
msec.entsize,
msec.type,
msec.flags,
});
for (msec.subsections.items) |msub| {
try writer.print(" {f}\n", .{msub.fmt(elf_file)});
}
}
};
pub const Index = u32;
};
pub const Subsection = struct {
value: i64 = 0,
merge_section_index: Section.Index = 0,
string_index: u32 = 0,
size: u32 = 0,
alignment: Atom.Alignment = .@"1",
entsize: u32 = 0,
alive: bool = false,
pub fn address(msub: Subsection, elf_file: *Elf) i64 {
return msub.mergeSection(elf_file).address(elf_file) + msub.value;
}
pub fn mergeSection(msub: Subsection, elf_file: *Elf) *Section {
return elf_file.mergeSection(msub.merge_section_index);
}
pub fn getString(msub: Subsection, elf_file: *Elf) []const u8 {
const msec = msub.mergeSection(elf_file);
return msec.bytes.items[msub.string_index..][0..msub.size];
}
pub fn fmt(msub: Subsection, elf_file: *Elf) std.fmt.Alt(Format, Format.default) {
return .{ .data = .{
.msub = msub,
.elf_file = elf_file,
} };
}
const Format = struct {
msub: Subsection,
elf_file: *Elf,
pub fn default(ctx: Format, writer: *std.Io.Writer) std.Io.Writer.Error!void {
const msub = ctx.msub;
const elf_file = ctx.elf_file;
try writer.print("@{x} : align({x}) : size({x})", .{
msub.address(elf_file),
msub.alignment,
msub.size,
});
if (!msub.alive) try writer.writeAll(" : [*]");
}
};
pub const Index = u32;
};
pub const InputSection = struct {
merge_section_index: Section.Index = 0,
atom_index: Atom.Index = 0,
offsets: std.ArrayListUnmanaged(u32) = .empty,
subsections: std.ArrayListUnmanaged(Subsection.Index) = .empty,
bytes: std.ArrayListUnmanaged(u8) = .empty,
strings: std.ArrayListUnmanaged(String) = .empty,
pub fn deinit(imsec: *InputSection, allocator: Allocator) void {
imsec.offsets.deinit(allocator);
imsec.subsections.deinit(allocator);
imsec.bytes.deinit(allocator);
imsec.strings.deinit(allocator);
}
pub fn clearAndFree(imsec: *InputSection, allocator: Allocator) void {
imsec.bytes.clearAndFree(allocator);
// TODO: imsec.strings.clearAndFree(allocator);
}
const FindSubsectionResult = struct {
msub_index: Subsection.Index,
offset: u32,
};
pub fn findSubsection(imsec: InputSection, offset: u32) ?FindSubsectionResult {
// TODO: binary search
for (imsec.offsets.items, 0..) |off, index| {
if (offset < off) return .{
.msub_index = imsec.subsections.items[index - 1],
.offset = offset - imsec.offsets.items[index - 1],
};
}
const last = imsec.offsets.items.len - 1;
const last_off = imsec.offsets.items[last];
const last_len = imsec.strings.items[last].len;
if (offset < last_off + last_len) return .{
.msub_index = imsec.subsections.items[last],
.offset = offset - last_off,
};
return null;
}
pub fn insert(imsec: *InputSection, allocator: Allocator, string: []const u8) !void {
const index: u32 = @intCast(imsec.bytes.items.len);
try imsec.bytes.appendSlice(allocator, string);
try imsec.strings.append(allocator, .{ .pos = index, .len = @intCast(string.len) });
}
pub const Index = u32;
};
const String = struct { pos: u32, len: u32 };
const assert = std.debug.assert;
const mem = std.mem;
const std = @import("std");
const Allocator = mem.Allocator;
const Atom = @import("Atom.zig");
const Elf = @import("../Elf.zig");
const Merge = @This();
|