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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
//! Represents a defined symbol.
/// Allocated address value of this symbol.
value: u64 = 0,
/// Offset into the linker's string table.
name_offset: u32 = 0,
/// Index of file where this symbol is defined.
file_index: File.Index = 0,
/// Index of atom containing this symbol.
/// Index of 0 means there is no associated atom with this symbol.
/// Use `atom` to get the pointer to the atom.
atom_index: Atom.Index = 0,
/// Assigned output section index for this atom.
output_section_index: u16 = 0,
/// Index of the source symbol this symbol references.
/// Use `elfSym` to pull the source symbol from the relevant file.
esym_index: Index = 0,
/// Index of the source version symbol this symbol references if any.
/// If the symbol is unversioned it will have either VER_NDX_LOCAL or VER_NDX_GLOBAL.
version_index: elf.Elf64_Versym = elf.VER_NDX_LOCAL,
/// Misc flags for the symbol packaged as packed struct for compression.
flags: Flags = .{},
extra_index: u32 = 0,
pub fn isAbs(symbol: Symbol, elf_file: *Elf) bool {
const file_ptr = symbol.file(elf_file).?;
// if (file_ptr == .shared) return symbol.sourceSymbol(elf_file).st_shndx == elf.SHN_ABS;
return !symbol.flags.import and symbol.atom(elf_file) == null and symbol.output_section_index == 0 and
file_ptr != .linker_defined;
}
pub fn isLocal(symbol: Symbol) bool {
return !(symbol.flags.import or symbol.flags.@"export");
}
pub fn isIFunc(symbol: Symbol, elf_file: *Elf) bool {
return symbol.type(elf_file) == elf.STT_GNU_IFUNC;
}
pub fn @"type"(symbol: Symbol, elf_file: *Elf) u4 {
const s_sym = symbol.elfSym(elf_file);
// const file_ptr = symbol.file(elf_file).?;
// if (s_sym.st_type() == elf.STT_GNU_IFUNC and file_ptr == .shared) return elf.STT_FUNC;
return s_sym.st_type();
}
pub fn name(symbol: Symbol, elf_file: *Elf) [:0]const u8 {
return elf_file.strtab.getAssumeExists(symbol.name_offset);
}
pub fn atom(symbol: Symbol, elf_file: *Elf) ?*Atom {
return elf_file.atom(symbol.atom_index);
}
pub fn file(symbol: Symbol, elf_file: *Elf) ?File {
return elf_file.file(symbol.file_index);
}
pub fn elfSym(symbol: Symbol, elf_file: *Elf) elf.Elf64_Sym {
const file_ptr = symbol.file(elf_file).?;
switch (file_ptr) {
.zig_module => |x| return x.elfSym(symbol.esym_index).*,
.linker_defined => |x| return x.symtab.items[symbol.esym_index],
.object => |x| return x.symtab[symbol.esym_index],
}
}
pub fn symbolRank(symbol: Symbol, elf_file: *Elf) u32 {
const file_ptr = symbol.file(elf_file) orelse return std.math.maxInt(u32);
const sym = symbol.elfSym(elf_file);
const in_archive = switch (file_ptr) {
.object => |x| !x.alive,
else => false,
};
return file_ptr.symbolRank(sym, in_archive);
}
pub fn address(symbol: Symbol, opts: struct {
plt: bool = true,
}, elf_file: *Elf) u64 {
_ = elf_file;
_ = opts;
// if (symbol.flags.copy_rel) {
// return elf_file.sectionAddress(elf_file.copy_rel_sect_index.?) + symbol.value;
// }
// if (symbol.flags.plt and opts.plt) {
// const extra = symbol.getExtra(elf_file).?;
// if (!symbol.flags.is_canonical and symbol.flags.got) {
// // We have a non-lazy bound function pointer, use that!
// return elf_file.getPltGotEntryAddress(extra.plt_got);
// }
// // Lazy-bound function it is!
// return elf_file.getPltEntryAddress(extra.plt);
// }
return symbol.value;
}
pub fn gotAddress(symbol: Symbol, elf_file: *Elf) u64 {
if (!symbol.flags.has_got) return 0;
const extras = symbol.extra(elf_file).?;
const entry = elf_file.got.entries.items[extras.got];
return entry.address(elf_file);
}
const GetOrCreateGotEntryResult = struct {
found_existing: bool,
index: GotSection.Index,
};
pub fn getOrCreateGotEntry(symbol: *Symbol, symbol_index: Index, elf_file: *Elf) !GetOrCreateGotEntryResult {
assert(symbol.flags.needs_got);
if (symbol.flags.has_got) return .{ .found_existing = true, .index = symbol.extra(elf_file).?.got };
const index = try elf_file.got.addGotSymbol(symbol_index, elf_file);
symbol.flags.has_got = true;
return .{ .found_existing = false, .index = index };
}
// pub fn tlsGdAddress(symbol: Symbol, elf_file: *Elf) u64 {
// if (!symbol.flags.tlsgd) return 0;
// const extra = symbol.getExtra(elf_file).?;
// return elf_file.getGotEntryAddress(extra.tlsgd);
// }
// pub fn gotTpAddress(symbol: Symbol, elf_file: *Elf) u64 {
// if (!symbol.flags.gottp) return 0;
// const extra = symbol.getExtra(elf_file).?;
// return elf_file.getGotEntryAddress(extra.gottp);
// }
// pub fn tlsDescAddress(symbol: Symbol, elf_file: *Elf) u64 {
// if (!symbol.flags.tlsdesc) return 0;
// const extra = symbol.getExtra(elf_file).?;
// return elf_file.getGotEntryAddress(extra.tlsdesc);
// }
// pub fn alignment(symbol: Symbol, elf_file: *Elf) !u64 {
// const file = symbol.getFile(elf_file) orelse return 0;
// const shared = file.shared;
// const s_sym = symbol.getSourceSymbol(elf_file);
// const shdr = shared.getShdrs()[s_sym.st_shndx];
// const alignment = @max(1, shdr.sh_addralign);
// return if (s_sym.st_value == 0)
// alignment
// else
// @min(alignment, try std.math.powi(u64, 2, @ctz(s_sym.st_value)));
// }
pub fn addExtra(symbol: *Symbol, extras: Extra, elf_file: *Elf) !void {
symbol.extra_index = try elf_file.addSymbolExtra(extras);
}
pub fn extra(symbol: Symbol, elf_file: *Elf) ?Extra {
return elf_file.symbolExtra(symbol.extra_index);
}
pub fn setExtra(symbol: Symbol, extras: Extra, elf_file: *Elf) void {
elf_file.setSymbolExtra(symbol.extra_index, extras);
}
pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void {
const file_ptr = symbol.file(elf_file) orelse {
out.* = Elf.null_sym;
return;
};
const esym = symbol.elfSym(elf_file);
const st_type = symbol.type(elf_file);
const st_bind: u8 = blk: {
if (symbol.isLocal()) break :blk 0;
if (symbol.flags.weak) break :blk elf.STB_WEAK;
// if (file_ptr == .shared) break :blk elf.STB_GLOBAL;
break :blk esym.st_bind();
};
const st_shndx = blk: {
// if (symbol.flags.copy_rel) break :blk elf_file.copy_rel_sect_index.?;
// if (file_ptr == .shared or s_sym.st_shndx == elf.SHN_UNDEF) break :blk elf.SHN_UNDEF;
if (symbol.atom(elf_file) == null and file_ptr != .linker_defined)
break :blk elf.SHN_ABS;
break :blk symbol.output_section_index;
};
const st_value = blk: {
// if (symbol.flags.copy_rel) break :blk symbol.address(.{}, elf_file);
// if (file_ptr == .shared or s_sym.st_shndx == elf.SHN_UNDEF) {
// if (symbol.flags.is_canonical) break :blk symbol.address(.{}, elf_file);
// break :blk 0;
// }
// if (st_shndx == elf.SHN_ABS) break :blk symbol.value;
// const shdr = &elf_file.sections.items(.shdr)[st_shndx];
// if (Elf.shdrIsTls(shdr)) break :blk symbol.value - elf_file.getTlsAddress();
break :blk symbol.value;
};
out.* = .{
.st_name = symbol.name_offset,
.st_info = (st_bind << 4) | st_type,
.st_other = esym.st_other,
.st_shndx = st_shndx,
.st_value = st_value,
.st_size = esym.st_size,
};
}
pub fn format(
symbol: Symbol,
comptime unused_fmt_string: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = symbol;
_ = unused_fmt_string;
_ = options;
_ = writer;
@compileError("do not format symbols directly");
}
const FormatContext = struct {
symbol: Symbol,
elf_file: *Elf,
};
pub fn fmtName(symbol: Symbol, elf_file: *Elf) std.fmt.Formatter(formatName) {
return .{ .data = .{
.symbol = symbol,
.elf_file = elf_file,
} };
}
fn formatName(
ctx: FormatContext,
comptime unused_fmt_string: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = options;
_ = unused_fmt_string;
const elf_file = ctx.elf_file;
const symbol = ctx.symbol;
try writer.writeAll(symbol.name(elf_file));
switch (symbol.version_index & elf.VERSYM_VERSION) {
elf.VER_NDX_LOCAL, elf.VER_NDX_GLOBAL => {},
else => {
unreachable;
// const shared = symbol.getFile(elf_file).?.shared;
// try writer.print("@{s}", .{shared.getVersionString(symbol.version_index)});
},
}
}
pub fn fmt(symbol: Symbol, elf_file: *Elf) std.fmt.Formatter(format2) {
return .{ .data = .{
.symbol = symbol,
.elf_file = elf_file,
} };
}
fn format2(
ctx: FormatContext,
comptime unused_fmt_string: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = options;
_ = unused_fmt_string;
const symbol = ctx.symbol;
try writer.print("%{d} : {s} : @{x}", .{ symbol.esym_index, symbol.fmtName(ctx.elf_file), symbol.value });
if (symbol.file(ctx.elf_file)) |file_ptr| {
if (symbol.isAbs(ctx.elf_file)) {
if (symbol.elfSym(ctx.elf_file).st_shndx == elf.SHN_UNDEF) {
try writer.writeAll(" : undef");
} else {
try writer.writeAll(" : absolute");
}
} else if (symbol.output_section_index != 0) {
try writer.print(" : sect({d})", .{symbol.output_section_index});
}
if (symbol.atom(ctx.elf_file)) |atom_ptr| {
try writer.print(" : atom({d})", .{atom_ptr.atom_index});
}
var buf: [2]u8 = .{'_'} ** 2;
if (symbol.flags.@"export") buf[0] = 'E';
if (symbol.flags.import) buf[1] = 'I';
try writer.print(" : {s}", .{&buf});
if (symbol.flags.weak) try writer.writeAll(" : weak");
switch (file_ptr) {
inline else => |x| try writer.print(" : {s}({d})", .{ @tagName(file_ptr), x.index }),
}
} else try writer.writeAll(" : unresolved");
}
pub const Flags = packed struct {
/// Whether the symbol is imported at runtime.
import: bool = false,
/// Whether the symbol is exported at runtime.
@"export": bool = false,
/// Whether this symbol is weak.
weak: bool = false,
/// Whether the symbol makes into the output symtab or not.
output_symtab: bool = false,
/// Whether the symbol contains GOT indirection.
needs_got: bool = false,
has_got: bool = false,
/// Whether the symbol contains PLT indirection.
needs_plt: bool = false,
plt: bool = false,
/// Whether the PLT entry is canonical.
is_canonical: bool = false,
/// Whether the symbol contains COPYREL directive.
copy_rel: bool = false,
has_copy_rel: bool = false,
has_dynamic: bool = false,
/// Whether the symbol contains TLSGD indirection.
tlsgd: bool = false,
/// Whether the symbol contains GOTTP indirection.
gottp: bool = false,
/// Whether the symbol contains TLSDESC indirection.
tlsdesc: bool = false,
};
pub const Extra = struct {
got: u32 = 0,
plt: u32 = 0,
plt_got: u32 = 0,
dynamic: u32 = 0,
copy_rel: u32 = 0,
tlsgd: u32 = 0,
gottp: u32 = 0,
tlsdesc: u32 = 0,
};
pub const Index = u32;
const assert = std.debug.assert;
const elf = std.elf;
const std = @import("std");
const synthetic_sections = @import("synthetic_sections.zig");
const Atom = @import("Atom.zig");
const Elf = @import("../Elf.zig");
const File = @import("file.zig").File;
const GotSection = synthetic_sections.GotSection;
const LinkerDefined = @import("LinkerDefined.zig");
// const Object = @import("Object.zig");
// const SharedObject = @import("SharedObject.zig");
const Symbol = @This();
const ZigModule = @import("ZigModule.zig");
|