Merge pull request #21666 from ziglang/reduce-flush

link.Elf: fix phdr_gnu_stack_index not included in sortPhdrs
This commit is contained in:
Andrew Kelley 2024-10-11 10:34:17 -07:00 committed by GitHub
commit 5e53203e82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 515 additions and 446 deletions

View File

@ -603,18 +603,18 @@ set(ZIG_STAGE2_SOURCES
src/link/Elf/AtomList.zig
src/link/Elf/LdScript.zig
src/link/Elf/LinkerDefined.zig
src/link/Elf/Merge.zig
src/link/Elf/Object.zig
src/link/Elf/SharedObject.zig
src/link/Elf/Symbol.zig
src/link/Elf/Thunk.zig
src/link/Elf/ZigObject.zig
src/link/Elf/eh_frame.zig
src/link/Elf/file.zig
src/link/Elf/gc.zig
src/link/Elf/merge_section.zig
src/link/Elf/relocatable.zig
src/link/Elf/relocation.zig
src/link/Elf/synthetic_sections.zig
src/link/Elf/Thunk.zig
src/link/MachO.zig
src/link/MachO/Archive.zig
src/link/MachO/Atom.zig

View File

@ -23,6 +23,12 @@ pub fn MultiArrayList(comptime T: type) type {
len: usize = 0,
capacity: usize = 0,
pub const empty: Self = .{
.bytes = undefined,
.len = 0,
.capacity = 0,
};
const Elem = switch (@typeInfo(T)) {
.@"struct" => T,
.@"union" => |u| struct {
@ -474,7 +480,7 @@ pub fn MultiArrayList(comptime T: type) type {
pub fn swap(sc: @This(), a_index: usize, b_index: usize) void {
inline for (fields, 0..) |field_info, i| {
if (@sizeOf(field_info.type) != 0) {
const field = @as(Field, @enumFromInt(i));
const field: Field = @enumFromInt(i);
const ptr = sc.slice.items(field);
mem.swap(field_info.type, &ptr[a_index], &ptr[b_index]);
}

File diff suppressed because it is too large Load Diff

View File

@ -205,7 +205,7 @@ pub fn allocateSymbols(self: *LinkerDefined, elf_file: *Elf) void {
}.allocSymbol;
// _DYNAMIC
if (elf_file.dynamic_section_index) |shndx| {
if (elf_file.section_indexes.dynamic) |shndx| {
const shdr = shdrs[shndx];
allocSymbol(self, self.dynamic_index.?, shdr.sh_addr, shndx, elf_file);
}
@ -236,19 +236,19 @@ pub fn allocateSymbols(self: *LinkerDefined, elf_file: *Elf) void {
// _GLOBAL_OFFSET_TABLE_
if (elf_file.getTarget().cpu.arch == .x86_64) {
if (elf_file.got_plt_section_index) |shndx| {
if (elf_file.section_indexes.got_plt) |shndx| {
const shdr = shdrs[shndx];
allocSymbol(self, self.got_index.?, shdr.sh_addr, shndx, elf_file);
}
} else {
if (elf_file.got_section_index) |shndx| {
if (elf_file.section_indexes.got) |shndx| {
const shdr = shdrs[shndx];
allocSymbol(self, self.got_index.?, shdr.sh_addr, shndx, elf_file);
}
}
// _PROCEDURE_LINKAGE_TABLE_
if (elf_file.plt_section_index) |shndx| {
if (elf_file.section_indexes.plt) |shndx| {
const shdr = shdrs[shndx];
allocSymbol(self, self.plt_index.?, shdr.sh_addr, shndx, elf_file);
}
@ -262,13 +262,13 @@ pub fn allocateSymbols(self: *LinkerDefined, elf_file: *Elf) void {
}
// __GNU_EH_FRAME_HDR
if (elf_file.eh_frame_hdr_section_index) |shndx| {
if (elf_file.section_indexes.eh_frame_hdr) |shndx| {
const shdr = shdrs[shndx];
allocSymbol(self, self.gnu_eh_frame_hdr_index.?, shdr.sh_addr, shndx, elf_file);
}
// __rela_iplt_start, __rela_iplt_end
if (elf_file.rela_dyn_section_index) |shndx| blk: {
if (elf_file.section_indexes.rela_dyn) |shndx| blk: {
if (link_mode != .static or comp.config.pie) break :blk;
const shdr = shdrs[shndx];
const end_addr = shdr.sh_addr + shdr.sh_size;

View File

@ -1,4 +1,4 @@
pub const MergeSection = struct {
pub const Section = struct {
value: u64 = 0,
size: u64 = 0,
alignment: Atom.Alignment = .@"1",
@ -10,25 +10,25 @@ pub const MergeSection = struct {
bytes: std.ArrayListUnmanaged(u8) = .empty,
table: std.HashMapUnmanaged(
String,
MergeSubsection.Index,
Subsection.Index,
IndexContext,
std.hash_map.default_max_load_percentage,
) = .{},
subsections: std.ArrayListUnmanaged(MergeSubsection) = .empty,
finalized_subsections: std.ArrayListUnmanaged(MergeSubsection.Index) = .empty,
subsections: std.ArrayListUnmanaged(Subsection) = .empty,
finalized_subsections: std.ArrayListUnmanaged(Subsection.Index) = .empty,
pub fn deinit(msec: *MergeSection, allocator: Allocator) void {
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: MergeSection, elf_file: *Elf) [:0]const u8 {
pub fn name(msec: Section, elf_file: *Elf) [:0]const u8 {
return elf_file.getShString(msec.name_offset);
}
pub fn address(msec: MergeSection, elf_file: *Elf) i64 {
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);
}
@ -36,10 +36,10 @@ pub const MergeSection = struct {
const InsertResult = struct {
found_existing: bool,
key: String,
sub: *MergeSubsection.Index,
sub: *Subsection.Index,
};
pub fn insert(msec: *MergeSection, allocator: Allocator, string: []const u8) !InsertResult {
pub fn insert(msec: *Section, allocator: Allocator, string: []const u8) !InsertResult {
const gop = try msec.table.getOrPutContextAdapted(
allocator,
string,
@ -54,7 +54,7 @@ pub const MergeSection = struct {
return .{ .found_existing = gop.found_existing, .key = gop.key_ptr.*, .sub = gop.value_ptr };
}
pub fn insertZ(msec: *MergeSection, allocator: Allocator, string: []const u8) !InsertResult {
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);
@ -64,7 +64,7 @@ pub const MergeSection = struct {
/// Finalizes the merge section and clears hash table.
/// Sorts all owned subsections.
pub fn finalize(msec: *MergeSection, allocator: Allocator) !void {
pub fn finalize(msec: *Section, allocator: Allocator) !void {
try msec.finalized_subsections.ensureTotalCapacityPrecise(allocator, msec.subsections.items.len);
var it = msec.table.iterator();
@ -76,7 +76,7 @@ pub const MergeSection = struct {
msec.table.clearAndFree(allocator);
const sortFn = struct {
pub fn sortFn(ctx: *MergeSection, lhs: MergeSubsection.Index, rhs: MergeSubsection.Index) bool {
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)) {
@ -91,10 +91,10 @@ pub const MergeSection = struct {
}
}.sortFn;
std.mem.sort(MergeSubsection.Index, msec.finalized_subsections.items, msec, sortFn);
std.mem.sort(Subsection.Index, msec.finalized_subsections.items, msec, sortFn);
}
pub fn updateSize(msec: *MergeSection) void {
pub fn updateSize(msec: *Section) void {
// TODO a 'stale' flag would be better here perhaps?
msec.size = 0;
msec.alignment = .@"1";
@ -111,7 +111,7 @@ pub const MergeSection = struct {
}
}
pub fn initOutputSection(msec: *MergeSection, elf_file: *Elf) !void {
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,
@ -119,14 +119,14 @@ pub const MergeSection = struct {
});
}
pub fn addMergeSubsection(msec: *MergeSection, allocator: Allocator) !MergeSubsection.Index {
const index: MergeSubsection.Index = @intCast(msec.subsections.items.len);
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: *MergeSection, index: MergeSubsection.Index) *MergeSubsection {
pub fn mergeSubsection(msec: *Section, index: Subsection.Index) *Subsection {
assert(index < msec.subsections.items.len);
return &msec.subsections.items[index];
}
@ -158,7 +158,7 @@ pub const MergeSection = struct {
};
pub fn format(
msec: MergeSection,
msec: Section,
comptime unused_fmt_string: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
@ -167,10 +167,10 @@ pub const MergeSection = struct {
_ = unused_fmt_string;
_ = options;
_ = writer;
@compileError("do not format MergeSection directly");
@compileError("do not format directly");
}
pub fn fmt(msec: MergeSection, elf_file: *Elf) std.fmt.Formatter(format2) {
pub fn fmt(msec: Section, elf_file: *Elf) std.fmt.Formatter(format2) {
return .{ .data = .{
.msec = msec,
.elf_file = elf_file,
@ -178,7 +178,7 @@ pub const MergeSection = struct {
}
const FormatContext = struct {
msec: MergeSection,
msec: Section,
elf_file: *Elf,
};
@ -209,30 +209,30 @@ pub const MergeSection = struct {
pub const Index = u32;
};
pub const MergeSubsection = struct {
pub const Subsection = struct {
value: i64 = 0,
merge_section_index: MergeSection.Index = 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: MergeSubsection, elf_file: *Elf) i64 {
pub fn address(msub: Subsection, elf_file: *Elf) i64 {
return msub.mergeSection(elf_file).address(elf_file) + msub.value;
}
pub fn mergeSection(msub: MergeSubsection, elf_file: *Elf) *MergeSection {
pub fn mergeSection(msub: Subsection, elf_file: *Elf) *Section {
return elf_file.mergeSection(msub.merge_section_index);
}
pub fn getString(msub: MergeSubsection, elf_file: *Elf) []const u8 {
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 format(
msub: MergeSubsection,
msub: Subsection,
comptime unused_fmt_string: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
@ -241,10 +241,10 @@ pub const MergeSubsection = struct {
_ = unused_fmt_string;
_ = options;
_ = writer;
@compileError("do not format MergeSubsection directly");
@compileError("do not format directly");
}
pub fn fmt(msub: MergeSubsection, elf_file: *Elf) std.fmt.Formatter(format2) {
pub fn fmt(msub: Subsection, elf_file: *Elf) std.fmt.Formatter(format2) {
return .{ .data = .{
.msub = msub,
.elf_file = elf_file,
@ -252,7 +252,7 @@ pub const MergeSubsection = struct {
}
const FormatContext = struct {
msub: MergeSubsection,
msub: Subsection,
elf_file: *Elf,
};
@ -277,32 +277,32 @@ pub const MergeSubsection = struct {
pub const Index = u32;
};
pub const InputMergeSection = struct {
merge_section_index: MergeSection.Index = 0,
pub const InputSection = struct {
merge_section_index: Section.Index = 0,
atom_index: Atom.Index = 0,
offsets: std.ArrayListUnmanaged(u32) = .empty,
subsections: std.ArrayListUnmanaged(MergeSubsection.Index) = .empty,
subsections: std.ArrayListUnmanaged(Subsection.Index) = .empty,
bytes: std.ArrayListUnmanaged(u8) = .empty,
strings: std.ArrayListUnmanaged(String) = .empty,
pub fn deinit(imsec: *InputMergeSection, allocator: Allocator) void {
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: *InputMergeSection, allocator: Allocator) void {
pub fn clearAndFree(imsec: *InputSection, allocator: Allocator) void {
imsec.bytes.clearAndFree(allocator);
// TODO: imsec.strings.clearAndFree(allocator);
}
const FindSubsectionResult = struct {
msub_index: MergeSubsection.Index,
msub_index: Subsection.Index,
offset: u32,
};
pub fn findSubsection(imsec: InputMergeSection, offset: u32) ?FindSubsectionResult {
pub fn findSubsection(imsec: InputSection, offset: u32) ?FindSubsectionResult {
// TODO: binary search
for (imsec.offsets.items, 0..) |off, index| {
if (offset < off) return .{
@ -320,7 +320,7 @@ pub const InputMergeSection = struct {
return null;
}
pub fn insert(imsec: *InputMergeSection, allocator: Allocator, string: []const u8) !void {
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) });
@ -338,3 +338,4 @@ const std = @import("std");
const Allocator = mem.Allocator;
const Atom = @import("Atom.zig");
const Elf = @import("../Elf.zig");
const Merge = @This();

View File

@ -23,8 +23,8 @@ atoms_extra: std.ArrayListUnmanaged(u32) = .empty,
comdat_groups: std.ArrayListUnmanaged(Elf.ComdatGroup) = .empty,
comdat_group_data: std.ArrayListUnmanaged(u32) = .empty,
input_merge_sections: std.ArrayListUnmanaged(InputMergeSection) = .empty,
input_merge_sections_indexes: std.ArrayListUnmanaged(InputMergeSection.Index) = .empty,
input_merge_sections: std.ArrayListUnmanaged(Merge.InputSection) = .empty,
input_merge_sections_indexes: std.ArrayListUnmanaged(Merge.InputSection.Index) = .empty,
fdes: std.ArrayListUnmanaged(Fde) = .empty,
cies: std.ArrayListUnmanaged(Cie) = .empty,
@ -927,7 +927,7 @@ pub fn initRelaSections(self: *Object, elf_file: *Elf) !void {
for (self.atoms_indexes.items) |atom_index| {
const atom_ptr = self.atom(atom_index) orelse continue;
if (!atom_ptr.alive) continue;
if (atom_ptr.output_section_index == elf_file.eh_frame_section_index) continue;
if (atom_ptr.output_section_index == elf_file.section_indexes.eh_frame) continue;
const shndx = atom_ptr.relocsShndx() orelse continue;
const shdr = self.shdrs.items[shndx];
const out_shndx = try elf_file.initOutputSection(.{
@ -947,7 +947,7 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void {
for (self.atoms_indexes.items) |atom_index| {
const atom_ptr = self.atom(atom_index) orelse continue;
if (!atom_ptr.alive) continue;
if (atom_ptr.output_section_index == elf_file.eh_frame_section_index) continue;
if (atom_ptr.output_section_index == elf_file.section_indexes.eh_frame) continue;
const shndx = blk: {
const shndx = atom_ptr.relocsShndx() orelse continue;
const shdr = self.shdrs.items[shndx];
@ -960,7 +960,7 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void {
const slice = elf_file.sections.slice();
const shdr = &slice.items(.shdr)[shndx];
shdr.sh_info = atom_ptr.output_section_index;
shdr.sh_link = elf_file.symtab_section_index.?;
shdr.sh_link = elf_file.section_indexes.symtab.?;
const gpa = elf_file.base.comp.gpa;
const atom_list = &elf_file.sections.items(.atom_list)[shndx];
try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
@ -1305,14 +1305,14 @@ fn setAtomFields(o: *Object, atom_ptr: *Atom, opts: Atom.Extra.AsOptionals) void
o.setAtomExtra(atom_ptr.extra_index, extras);
}
fn addInputMergeSection(self: *Object, allocator: Allocator) !InputMergeSection.Index {
const index: InputMergeSection.Index = @intCast(self.input_merge_sections.items.len);
fn addInputMergeSection(self: *Object, allocator: Allocator) !Merge.InputSection.Index {
const index: Merge.InputSection.Index = @intCast(self.input_merge_sections.items.len);
const msec = try self.input_merge_sections.addOne(allocator);
msec.* = .{};
return index;
}
fn inputMergeSection(self: *Object, index: InputMergeSection.Index) ?*InputMergeSection {
fn inputMergeSection(self: *Object, index: Merge.InputSection.Index) ?*Merge.InputSection {
if (index == 0) return null;
return &self.input_merge_sections.items[index];
}
@ -1522,6 +1522,6 @@ const Cie = eh_frame.Cie;
const Elf = @import("../Elf.zig");
const Fde = eh_frame.Fde;
const File = @import("file.zig").File;
const InputMergeSection = @import("merge_section.zig").InputMergeSection;
const Merge = @import("Merge.zig");
const Symbol = @import("Symbol.zig");
const Alignment = Atom.Alignment;

View File

@ -74,7 +74,7 @@ pub fn atom(symbol: Symbol, elf_file: *Elf) ?*Atom {
return file_ptr.atom(symbol.ref.index);
}
pub fn mergeSubsection(symbol: Symbol, elf_file: *Elf) ?*MergeSubsection {
pub fn mergeSubsection(symbol: Symbol, elf_file: *Elf) ?*Merge.Subsection {
if (!symbol.flags.merge_subsection) return null;
const msec = elf_file.mergeSection(symbol.ref.file);
return msec.mergeSubsection(symbol.ref.index);
@ -128,7 +128,7 @@ pub fn address(symbol: Symbol, opts: struct { plt: bool = true, trampoline: bool
if (mem.eql(u8, atom_ptr.name(elf_file), ".eh_frame")) {
const sym_name = symbol.name(elf_file);
const sh_addr, const sh_size = blk: {
const shndx = elf_file.eh_frame_section_index orelse break :blk .{ 0, 0 };
const shndx = elf_file.section_indexes.eh_frame orelse break :blk .{ 0, 0 };
const shdr = elf_file.sections.items(.shdr)[shndx];
break :blk .{ shdr.sh_addr, shdr.sh_size };
};
@ -176,7 +176,7 @@ pub fn gotAddress(symbol: Symbol, elf_file: *Elf) i64 {
pub fn pltGotAddress(symbol: Symbol, elf_file: *Elf) i64 {
if (!symbol.flags.has_pltgot) return 0;
const extras = symbol.extra(elf_file);
const shdr = elf_file.sections.items(.shdr)[elf_file.plt_got_section_index.?];
const shdr = elf_file.sections.items(.shdr)[elf_file.section_indexes.plt_got.?];
const cpu_arch = elf_file.getTarget().cpu.arch;
return @intCast(shdr.sh_addr + extras.plt_got * PltGotSection.entrySize(cpu_arch));
}
@ -184,7 +184,7 @@ pub fn pltGotAddress(symbol: Symbol, elf_file: *Elf) i64 {
pub fn pltAddress(symbol: Symbol, elf_file: *Elf) i64 {
if (!symbol.flags.has_plt) return 0;
const extras = symbol.extra(elf_file);
const shdr = elf_file.sections.items(.shdr)[elf_file.plt_section_index.?];
const shdr = elf_file.sections.items(.shdr)[elf_file.section_indexes.plt.?];
const cpu_arch = elf_file.getTarget().cpu.arch;
return @intCast(shdr.sh_addr + extras.plt * PltSection.entrySize(cpu_arch) + PltSection.preambleSize(cpu_arch));
}
@ -192,13 +192,13 @@ pub fn pltAddress(symbol: Symbol, elf_file: *Elf) i64 {
pub fn gotPltAddress(symbol: Symbol, elf_file: *Elf) i64 {
if (!symbol.flags.has_plt) return 0;
const extras = symbol.extra(elf_file);
const shdr = elf_file.sections.items(.shdr)[elf_file.got_plt_section_index.?];
const shdr = elf_file.sections.items(.shdr)[elf_file.section_indexes.got_plt.?];
return @intCast(shdr.sh_addr + extras.plt * 8 + GotPltSection.preamble_size);
}
pub fn copyRelAddress(symbol: Symbol, elf_file: *Elf) i64 {
if (!symbol.flags.has_copy_rel) return 0;
const shdr = elf_file.sections.items(.shdr)[elf_file.copy_rel_section_index.?];
const shdr = elf_file.sections.items(.shdr)[elf_file.section_indexes.copy_rel.?];
return @as(i64, @intCast(shdr.sh_addr)) + symbol.value;
}
@ -289,7 +289,7 @@ pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void {
break :blk esym.st_bind();
};
const st_shndx: u16 = blk: {
if (symbol.flags.has_copy_rel) break :blk @intCast(elf_file.copy_rel_section_index.?);
if (symbol.flags.has_copy_rel) break :blk @intCast(elf_file.section_indexes.copy_rel.?);
if (file_ptr == .shared_object or esym.st_shndx == elf.SHN_UNDEF) break :blk elf.SHN_UNDEF;
if (elf_file.base.isRelocatable() and esym.st_shndx == elf.SHN_COMMON) break :blk elf.SHN_COMMON;
if (symbol.mergeSubsection(elf_file)) |msub| break :blk @intCast(msub.mergeSection(elf_file).output_section_index);
@ -493,7 +493,7 @@ const File = @import("file.zig").File;
const GotSection = synthetic_sections.GotSection;
const GotPltSection = synthetic_sections.GotPltSection;
const LinkerDefined = @import("LinkerDefined.zig");
const MergeSubsection = @import("merge_section.zig").MergeSubsection;
const Merge = @import("Merge.zig");
const Object = @import("Object.zig");
const PltSection = synthetic_sections.PltSection;
const PltGotSection = synthetic_sections.PltGotSection;

View File

@ -791,7 +791,7 @@ pub fn initRelaSections(self: *ZigObject, elf_file: *Elf) !void {
for (self.atoms_indexes.items) |atom_index| {
const atom_ptr = self.atom(atom_index) orelse continue;
if (!atom_ptr.alive) continue;
if (atom_ptr.output_section_index == elf_file.eh_frame_section_index) continue;
if (atom_ptr.output_section_index == elf_file.section_indexes.eh_frame) continue;
const rela_shndx = atom_ptr.relocsShndx() orelse continue;
// TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level
if (self.relocs.items[rela_shndx].items.len == 0) continue;
@ -812,7 +812,7 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
for (self.atoms_indexes.items) |atom_index| {
const atom_ptr = self.atom(atom_index) orelse continue;
if (!atom_ptr.alive) continue;
if (atom_ptr.output_section_index == elf_file.eh_frame_section_index) continue;
if (atom_ptr.output_section_index == elf_file.section_indexes.eh_frame) continue;
const rela_shndx = atom_ptr.relocsShndx() orelse continue;
// TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level
if (self.relocs.items[rela_shndx].items.len == 0) continue;
@ -826,7 +826,7 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
const out_rela_shndx = elf_file.sectionByName(rela_sect_name).?;
const out_rela_shdr = &elf_file.sections.items(.shdr)[out_rela_shndx];
out_rela_shdr.sh_info = out_shndx;
out_rela_shdr.sh_link = elf_file.symtab_section_index.?;
out_rela_shdr.sh_link = elf_file.section_indexes.symtab.?;
const atom_list = &elf_file.sections.items(.atom_list)[out_rela_shndx];
try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
}
@ -2027,7 +2027,7 @@ pub fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, requires_padding: bool, e
log.debug(" prev {?}, next {?}", .{ atom_ptr.prev_atom_ref, atom_ptr.next_atom_ref });
}
pub fn resetShdrIndexes(self: *ZigObject, backlinks: anytype) void {
pub fn resetShdrIndexes(self: *ZigObject, backlinks: []const u32) void {
for (self.atoms_indexes.items) |atom_index| {
const atom_ptr = self.atom(atom_index) orelse continue;
atom_ptr.output_section_index = backlinks[atom_ptr.output_section_index];

View File

@ -12,7 +12,7 @@ pub const Fde = struct {
out_offset: u64 = 0,
pub fn address(fde: Fde, elf_file: *Elf) u64 {
const base: u64 = if (elf_file.eh_frame_section_index) |shndx|
const base: u64 = if (elf_file.section_indexes.eh_frame) |shndx|
elf_file.sections.items(.shdr)[shndx].sh_addr
else
0;
@ -111,7 +111,7 @@ pub const Cie = struct {
alive: bool = false,
pub fn address(cie: Cie, elf_file: *Elf) u64 {
const base: u64 = if (elf_file.eh_frame_section_index) |shndx|
const base: u64 = if (elf_file.section_indexes.eh_frame) |shndx|
elf_file.sections.items(.shdr)[shndx].sh_addr
else
0;
@ -337,7 +337,7 @@ fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file:
pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void {
relocs_log.debug("{x}: .eh_frame", .{
elf_file.sections.items(.shdr)[elf_file.eh_frame_section_index.?].sh_addr,
elf_file.sections.items(.shdr)[elf_file.section_indexes.eh_frame.?].sh_addr,
});
var has_reloc_errors = false;
@ -458,7 +458,7 @@ fn emitReloc(elf_file: *Elf, r_offset: u64, sym: *const Symbol, rel: elf.Elf64_R
pub fn writeEhFrameRelocs(elf_file: *Elf, writer: anytype) !void {
relocs_log.debug("{x}: .eh_frame", .{
elf_file.sections.items(.shdr)[elf_file.eh_frame_section_index.?].sh_addr,
elf_file.sections.items(.shdr)[elf_file.section_indexes.eh_frame.?].sh_addr,
});
if (elf_file.zigObjectPtr()) |zo| zo: {
@ -511,8 +511,8 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
try writer.writeByte(DW_EH_PE.datarel | DW_EH_PE.sdata4);
const shdrs = elf_file.sections.items(.shdr);
const eh_frame_shdr = shdrs[elf_file.eh_frame_section_index.?];
const eh_frame_hdr_shdr = shdrs[elf_file.eh_frame_hdr_section_index.?];
const eh_frame_shdr = shdrs[elf_file.section_indexes.eh_frame.?];
const eh_frame_hdr_shdr = shdrs[elf_file.section_indexes.eh_frame_hdr.?];
const num_fdes = @as(u32, @intCast(@divExact(eh_frame_hdr_shdr.sh_size - eh_frame_hdr_header_size, 8)));
const existing_size = existing_size: {
const zo = elf_file.zigObjectPtr() orelse break :existing_size 0;

View File

@ -32,7 +32,16 @@ pub fn flushStaticLib(elf_file: *Elf, comp: *Compilation, module_obj_path: ?Path
zig_object.claimUnresolvedRelocatable(elf_file);
try initSections(elf_file);
try elf_file.sortShdrs();
try Elf.sortShdrs(
gpa,
&elf_file.section_indexes,
&elf_file.sections,
elf_file.shstrtab.items,
elf_file.merge_sections.items,
elf_file.comdat_group_sections.items,
elf_file.zigObjectPtr(),
elf_file.files,
);
try zig_object.addAtomsToRelaSections(elf_file);
try elf_file.updateMergeSectionSizes();
try updateSectionSizes(elf_file);
@ -171,7 +180,16 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?Path) l
claimUnresolved(elf_file);
try initSections(elf_file);
try elf_file.sortShdrs();
try Elf.sortShdrs(
comp.gpa,
&elf_file.section_indexes,
&elf_file.sections,
elf_file.shstrtab.items,
elf_file.merge_sections.items,
elf_file.comdat_group_sections.items,
elf_file.zigObjectPtr(),
elf_file.files,
);
if (elf_file.zigObjectPtr()) |zig_object| {
try zig_object.addAtomsToRelaSections(elf_file);
}
@ -288,8 +306,8 @@ fn initSections(elf_file: *Elf) !void {
} else false;
};
if (needs_eh_frame) {
if (elf_file.eh_frame_section_index == null) {
elf_file.eh_frame_section_index = elf_file.sectionByName(".eh_frame") orelse
if (elf_file.section_indexes.eh_frame == null) {
elf_file.section_indexes.eh_frame = elf_file.sectionByName(".eh_frame") orelse
try elf_file.addSection(.{
.name = try elf_file.insertShString(".eh_frame"),
.type = if (elf_file.getTarget().cpu.arch == .x86_64)
@ -300,10 +318,10 @@ fn initSections(elf_file: *Elf) !void {
.addralign = elf_file.ptrWidthBytes(),
});
}
elf_file.eh_frame_rela_section_index = elf_file.sectionByName(".rela.eh_frame") orelse
elf_file.section_indexes.eh_frame_rela = elf_file.sectionByName(".rela.eh_frame") orelse
try elf_file.addRelaShdr(
try elf_file.insertShString(".rela.eh_frame"),
elf_file.eh_frame_section_index.?,
elf_file.section_indexes.eh_frame.?,
);
}
@ -346,7 +364,7 @@ fn updateSectionSizes(elf_file: *Elf) !void {
for (slice.items(.shdr), 0..) |*shdr, shndx| {
const atom_list = slice.items(.atom_list)[shndx];
if (shdr.sh_type != elf.SHT_RELA) continue;
if (@as(u32, @intCast(shndx)) == elf_file.eh_frame_section_index) continue;
if (@as(u32, @intCast(shndx)) == elf_file.section_indexes.eh_frame) continue;
for (atom_list.items) |ref| {
const atom_ptr = elf_file.atom(ref) orelse continue;
if (!atom_ptr.alive) continue;
@ -357,10 +375,10 @@ fn updateSectionSizes(elf_file: *Elf) !void {
if (shdr.sh_size == 0) shdr.sh_offset = 0;
}
if (elf_file.eh_frame_section_index) |index| {
if (elf_file.section_indexes.eh_frame) |index| {
slice.items(.shdr)[index].sh_size = try eh_frame.calcEhFrameSize(elf_file);
}
if (elf_file.eh_frame_rela_section_index) |index| {
if (elf_file.section_indexes.eh_frame_rela) |index| {
const shdr = &slice.items(.shdr)[index];
shdr.sh_size = eh_frame.calcEhFrameRelocs(elf_file) * shdr.sh_entsize;
}
@ -374,7 +392,7 @@ fn updateComdatGroupsSizes(elf_file: *Elf) void {
for (elf_file.comdat_group_sections.items) |cg| {
const shdr = &elf_file.sections.items(.shdr)[cg.shndx];
shdr.sh_size = cg.size(elf_file);
shdr.sh_link = elf_file.symtab_section_index.?;
shdr.sh_link = elf_file.section_indexes.symtab.?;
const sym = cg.symbol(elf_file);
shdr.sh_info = sym.outputSymtabIndex(elf_file) orelse sym.outputShndx(elf_file).?;
@ -439,7 +457,7 @@ fn writeSyntheticSections(elf_file: *Elf) !void {
for (slice.items(.shdr), slice.items(.atom_list), 0..) |shdr, atom_list, shndx| {
if (shdr.sh_type != elf.SHT_RELA) continue;
if (atom_list.items.len == 0) continue;
if (@as(u32, @intCast(shndx)) == elf_file.eh_frame_section_index) continue;
if (@as(u32, @intCast(shndx)) == elf_file.section_indexes.eh_frame) continue;
const num_relocs = math.cast(usize, @divExact(shdr.sh_size, shdr.sh_entsize)) orelse
return error.Overflow;
@ -471,7 +489,7 @@ fn writeSyntheticSections(elf_file: *Elf) !void {
try elf_file.base.file.?.pwriteAll(mem.sliceAsBytes(relocs.items), shdr.sh_offset);
}
if (elf_file.eh_frame_section_index) |shndx| {
if (elf_file.section_indexes.eh_frame) |shndx| {
const existing_size = existing_size: {
const zo = elf_file.zigObjectPtr() orelse break :existing_size 0;
const sym = zo.symbol(zo.eh_frame_index orelse break :existing_size 0);
@ -489,7 +507,7 @@ fn writeSyntheticSections(elf_file: *Elf) !void {
assert(buffer.items.len == sh_size - existing_size);
try elf_file.base.file.?.pwriteAll(buffer.items, shdr.sh_offset + existing_size);
}
if (elf_file.eh_frame_rela_section_index) |shndx| {
if (elf_file.section_indexes.eh_frame_rela) |shndx| {
const shdr = slice.items(.shdr)[shndx];
const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
var buffer = try std.ArrayList(u8).initCapacity(gpa, sh_size);

View File

@ -75,18 +75,18 @@ pub const DynamicSection = struct {
if (elf_file.sectionByName(".fini") != null) nentries += 1; // FINI
if (elf_file.sectionByName(".init_array") != null) nentries += 2; // INIT_ARRAY
if (elf_file.sectionByName(".fini_array") != null) nentries += 2; // FINI_ARRAY
if (elf_file.rela_dyn_section_index != null) nentries += 3; // RELA
if (elf_file.rela_plt_section_index != null) nentries += 3; // JMPREL
if (elf_file.got_plt_section_index != null) nentries += 1; // PLTGOT
if (elf_file.section_indexes.rela_dyn != null) nentries += 3; // RELA
if (elf_file.section_indexes.rela_plt != null) nentries += 3; // JMPREL
if (elf_file.section_indexes.got_plt != null) nentries += 1; // PLTGOT
nentries += 1; // HASH
if (elf_file.gnu_hash_section_index != null) nentries += 1; // GNU_HASH
if (elf_file.section_indexes.gnu_hash != null) nentries += 1; // GNU_HASH
if (elf_file.has_text_reloc) nentries += 1; // TEXTREL
nentries += 1; // SYMTAB
nentries += 1; // SYMENT
nentries += 1; // STRTAB
nentries += 1; // STRSZ
if (elf_file.versym_section_index != null) nentries += 1; // VERSYM
if (elf_file.verneed_section_index != null) nentries += 2; // VERNEED
if (elf_file.section_indexes.versym != null) nentries += 1; // VERSYM
if (elf_file.section_indexes.verneed != 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.isEffectivelyDynLib()) nentries += 1; // DEBUG
@ -139,7 +139,7 @@ pub const DynamicSection = struct {
}
// RELA
if (elf_file.rela_dyn_section_index) |shndx| {
if (elf_file.section_indexes.rela_dyn) |shndx| {
const shdr = shdrs[shndx];
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_RELA, .d_val = shdr.sh_addr });
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_RELASZ, .d_val = shdr.sh_size });
@ -147,7 +147,7 @@ pub const DynamicSection = struct {
}
// JMPREL
if (elf_file.rela_plt_section_index) |shndx| {
if (elf_file.section_indexes.rela_plt) |shndx| {
const shdr = shdrs[shndx];
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_JMPREL, .d_val = shdr.sh_addr });
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_PLTRELSZ, .d_val = shdr.sh_size });
@ -155,18 +155,18 @@ pub const DynamicSection = struct {
}
// PLTGOT
if (elf_file.got_plt_section_index) |shndx| {
if (elf_file.section_indexes.got_plt) |shndx| {
const addr = shdrs[shndx].sh_addr;
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_PLTGOT, .d_val = addr });
}
{
assert(elf_file.hash_section_index != null);
const addr = shdrs[elf_file.hash_section_index.?].sh_addr;
assert(elf_file.section_indexes.hash != null);
const addr = shdrs[elf_file.section_indexes.hash.?].sh_addr;
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_HASH, .d_val = addr });
}
if (elf_file.gnu_hash_section_index) |shndx| {
if (elf_file.section_indexes.gnu_hash) |shndx| {
const addr = shdrs[shndx].sh_addr;
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_GNU_HASH, .d_val = addr });
}
@ -178,28 +178,28 @@ pub const DynamicSection = struct {
// SYMTAB + SYMENT
{
assert(elf_file.dynsymtab_section_index != null);
const shdr = shdrs[elf_file.dynsymtab_section_index.?];
assert(elf_file.section_indexes.dynsymtab != null);
const shdr = shdrs[elf_file.section_indexes.dynsymtab.?];
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_SYMTAB, .d_val = shdr.sh_addr });
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_SYMENT, .d_val = shdr.sh_entsize });
}
// STRTAB + STRSZ
{
assert(elf_file.dynstrtab_section_index != null);
const shdr = shdrs[elf_file.dynstrtab_section_index.?];
assert(elf_file.section_indexes.dynstrtab != null);
const shdr = shdrs[elf_file.section_indexes.dynstrtab.?];
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_STRTAB, .d_val = shdr.sh_addr });
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_STRSZ, .d_val = shdr.sh_size });
}
// VERSYM
if (elf_file.versym_section_index) |shndx| {
if (elf_file.section_indexes.versym) |shndx| {
const addr = shdrs[shndx].sh_addr;
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_VERSYM, .d_val = addr });
}
// VERNEED + VERNEEDNUM
if (elf_file.verneed_section_index) |shndx| {
if (elf_file.section_indexes.verneed) |shndx| {
const addr = shdrs[shndx].sh_addr;
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_VERNEED, .d_val = addr });
try writer.writeStruct(elf.Elf64_Dyn{
@ -261,7 +261,7 @@ pub const GotSection = struct {
pub fn address(entry: Entry, elf_file: *Elf) i64 {
const ptr_bytes = elf_file.archPtrWidthBytes();
const shdr = &elf_file.sections.items(.shdr)[elf_file.got_section_index.?];
const shdr = &elf_file.sections.items(.shdr)[elf_file.section_indexes.got.?];
return @as(i64, @intCast(shdr.sh_addr)) + entry.cell_index * ptr_bytes;
}
};
@ -599,7 +599,7 @@ pub const GotSection = struct {
.st_name = st_name,
.st_info = elf.STT_OBJECT,
.st_other = 0,
.st_shndx = @intCast(elf_file.got_section_index.?),
.st_shndx = @intCast(elf_file.section_indexes.got.?),
.st_value = @intCast(st_value),
.st_size = st_size,
};
@ -742,7 +742,7 @@ pub const PltSection = struct {
.st_name = st_name,
.st_info = elf.STT_FUNC,
.st_other = 0,
.st_shndx = @intCast(elf_file.plt_section_index.?),
.st_shndx = @intCast(elf_file.section_indexes.plt.?),
.st_value = @intCast(sym.pltAddress(elf_file)),
.st_size = entrySize(cpu_arch),
};
@ -784,8 +784,8 @@ pub const PltSection = struct {
const x86_64 = struct {
fn write(plt: PltSection, elf_file: *Elf, writer: anytype) !void {
const shdrs = elf_file.sections.items(.shdr);
const plt_addr = shdrs[elf_file.plt_section_index.?].sh_addr;
const got_plt_addr = shdrs[elf_file.got_plt_section_index.?].sh_addr;
const plt_addr = shdrs[elf_file.section_indexes.plt.?].sh_addr;
const got_plt_addr = shdrs[elf_file.section_indexes.got_plt.?].sh_addr;
var preamble = [_]u8{
0xf3, 0x0f, 0x1e, 0xfa, // endbr64
0x41, 0x53, // push r11
@ -820,8 +820,8 @@ pub const PltSection = struct {
fn write(plt: PltSection, elf_file: *Elf, writer: anytype) !void {
{
const shdrs = elf_file.sections.items(.shdr);
const plt_addr: i64 = @intCast(shdrs[elf_file.plt_section_index.?].sh_addr);
const got_plt_addr: i64 = @intCast(shdrs[elf_file.got_plt_section_index.?].sh_addr);
const plt_addr: i64 = @intCast(shdrs[elf_file.section_indexes.plt.?].sh_addr);
const got_plt_addr: i64 = @intCast(shdrs[elf_file.section_indexes.got_plt.?].sh_addr);
// TODO: relax if possible
// .got.plt[2]
const pages = try aarch64_util.calcNumberOfPages(plt_addr + 4, got_plt_addr + 16);
@ -894,7 +894,7 @@ pub const GotPltSection = struct {
// [2]: 0x0
try writer.writeInt(u64, 0x0, .little);
try writer.writeInt(u64, 0x0, .little);
if (elf_file.plt_section_index) |shndx| {
if (elf_file.section_indexes.plt) |shndx| {
const plt_addr = elf_file.sections.items(.shdr)[shndx].sh_addr;
for (0..elf_file.plt.symbols.items.len) |_| {
// [N]: .plt
@ -962,7 +962,7 @@ pub const PltGotSection = struct {
.st_name = st_name,
.st_info = elf.STT_FUNC,
.st_other = 0,
.st_shndx = @intCast(elf_file.plt_got_section_index.?),
.st_shndx = @intCast(elf_file.section_indexes.plt_got.?),
.st_value = @intCast(sym.pltGotAddress(elf_file)),
.st_size = 16,
};