authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-24 17:07:00+02:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-10-25 04:27:44-04:00
log0a04bd87bab4842dbf63133952d1d6eee3aa46bc
tree81977bf9bbd1fd74002266f3549771995134163c
parentf30ab46306d7d137ed70bbcb3466c27846833ed3

elf: use std.math.maxInt(u64) as signal that shdr/phdr not allocated yet


1 files changed, 27 insertions(+), 4 deletions(-)

src/link/Elf.zig+27-4
......@@ -543,7 +543,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 {
543543 const shdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Shdr) else @sizeOf(elf.Elf64_Shdr);
544544 const tight_size = self.shdrs.items.len * shdr_size;
545545 const increased_size = padToIdeal(tight_size);
546 const test_end = off + increased_size;
546 const test_end = off +| increased_size;
547547 if (end > off and start < test_end) {
548548 return test_end;
549549 }
......@@ -552,7 +552,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 {
552552 for (self.shdrs.items) |shdr| {
553553 if (shdr.sh_type == elf.SHT_NOBITS) continue;
554554 const increased_size = padToIdeal(shdr.sh_size);
555 const test_end = shdr.sh_offset + increased_size;
555 const test_end = shdr.sh_offset +| increased_size;
556556 if (end > shdr.sh_offset and start < test_end) {
557557 return test_end;
558558 }
......@@ -561,7 +561,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 {
561561 for (self.phdrs.items) |phdr| {
562562 if (phdr.p_type != elf.PT_LOAD) continue;
563563 const increased_size = padToIdeal(phdr.p_filesz);
564 const test_end = phdr.p_offset + increased_size;
564 const test_end = phdr.p_offset +| increased_size;
565565 if (end > phdr.p_offset and start < test_end) {
566566 return test_end;
567567 }
......@@ -653,6 +653,7 @@ pub fn allocateAllocSection(self: *Elf, opts: AllocateAllocSectionOpts) error{Ou
653653 .type = opts.type,
654654 .flags = opts.flags,
655655 .addralign = opts.alignment,
656 .offset = std.math.maxInt(u64),
656657 });
657658 const shdr = &self.shdrs.items[index];
658659 try self.phdr_to_shdr_table.putNoClobber(gpa, index, opts.phdr_index);
......@@ -690,6 +691,7 @@ fn allocateNonAllocSection(self: *Elf, opts: AllocateNonAllocSectionOpts) error{
690691 .info = opts.info,
691692 .addralign = opts.alignment,
692693 .entsize = opts.entsize,
694 .offset = std.math.maxInt(u64),
693695 });
694696 const shdr = &self.shdrs.items[index];
695697 const off = self.findFreeSpace(opts.size, opts.alignment);
......@@ -3841,6 +3843,7 @@ fn initSections(self: *Elf) !void {
38413843 .type = elf.SHT_PROGBITS,
38423844 .flags = elf.SHF_ALLOC,
38433845 .addralign = ptr_size,
3846 .offset = std.math.maxInt(u64),
38443847 });
38453848
38463849 if (self.base.options.eh_frame_hdr) {
......@@ -3849,6 +3852,7 @@ fn initSections(self: *Elf) !void {
38493852 .type = elf.SHT_PROGBITS,
38503853 .flags = elf.SHF_ALLOC,
38513854 .addralign = 4,
3855 .offset = std.math.maxInt(u64),
38523856 });
38533857 }
38543858 }
......@@ -3859,6 +3863,7 @@ fn initSections(self: *Elf) !void {
38593863 .type = elf.SHT_PROGBITS,
38603864 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
38613865 .addralign = ptr_size,
3866 .offset = std.math.maxInt(u64),
38623867 });
38633868 }
38643869
......@@ -3880,6 +3885,7 @@ fn initSections(self: *Elf) !void {
38803885 .flags = elf.SHF_ALLOC,
38813886 .addralign = @alignOf(elf.Elf64_Rela),
38823887 .entsize = @sizeOf(elf.Elf64_Rela),
3888 .offset = std.math.maxInt(u64),
38833889 });
38843890 }
38853891
......@@ -3889,12 +3895,14 @@ fn initSections(self: *Elf) !void {
38893895 .type = elf.SHT_PROGBITS,
38903896 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
38913897 .addralign = 16,
3898 .offset = std.math.maxInt(u64),
38923899 });
38933900 self.got_plt_section_index = try self.addSection(.{
38943901 .name = ".got.plt",
38953902 .type = elf.SHT_PROGBITS,
38963903 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
38973904 .addralign = @alignOf(u64),
3905 .offset = std.math.maxInt(u64),
38983906 });
38993907 self.rela_plt_section_index = try self.addSection(.{
39003908 .name = ".rela.plt",
......@@ -3902,6 +3910,7 @@ fn initSections(self: *Elf) !void {
39023910 .flags = elf.SHF_ALLOC,
39033911 .addralign = @alignOf(elf.Elf64_Rela),
39043912 .entsize = @sizeOf(elf.Elf64_Rela),
3913 .offset = std.math.maxInt(u64),
39053914 });
39063915 }
39073916
......@@ -3911,6 +3920,7 @@ fn initSections(self: *Elf) !void {
39113920 .type = elf.SHT_PROGBITS,
39123921 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
39133922 .addralign = 16,
3923 .offset = std.math.maxInt(u64),
39143924 });
39153925 }
39163926
......@@ -3919,6 +3929,7 @@ fn initSections(self: *Elf) !void {
39193929 .name = ".copyrel",
39203930 .type = elf.SHT_NOBITS,
39213931 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
3932 .offset = std.math.maxInt(u64),
39223933 });
39233934 }
39243935
......@@ -3937,6 +3948,7 @@ fn initSections(self: *Elf) !void {
39373948 .type = elf.SHT_PROGBITS,
39383949 .flags = elf.SHF_ALLOC,
39393950 .addralign = 1,
3951 .offset = std.math.maxInt(u64),
39403952 });
39413953 }
39423954
......@@ -3947,6 +3959,7 @@ fn initSections(self: *Elf) !void {
39473959 .type = elf.SHT_STRTAB,
39483960 .entsize = 1,
39493961 .addralign = 1,
3962 .offset = std.math.maxInt(u64),
39503963 });
39513964 self.dynamic_section_index = try self.addSection(.{
39523965 .name = ".dynamic",
......@@ -3954,6 +3967,7 @@ fn initSections(self: *Elf) !void {
39543967 .type = elf.SHT_DYNAMIC,
39553968 .entsize = @sizeOf(elf.Elf64_Dyn),
39563969 .addralign = @alignOf(elf.Elf64_Dyn),
3970 .offset = std.math.maxInt(u64),
39573971 });
39583972 self.dynsymtab_section_index = try self.addSection(.{
39593973 .name = ".dynsym",
......@@ -3962,6 +3976,7 @@ fn initSections(self: *Elf) !void {
39623976 .addralign = @alignOf(elf.Elf64_Sym),
39633977 .entsize = @sizeOf(elf.Elf64_Sym),
39643978 .info = 1,
3979 .offset = std.math.maxInt(u64),
39653980 });
39663981 self.hash_section_index = try self.addSection(.{
39673982 .name = ".hash",
......@@ -3969,12 +3984,14 @@ fn initSections(self: *Elf) !void {
39693984 .type = elf.SHT_HASH,
39703985 .addralign = 4,
39713986 .entsize = 4,
3987 .offset = std.math.maxInt(u64),
39723988 });
39733989 self.gnu_hash_section_index = try self.addSection(.{
39743990 .name = ".gnu.hash",
39753991 .flags = elf.SHF_ALLOC,
39763992 .type = elf.SHT_GNU_HASH,
39773993 .addralign = 8,
3994 .offset = std.math.maxInt(u64),
39783995 });
39793996
39803997 const needs_versions = for (self.dynsym.entries.items) |entry| {
......@@ -3988,12 +4005,14 @@ fn initSections(self: *Elf) !void {
39884005 .type = elf.SHT_GNU_VERSYM,
39894006 .addralign = @alignOf(elf.Elf64_Versym),
39904007 .entsize = @sizeOf(elf.Elf64_Versym),
4008 .offset = std.math.maxInt(u64),
39914009 });
39924010 self.verneed_section_index = try self.addSection(.{
39934011 .name = ".gnu.version_r",
39944012 .flags = elf.SHF_ALLOC,
39954013 .type = elf.SHT_GNU_VERNEED,
39964014 .addralign = @alignOf(elf.Elf64_Verneed),
4015 .offset = std.math.maxInt(u64),
39974016 });
39984017 }
39994018 }
......@@ -4004,6 +4023,7 @@ fn initSections(self: *Elf) !void {
40044023 .type = elf.SHT_SYMTAB,
40054024 .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym),
40064025 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),
4026 .offset = std.math.maxInt(u64),
40074027 });
40084028 }
40094029 if (self.strtab_section_index == null) {
......@@ -4012,6 +4032,7 @@ fn initSections(self: *Elf) !void {
40124032 .type = elf.SHT_STRTAB,
40134033 .entsize = 1,
40144034 .addralign = 1,
4035 .offset = std.math.maxInt(u64),
40154036 });
40164037 }
40174038 if (self.shstrtab_section_index == null) {
......@@ -4020,6 +4041,7 @@ fn initSections(self: *Elf) !void {
40204041 .type = elf.SHT_STRTAB,
40214042 .entsize = 1,
40224043 .addralign = 1,
4044 .offset = std.math.maxInt(u64),
40234045 });
40244046 }
40254047}
......@@ -5651,6 +5673,7 @@ pub const AddSectionOpts = struct {
56515673 info: u32 = 0,
56525674 addralign: u64 = 0,
56535675 entsize: u64 = 0,
5676 offset: u64 = 0,
56545677};
56555678
56565679pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 {
......@@ -5662,7 +5685,7 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 {
56625685 .sh_type = opts.type,
56635686 .sh_flags = opts.flags,
56645687 .sh_addr = 0,
5665 .sh_offset = 0,
5688 .sh_offset = opts.offset,
56665689 .sh_size = 0,
56675690 .sh_link = opts.link,
56685691 .sh_info = opts.info,