mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
linker: fix some allocator references
This commit is contained in:
parent
48d5861f92
commit
4629708787
@ -20,7 +20,8 @@ pub fn deinit(self: *Archive, allocator: Allocator) void {
|
||||
}
|
||||
|
||||
pub fn parse(self: *Archive, elf_file: *Elf) !void {
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
|
||||
var stream = std.io.fixedBufferStream(self.data);
|
||||
const reader = stream.reader();
|
||||
@ -150,7 +151,8 @@ pub const ArSymtab = struct {
|
||||
const hdr = setArHdr(.{ .name = .symtab, .size = @intCast(ar.size(.p64)) });
|
||||
try writer.writeAll(mem.asBytes(&hdr));
|
||||
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
var offsets = std.AutoHashMap(File.Index, u64).init(gpa);
|
||||
defer offsets.deinit();
|
||||
try offsets.ensureUnusedCapacity(@intCast(elf_file.objects.items.len + 1));
|
||||
|
||||
@ -14,7 +14,8 @@ pub const Error = error{
|
||||
};
|
||||
|
||||
pub fn parse(scr: *LdScript, data: []const u8, elf_file: *Elf) Error!void {
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
var tokenizer = Tokenizer{ .source = data };
|
||||
var tokens = std.ArrayList(Token).init(gpa);
|
||||
defer tokens.deinit();
|
||||
|
||||
@ -12,7 +12,8 @@ pub fn deinit(self: *LinkerDefined, allocator: Allocator) void {
|
||||
}
|
||||
|
||||
pub fn addGlobal(self: *LinkerDefined, name: [:0]const u8, elf_file: *Elf) !u32 {
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
try self.symtab.ensureUnusedCapacity(gpa, 1);
|
||||
try self.symbols.ensureUnusedCapacity(gpa, 1);
|
||||
const name_off = @as(u32, @intCast(self.strtab.items.len));
|
||||
|
||||
@ -66,7 +66,8 @@ pub fn parse(self: *Object, elf_file: *Elf) !void {
|
||||
|
||||
if (self.header.?.e_shnum == 0) return;
|
||||
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
|
||||
if (self.data.len < self.header.?.e_shoff or
|
||||
self.data.len < self.header.?.e_shoff + @as(u64, @intCast(self.header.?.e_shnum)) * @sizeOf(elf.Elf64_Shdr))
|
||||
@ -149,8 +150,10 @@ pub fn init(self: *Object, elf_file: *Elf) !void {
|
||||
}
|
||||
|
||||
fn initAtoms(self: *Object, elf_file: *Elf) !void {
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
const shdrs = self.shdrs.items;
|
||||
try self.atoms.resize(elf_file.base.allocator, shdrs.len);
|
||||
try self.atoms.resize(gpa, shdrs.len);
|
||||
@memset(self.atoms.items, 0);
|
||||
|
||||
for (shdrs, 0..) |shdr, i| {
|
||||
@ -185,7 +188,6 @@ fn initAtoms(self: *Object, elf_file: *Elf) !void {
|
||||
continue;
|
||||
}
|
||||
|
||||
const gpa = elf_file.base.allocator;
|
||||
const gop = try elf_file.getOrCreateComdatGroupOwner(group_signature);
|
||||
const comdat_group_index = try elf_file.addComdatGroup();
|
||||
const comdat_group = elf_file.comdatGroup(comdat_group_index);
|
||||
@ -308,7 +310,8 @@ fn skipShdr(self: *Object, index: u16, elf_file: *Elf) bool {
|
||||
}
|
||||
|
||||
fn initSymtab(self: *Object, elf_file: *Elf) !void {
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
const first_global = self.first_global orelse self.symtab.items.len;
|
||||
|
||||
try self.symbols.ensureTotalCapacityPrecise(gpa, self.symtab.items.len);
|
||||
@ -340,7 +343,8 @@ fn parseEhFrame(self: *Object, shndx: u16, elf_file: *Elf) !void {
|
||||
return;
|
||||
};
|
||||
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
const raw = self.shdrContents(shndx);
|
||||
const relocs = self.getRelocs(relocs_shndx);
|
||||
const fdes_start = self.fdes.items.len;
|
||||
@ -440,6 +444,8 @@ fn filterRelocs(
|
||||
}
|
||||
|
||||
pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void {
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
for (self.atoms.items) |atom_index| {
|
||||
const atom = elf_file.atom(atom_index) orelse continue;
|
||||
if (!atom.flags.alive) continue;
|
||||
@ -450,7 +456,7 @@ pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void {
|
||||
// TODO ideally, we don't have to decompress at this stage (should already be done)
|
||||
// and we just fetch the code slice.
|
||||
const code = try self.codeDecompressAlloc(elf_file, atom_index);
|
||||
defer elf_file.base.allocator.free(code);
|
||||
defer gpa.free(code);
|
||||
try atom.scanRelocs(elf_file, code, undefs);
|
||||
} else try atom.scanRelocs(elf_file, null, undefs);
|
||||
}
|
||||
@ -623,7 +629,8 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void {
|
||||
continue;
|
||||
}
|
||||
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
|
||||
const atom_index = try elf_file.addAtom();
|
||||
try self.atoms.append(gpa, atom_index);
|
||||
@ -682,7 +689,8 @@ pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void {
|
||||
const shdr = atom.inputShdr(elf_file);
|
||||
atom.output_section_index = self.initOutputSection(elf_file, shdr) catch unreachable;
|
||||
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
const gop = try elf_file.output_sections.getOrPut(gpa, atom.output_section_index);
|
||||
if (!gop.found_existing) gop.value_ptr.* = .{};
|
||||
try gop.value_ptr.append(gpa, atom_index);
|
||||
@ -742,7 +750,8 @@ pub fn addAtomsToRelaSections(self: Object, elf_file: *Elf) !void {
|
||||
shdr.sh_info = atom.outputShndx().?;
|
||||
shdr.sh_link = elf_file.symtab_section_index.?;
|
||||
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
const gop = try elf_file.output_rela_sections.getOrPut(gpa, atom.outputShndx().?);
|
||||
if (!gop.found_existing) gop.value_ptr.* = .{ .shndx = shndx };
|
||||
try gop.value_ptr.atom_list.append(gpa, atom_index);
|
||||
@ -750,7 +759,8 @@ pub fn addAtomsToRelaSections(self: Object, elf_file: *Elf) !void {
|
||||
}
|
||||
|
||||
pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, elf_file: *Elf) !void {
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
const start = self.first_global orelse self.symtab.items.len;
|
||||
|
||||
try ar_symtab.symtab.ensureUnusedCapacity(gpa, self.symtab.items.len - start);
|
||||
@ -857,7 +867,8 @@ pub fn shdrContents(self: Object, index: u32) []const u8 {
|
||||
/// Returns atom's code and optionally uncompresses data if required (for compressed sections).
|
||||
/// Caller owns the memory.
|
||||
pub fn codeDecompressAlloc(self: Object, elf_file: *Elf, atom_index: Atom.Index) ![]u8 {
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
const atom_ptr = elf_file.atom(atom_index).?;
|
||||
assert(atom_ptr.file_index == self.index);
|
||||
const data = self.shdrContents(atom_ptr.input_section_index);
|
||||
|
||||
@ -47,7 +47,8 @@ pub fn deinit(self: *SharedObject, allocator: Allocator) void {
|
||||
}
|
||||
|
||||
pub fn parse(self: *SharedObject, elf_file: *Elf) !void {
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
var stream = std.io.fixedBufferStream(self.data);
|
||||
const reader = stream.reader();
|
||||
|
||||
@ -101,7 +102,8 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void {
|
||||
}
|
||||
|
||||
fn parseVersions(self: *SharedObject, elf_file: *Elf) !void {
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
const symtab = self.getSymtabRaw();
|
||||
|
||||
try self.verstrings.resize(gpa, 2);
|
||||
@ -146,7 +148,8 @@ fn parseVersions(self: *SharedObject, elf_file: *Elf) !void {
|
||||
}
|
||||
|
||||
pub fn init(self: *SharedObject, elf_file: *Elf) !void {
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
const symtab = self.getSymtabRaw();
|
||||
const strtab = self.getStrtabRaw();
|
||||
|
||||
@ -295,7 +298,8 @@ pub fn initSymbolAliases(self: *SharedObject, elf_file: *Elf) !void {
|
||||
}
|
||||
};
|
||||
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
var aliases = std.ArrayList(Symbol.Index).init(gpa);
|
||||
defer aliases.deinit();
|
||||
try aliases.ensureTotalCapacityPrecise(self.globals().len);
|
||||
|
||||
@ -233,9 +233,12 @@ pub const Iterator = struct {
|
||||
};
|
||||
|
||||
pub fn calcEhFrameSize(elf_file: *Elf) !usize {
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
|
||||
var offset: usize = 0;
|
||||
|
||||
var cies = std.ArrayList(Cie).init(elf_file.base.allocator);
|
||||
var cies = std.ArrayList(Cie).init(gpa);
|
||||
defer cies.deinit();
|
||||
|
||||
for (elf_file.objects.items) |index| {
|
||||
@ -327,7 +330,8 @@ fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file:
|
||||
}
|
||||
|
||||
pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void {
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
|
||||
relocs_log.debug("{x}: .eh_frame", .{elf_file.shdrs.items[elf_file.eh_frame_section_index.?].sh_addr});
|
||||
|
||||
@ -378,7 +382,8 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void {
|
||||
}
|
||||
|
||||
pub fn writeEhFrameObject(elf_file: *Elf, writer: anytype) !void {
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
|
||||
for (elf_file.objects.items) |index| {
|
||||
const object = elf_file.file(index).?.object;
|
||||
@ -467,6 +472,9 @@ pub fn writeEhFrameRelocs(elf_file: *Elf, writer: anytype) !void {
|
||||
}
|
||||
|
||||
pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
|
||||
try writer.writeByte(1); // version
|
||||
try writer.writeByte(EH_PE.pcrel | EH_PE.sdata4);
|
||||
try writer.writeByte(EH_PE.udata4);
|
||||
@ -495,7 +503,7 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
|
||||
}
|
||||
};
|
||||
|
||||
var entries = std.ArrayList(Entry).init(elf_file.base.allocator);
|
||||
var entries = std.ArrayList(Entry).init(gpa);
|
||||
defer entries.deinit();
|
||||
try entries.ensureTotalCapacityPrecise(num_fdes);
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
pub fn gcAtoms(elf_file: *Elf) !void {
|
||||
const gpa = elf_file.base.allocator;
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
const num_files = elf_file.objects.items.len + @intFromBool(elf_file.zig_object_index != null);
|
||||
var files = try std.ArrayList(File.Index).initCapacity(gpa, num_files);
|
||||
defer files.deinit();
|
||||
|
||||
@ -57,7 +57,7 @@ pub const DynamicSection = struct {
|
||||
if (elf_file.z_now) {
|
||||
flags_1 |= elf.DF_1_NOW;
|
||||
}
|
||||
if (elf_file.isExe() and comp.config.pie) {
|
||||
if (elf_file.base.isExe() and comp.config.pie) {
|
||||
flags_1 |= elf.DF_1_PIE;
|
||||
}
|
||||
// if (elf_file.z_nodlopen) {
|
||||
@ -89,7 +89,7 @@ pub const DynamicSection = struct {
|
||||
if (elf_file.verneed_section_index != null) nentries += 2; // VERNEED
|
||||
if (dt.getFlags(elf_file) != null) nentries += 1; // FLAGS
|
||||
if (dt.getFlags1(elf_file) != null) nentries += 1; // FLAGS_1
|
||||
if (!elf_file.isDynLib()) nentries += 1; // DEBUG
|
||||
if (!elf_file.base.isDynLib()) nentries += 1; // DEBUG
|
||||
nentries += 1; // NULL
|
||||
return nentries * @sizeOf(elf.Elf64_Dyn);
|
||||
}
|
||||
@ -216,7 +216,7 @@ pub const DynamicSection = struct {
|
||||
}
|
||||
|
||||
// DEBUG
|
||||
if (!elf_file.isDynLib()) try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_DEBUG, .d_val = 0 });
|
||||
if (!elf_file.base.isDynLib()) try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_DEBUG, .d_val = 0 });
|
||||
|
||||
// NULL
|
||||
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_NULL, .d_val = 0 });
|
||||
@ -256,7 +256,7 @@ pub const ZigGotSection = struct {
|
||||
entry.* = sym_index;
|
||||
const symbol = elf_file.symbol(sym_index);
|
||||
symbol.flags.has_zig_got = true;
|
||||
if (elf_file.isDynLib() or (elf_file.isExe() and comp.config.pie)) {
|
||||
if (elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) {
|
||||
zig_got.flags.needs_rela = true;
|
||||
}
|
||||
if (symbol.extra(elf_file)) |extra| {
|
||||
@ -494,7 +494,7 @@ pub const GotSection = struct {
|
||||
const symbol = elf_file.symbol(sym_index);
|
||||
symbol.flags.has_got = true;
|
||||
if (symbol.flags.import or symbol.isIFunc(elf_file) or
|
||||
((elf_file.isDynLib() or (elf_file.isExe() and comp.config.pie)) and !symbol.isAbs(elf_file)))
|
||||
((elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) and !symbol.isAbs(elf_file)))
|
||||
{
|
||||
got.flags.needs_rela = true;
|
||||
}
|
||||
@ -527,7 +527,7 @@ pub const GotSection = struct {
|
||||
entry.symbol_index = sym_index;
|
||||
const symbol = elf_file.symbol(sym_index);
|
||||
symbol.flags.has_tlsgd = true;
|
||||
if (symbol.flags.import or elf_file.isDynLib()) got.flags.needs_rela = true;
|
||||
if (symbol.flags.import or elf_file.base.isDynLib()) got.flags.needs_rela = true;
|
||||
if (symbol.extra(elf_file)) |extra| {
|
||||
var new_extra = extra;
|
||||
new_extra.tlsgd = index;
|
||||
@ -544,7 +544,7 @@ pub const GotSection = struct {
|
||||
entry.symbol_index = sym_index;
|
||||
const symbol = elf_file.symbol(sym_index);
|
||||
symbol.flags.has_gottp = true;
|
||||
if (symbol.flags.import or elf_file.isDynLib()) got.flags.needs_rela = true;
|
||||
if (symbol.flags.import or elf_file.base.isDynLib()) got.flags.needs_rela = true;
|
||||
if (symbol.extra(elf_file)) |extra| {
|
||||
var new_extra = extra;
|
||||
new_extra.gottp = index;
|
||||
@ -579,7 +579,7 @@ pub const GotSection = struct {
|
||||
|
||||
pub fn write(got: GotSection, elf_file: *Elf, writer: anytype) !void {
|
||||
const comp = elf_file.base.comp;
|
||||
const is_dyn_lib = elf_file.isDynLib();
|
||||
const is_dyn_lib = elf_file.base.isDynLib();
|
||||
const apply_relocs = true; // TODO add user option for this
|
||||
|
||||
for (got.entries.items) |entry| {
|
||||
@ -594,7 +594,7 @@ pub const GotSection = struct {
|
||||
if (symbol.?.flags.import) break :blk 0;
|
||||
if (symbol.?.isIFunc(elf_file))
|
||||
break :blk if (apply_relocs) value else 0;
|
||||
if ((elf_file.isDynLib() or (elf_file.isExe() and comp.config.pie)) and
|
||||
if ((elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) and
|
||||
!symbol.?.isAbs(elf_file))
|
||||
{
|
||||
break :blk if (apply_relocs) value else 0;
|
||||
@ -643,7 +643,7 @@ pub const GotSection = struct {
|
||||
pub fn addRela(got: GotSection, elf_file: *Elf) !void {
|
||||
const comp = elf_file.base.comp;
|
||||
const gpa = comp.gpa;
|
||||
const is_dyn_lib = elf_file.isDynLib();
|
||||
const is_dyn_lib = elf_file.base.isDynLib();
|
||||
try elf_file.rela_dyn.ensureUnusedCapacity(gpa, got.numRela(elf_file));
|
||||
|
||||
for (got.entries.items) |entry| {
|
||||
@ -672,7 +672,7 @@ pub const GotSection = struct {
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if ((elf_file.isDynLib() or (elf_file.isExe() and comp.config.pie)) and
|
||||
if ((elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) and
|
||||
!symbol.?.isAbs(elf_file))
|
||||
{
|
||||
elf_file.addRelaDynAssumeCapacity(.{
|
||||
@ -746,7 +746,7 @@ pub const GotSection = struct {
|
||||
|
||||
pub fn numRela(got: GotSection, elf_file: *Elf) usize {
|
||||
const comp = elf_file.base.comp;
|
||||
const is_dyn_lib = elf_file.isDynLib();
|
||||
const is_dyn_lib = elf_file.base.isDynLib();
|
||||
var num: usize = 0;
|
||||
for (got.entries.items) |entry| {
|
||||
const symbol = switch (entry.tag) {
|
||||
@ -755,7 +755,7 @@ pub const GotSection = struct {
|
||||
};
|
||||
switch (entry.tag) {
|
||||
.got => if (symbol.?.flags.import or symbol.?.isIFunc(elf_file) or
|
||||
((elf_file.isDynLib() or (elf_file.isExe() and comp.config.pie)) and
|
||||
((elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) and
|
||||
!symbol.?.isAbs(elf_file)))
|
||||
{
|
||||
num += 1;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user