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 {...@@ -543,7 +543,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 {
543 const shdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Shdr) else @sizeOf(elf.Elf64_Shdr);543 const shdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Shdr) else @sizeOf(elf.Elf64_Shdr);
544 const tight_size = self.shdrs.items.len * shdr_size;544 const tight_size = self.shdrs.items.len * shdr_size;
545 const increased_size = padToIdeal(tight_size);545 const increased_size = padToIdeal(tight_size);
546 const test_end = off + increased_size;546 const test_end = off +| increased_size;
547 if (end > off and start < test_end) {547 if (end > off and start < test_end) {
548 return test_end;548 return test_end;
549 }549 }
...@@ -552,7 +552,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 {...@@ -552,7 +552,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 {
552 for (self.shdrs.items) |shdr| {552 for (self.shdrs.items) |shdr| {
553 if (shdr.sh_type == elf.SHT_NOBITS) continue;553 if (shdr.sh_type == elf.SHT_NOBITS) continue;
554 const increased_size = padToIdeal(shdr.sh_size);554 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;
556 if (end > shdr.sh_offset and start < test_end) {556 if (end > shdr.sh_offset and start < test_end) {
557 return test_end;557 return test_end;
558 }558 }
...@@ -561,7 +561,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 {...@@ -561,7 +561,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 {
561 for (self.phdrs.items) |phdr| {561 for (self.phdrs.items) |phdr| {
562 if (phdr.p_type != elf.PT_LOAD) continue;562 if (phdr.p_type != elf.PT_LOAD) continue;
563 const increased_size = padToIdeal(phdr.p_filesz);563 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;
565 if (end > phdr.p_offset and start < test_end) {565 if (end > phdr.p_offset and start < test_end) {
566 return test_end;566 return test_end;
567 }567 }
...@@ -653,6 +653,7 @@ pub fn allocateAllocSection(self: *Elf, opts: AllocateAllocSectionOpts) error{Ou...@@ -653,6 +653,7 @@ pub fn allocateAllocSection(self: *Elf, opts: AllocateAllocSectionOpts) error{Ou
653 .type = opts.type,653 .type = opts.type,
654 .flags = opts.flags,654 .flags = opts.flags,
655 .addralign = opts.alignment,655 .addralign = opts.alignment,
656 .offset = std.math.maxInt(u64),
656 });657 });
657 const shdr = &self.shdrs.items[index];658 const shdr = &self.shdrs.items[index];
658 try self.phdr_to_shdr_table.putNoClobber(gpa, index, opts.phdr_index);659 try self.phdr_to_shdr_table.putNoClobber(gpa, index, opts.phdr_index);
...@@ -690,6 +691,7 @@ fn allocateNonAllocSection(self: *Elf, opts: AllocateNonAllocSectionOpts) error{...@@ -690,6 +691,7 @@ fn allocateNonAllocSection(self: *Elf, opts: AllocateNonAllocSectionOpts) error{
690 .info = opts.info,691 .info = opts.info,
691 .addralign = opts.alignment,692 .addralign = opts.alignment,
692 .entsize = opts.entsize,693 .entsize = opts.entsize,
694 .offset = std.math.maxInt(u64),
693 });695 });
694 const shdr = &self.shdrs.items[index];696 const shdr = &self.shdrs.items[index];
695 const off = self.findFreeSpace(opts.size, opts.alignment);697 const off = self.findFreeSpace(opts.size, opts.alignment);
...@@ -3841,6 +3843,7 @@ fn initSections(self: *Elf) !void {...@@ -3841,6 +3843,7 @@ fn initSections(self: *Elf) !void {
3841 .type = elf.SHT_PROGBITS,3843 .type = elf.SHT_PROGBITS,
3842 .flags = elf.SHF_ALLOC,3844 .flags = elf.SHF_ALLOC,
3843 .addralign = ptr_size,3845 .addralign = ptr_size,
3846 .offset = std.math.maxInt(u64),
3844 });3847 });
38453848
3846 if (self.base.options.eh_frame_hdr) {3849 if (self.base.options.eh_frame_hdr) {
...@@ -3849,6 +3852,7 @@ fn initSections(self: *Elf) !void {...@@ -3849,6 +3852,7 @@ fn initSections(self: *Elf) !void {
3849 .type = elf.SHT_PROGBITS,3852 .type = elf.SHT_PROGBITS,
3850 .flags = elf.SHF_ALLOC,3853 .flags = elf.SHF_ALLOC,
3851 .addralign = 4,3854 .addralign = 4,
3855 .offset = std.math.maxInt(u64),
3852 });3856 });
3853 }3857 }
3854 }3858 }
...@@ -3859,6 +3863,7 @@ fn initSections(self: *Elf) !void {...@@ -3859,6 +3863,7 @@ fn initSections(self: *Elf) !void {
3859 .type = elf.SHT_PROGBITS,3863 .type = elf.SHT_PROGBITS,
3860 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,3864 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
3861 .addralign = ptr_size,3865 .addralign = ptr_size,
3866 .offset = std.math.maxInt(u64),
3862 });3867 });
3863 }3868 }
38643869
...@@ -3880,6 +3885,7 @@ fn initSections(self: *Elf) !void {...@@ -3880,6 +3885,7 @@ fn initSections(self: *Elf) !void {
3880 .flags = elf.SHF_ALLOC,3885 .flags = elf.SHF_ALLOC,
3881 .addralign = @alignOf(elf.Elf64_Rela),3886 .addralign = @alignOf(elf.Elf64_Rela),
3882 .entsize = @sizeOf(elf.Elf64_Rela),3887 .entsize = @sizeOf(elf.Elf64_Rela),
3888 .offset = std.math.maxInt(u64),
3883 });3889 });
3884 }3890 }
38853891
...@@ -3889,12 +3895,14 @@ fn initSections(self: *Elf) !void {...@@ -3889,12 +3895,14 @@ fn initSections(self: *Elf) !void {
3889 .type = elf.SHT_PROGBITS,3895 .type = elf.SHT_PROGBITS,
3890 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,3896 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
3891 .addralign = 16,3897 .addralign = 16,
3898 .offset = std.math.maxInt(u64),
3892 });3899 });
3893 self.got_plt_section_index = try self.addSection(.{3900 self.got_plt_section_index = try self.addSection(.{
3894 .name = ".got.plt",3901 .name = ".got.plt",
3895 .type = elf.SHT_PROGBITS,3902 .type = elf.SHT_PROGBITS,
3896 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,3903 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
3897 .addralign = @alignOf(u64),3904 .addralign = @alignOf(u64),
3905 .offset = std.math.maxInt(u64),
3898 });3906 });
3899 self.rela_plt_section_index = try self.addSection(.{3907 self.rela_plt_section_index = try self.addSection(.{
3900 .name = ".rela.plt",3908 .name = ".rela.plt",
...@@ -3902,6 +3910,7 @@ fn initSections(self: *Elf) !void {...@@ -3902,6 +3910,7 @@ fn initSections(self: *Elf) !void {
3902 .flags = elf.SHF_ALLOC,3910 .flags = elf.SHF_ALLOC,
3903 .addralign = @alignOf(elf.Elf64_Rela),3911 .addralign = @alignOf(elf.Elf64_Rela),
3904 .entsize = @sizeOf(elf.Elf64_Rela),3912 .entsize = @sizeOf(elf.Elf64_Rela),
3913 .offset = std.math.maxInt(u64),
3905 });3914 });
3906 }3915 }
39073916
...@@ -3911,6 +3920,7 @@ fn initSections(self: *Elf) !void {...@@ -3911,6 +3920,7 @@ fn initSections(self: *Elf) !void {
3911 .type = elf.SHT_PROGBITS,3920 .type = elf.SHT_PROGBITS,
3912 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,3921 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
3913 .addralign = 16,3922 .addralign = 16,
3923 .offset = std.math.maxInt(u64),
3914 });3924 });
3915 }3925 }
39163926
...@@ -3919,6 +3929,7 @@ fn initSections(self: *Elf) !void {...@@ -3919,6 +3929,7 @@ fn initSections(self: *Elf) !void {
3919 .name = ".copyrel",3929 .name = ".copyrel",
3920 .type = elf.SHT_NOBITS,3930 .type = elf.SHT_NOBITS,
3921 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,3931 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
3932 .offset = std.math.maxInt(u64),
3922 });3933 });
3923 }3934 }
39243935
...@@ -3937,6 +3948,7 @@ fn initSections(self: *Elf) !void {...@@ -3937,6 +3948,7 @@ fn initSections(self: *Elf) !void {
3937 .type = elf.SHT_PROGBITS,3948 .type = elf.SHT_PROGBITS,
3938 .flags = elf.SHF_ALLOC,3949 .flags = elf.SHF_ALLOC,
3939 .addralign = 1,3950 .addralign = 1,
3951 .offset = std.math.maxInt(u64),
3940 });3952 });
3941 }3953 }
39423954
...@@ -3947,6 +3959,7 @@ fn initSections(self: *Elf) !void {...@@ -3947,6 +3959,7 @@ fn initSections(self: *Elf) !void {
3947 .type = elf.SHT_STRTAB,3959 .type = elf.SHT_STRTAB,
3948 .entsize = 1,3960 .entsize = 1,
3949 .addralign = 1,3961 .addralign = 1,
3962 .offset = std.math.maxInt(u64),
3950 });3963 });
3951 self.dynamic_section_index = try self.addSection(.{3964 self.dynamic_section_index = try self.addSection(.{
3952 .name = ".dynamic",3965 .name = ".dynamic",
...@@ -3954,6 +3967,7 @@ fn initSections(self: *Elf) !void {...@@ -3954,6 +3967,7 @@ fn initSections(self: *Elf) !void {
3954 .type = elf.SHT_DYNAMIC,3967 .type = elf.SHT_DYNAMIC,
3955 .entsize = @sizeOf(elf.Elf64_Dyn),3968 .entsize = @sizeOf(elf.Elf64_Dyn),
3956 .addralign = @alignOf(elf.Elf64_Dyn),3969 .addralign = @alignOf(elf.Elf64_Dyn),
3970 .offset = std.math.maxInt(u64),
3957 });3971 });
3958 self.dynsymtab_section_index = try self.addSection(.{3972 self.dynsymtab_section_index = try self.addSection(.{
3959 .name = ".dynsym",3973 .name = ".dynsym",
...@@ -3962,6 +3976,7 @@ fn initSections(self: *Elf) !void {...@@ -3962,6 +3976,7 @@ fn initSections(self: *Elf) !void {
3962 .addralign = @alignOf(elf.Elf64_Sym),3976 .addralign = @alignOf(elf.Elf64_Sym),
3963 .entsize = @sizeOf(elf.Elf64_Sym),3977 .entsize = @sizeOf(elf.Elf64_Sym),
3964 .info = 1,3978 .info = 1,
3979 .offset = std.math.maxInt(u64),
3965 });3980 });
3966 self.hash_section_index = try self.addSection(.{3981 self.hash_section_index = try self.addSection(.{
3967 .name = ".hash",3982 .name = ".hash",
...@@ -3969,12 +3984,14 @@ fn initSections(self: *Elf) !void {...@@ -3969,12 +3984,14 @@ fn initSections(self: *Elf) !void {
3969 .type = elf.SHT_HASH,3984 .type = elf.SHT_HASH,
3970 .addralign = 4,3985 .addralign = 4,
3971 .entsize = 4,3986 .entsize = 4,
3987 .offset = std.math.maxInt(u64),
3972 });3988 });
3973 self.gnu_hash_section_index = try self.addSection(.{3989 self.gnu_hash_section_index = try self.addSection(.{
3974 .name = ".gnu.hash",3990 .name = ".gnu.hash",
3975 .flags = elf.SHF_ALLOC,3991 .flags = elf.SHF_ALLOC,
3976 .type = elf.SHT_GNU_HASH,3992 .type = elf.SHT_GNU_HASH,
3977 .addralign = 8,3993 .addralign = 8,
3994 .offset = std.math.maxInt(u64),
3978 });3995 });
39793996
3980 const needs_versions = for (self.dynsym.entries.items) |entry| {3997 const needs_versions = for (self.dynsym.entries.items) |entry| {
...@@ -3988,12 +4005,14 @@ fn initSections(self: *Elf) !void {...@@ -3988,12 +4005,14 @@ fn initSections(self: *Elf) !void {
3988 .type = elf.SHT_GNU_VERSYM,4005 .type = elf.SHT_GNU_VERSYM,
3989 .addralign = @alignOf(elf.Elf64_Versym),4006 .addralign = @alignOf(elf.Elf64_Versym),
3990 .entsize = @sizeOf(elf.Elf64_Versym),4007 .entsize = @sizeOf(elf.Elf64_Versym),
4008 .offset = std.math.maxInt(u64),
3991 });4009 });
3992 self.verneed_section_index = try self.addSection(.{4010 self.verneed_section_index = try self.addSection(.{
3993 .name = ".gnu.version_r",4011 .name = ".gnu.version_r",
3994 .flags = elf.SHF_ALLOC,4012 .flags = elf.SHF_ALLOC,
3995 .type = elf.SHT_GNU_VERNEED,4013 .type = elf.SHT_GNU_VERNEED,
3996 .addralign = @alignOf(elf.Elf64_Verneed),4014 .addralign = @alignOf(elf.Elf64_Verneed),
4015 .offset = std.math.maxInt(u64),
3997 });4016 });
3998 }4017 }
3999 }4018 }
...@@ -4004,6 +4023,7 @@ fn initSections(self: *Elf) !void {...@@ -4004,6 +4023,7 @@ fn initSections(self: *Elf) !void {
4004 .type = elf.SHT_SYMTAB,4023 .type = elf.SHT_SYMTAB,
4005 .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym),4024 .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym),
4006 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),4025 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),
4026 .offset = std.math.maxInt(u64),
4007 });4027 });
4008 }4028 }
4009 if (self.strtab_section_index == null) {4029 if (self.strtab_section_index == null) {
...@@ -4012,6 +4032,7 @@ fn initSections(self: *Elf) !void {...@@ -4012,6 +4032,7 @@ fn initSections(self: *Elf) !void {
4012 .type = elf.SHT_STRTAB,4032 .type = elf.SHT_STRTAB,
4013 .entsize = 1,4033 .entsize = 1,
4014 .addralign = 1,4034 .addralign = 1,
4035 .offset = std.math.maxInt(u64),
4015 });4036 });
4016 }4037 }
4017 if (self.shstrtab_section_index == null) {4038 if (self.shstrtab_section_index == null) {
...@@ -4020,6 +4041,7 @@ fn initSections(self: *Elf) !void {...@@ -4020,6 +4041,7 @@ fn initSections(self: *Elf) !void {
4020 .type = elf.SHT_STRTAB,4041 .type = elf.SHT_STRTAB,
4021 .entsize = 1,4042 .entsize = 1,
4022 .addralign = 1,4043 .addralign = 1,
4044 .offset = std.math.maxInt(u64),
4023 });4045 });
4024 }4046 }
4025}4047}
...@@ -5651,6 +5673,7 @@ pub const AddSectionOpts = struct {...@@ -5651,6 +5673,7 @@ pub const AddSectionOpts = struct {
5651 info: u32 = 0,5673 info: u32 = 0,
5652 addralign: u64 = 0,5674 addralign: u64 = 0,
5653 entsize: u64 = 0,5675 entsize: u64 = 0,
5676 offset: u64 = 0,
5654};5677};
56555678
5656pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 {5679pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 {
...@@ -5662,7 +5685,7 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 {...@@ -5662,7 +5685,7 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 {
5662 .sh_type = opts.type,5685 .sh_type = opts.type,
5663 .sh_flags = opts.flags,5686 .sh_flags = opts.flags,
5664 .sh_addr = 0,5687 .sh_addr = 0,
5665 .sh_offset = 0,5688 .sh_offset = opts.offset,
5666 .sh_size = 0,5689 .sh_size = 0,
5667 .sh_link = opts.link,5690 .sh_link = opts.link,
5668 .sh_info = opts.info,5691 .sh_info = opts.info,