authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-02 14:00:50+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:03+02:00
logff1423c4cedda2e35d3ad32c350cc6c97da736b5
tree73e9777fe6af56bdca0595e9fd311d75fd645495
parentc8c50a058e4d18a47724c489a2b9b7e3316c105a

elf: reuse addSection in alloc functions


1 files changed, 24 insertions(+), 34 deletions(-)

src/link/Elf.zig+24-34
...@@ -541,9 +541,13 @@ const AllocateAllocSectionOpts = struct {...@@ -541,9 +541,13 @@ const AllocateAllocSectionOpts = struct {
541pub fn allocateAllocSection(self: *Elf, opts: AllocateAllocSectionOpts) error{OutOfMemory}!u16 {541pub fn allocateAllocSection(self: *Elf, opts: AllocateAllocSectionOpts) error{OutOfMemory}!u16 {
542 const gpa = self.base.allocator;542 const gpa = self.base.allocator;
543 const phdr = &self.phdrs.items[opts.phdr_index];543 const phdr = &self.phdrs.items[opts.phdr_index];
544 const index = @as(u16, @intCast(self.shdrs.items.len));544 const index = try self.addSection(.{
545 try self.shdrs.ensureUnusedCapacity(gpa, 1);545 .name = opts.name,
546 const sh_name = try self.shstrtab.insert(gpa, opts.name);546 .type = opts.type,
547 .flags = opts.flags,
548 .addralign = opts.alignment,
549 });
550 const shdr = &self.shdrs.items[index];
547 try self.phdr_to_shdr_table.putNoClobber(gpa, index, opts.phdr_index);551 try self.phdr_to_shdr_table.putNoClobber(gpa, index, opts.phdr_index);
548 log.debug("allocating '{s}' in phdr({d}) from 0x{x} to 0x{x} (0x{x} - 0x{x})", .{552 log.debug("allocating '{s}' in phdr({d}) from 0x{x} to 0x{x} (0x{x} - 0x{x})", .{
549 opts.name,553 opts.name,
...@@ -553,19 +557,9 @@ pub fn allocateAllocSection(self: *Elf, opts: AllocateAllocSectionOpts) error{Ou...@@ -553,19 +557,9 @@ pub fn allocateAllocSection(self: *Elf, opts: AllocateAllocSectionOpts) error{Ou
553 phdr.p_vaddr,557 phdr.p_vaddr,
554 phdr.p_vaddr + phdr.p_memsz,558 phdr.p_vaddr + phdr.p_memsz,
555 });559 });
556 self.shdrs.appendAssumeCapacity(.{560 shdr.sh_addr = phdr.p_vaddr;
557 .sh_name = sh_name,561 shdr.sh_offset = phdr.p_offset;
558 .sh_type = opts.type,562 shdr.sh_size = phdr.p_memsz;
559 .sh_flags = opts.flags,
560 .sh_addr = phdr.p_vaddr,
561 .sh_offset = phdr.p_offset,
562 .sh_size = phdr.p_memsz,
563 .sh_link = 0,
564 .sh_info = 0,
565 .sh_addralign = opts.alignment,
566 .sh_entsize = 0,
567 });
568 self.shdr_table_dirty = true;
569 return index;563 return index;
570}564}
571565
...@@ -581,24 +575,20 @@ const AllocateNonAllocSectionOpts = struct {...@@ -581,24 +575,20 @@ const AllocateNonAllocSectionOpts = struct {
581};575};
582576
583fn allocateNonAllocSection(self: *Elf, opts: AllocateNonAllocSectionOpts) error{OutOfMemory}!u16 {577fn allocateNonAllocSection(self: *Elf, opts: AllocateNonAllocSectionOpts) error{OutOfMemory}!u16 {
584 const index = @as(u16, @intCast(self.shdrs.items.len));578 const index = try self.addSection(.{
585 try self.shdrs.ensureUnusedCapacity(self.base.allocator, 1);579 .name = opts.name,
586 const sh_name = try self.shstrtab.insert(self.base.allocator, opts.name);580 .type = opts.type,
581 .flags = opts.flags,
582 .link = opts.link,
583 .info = opts.info,
584 .addralign = opts.alignment,
585 .entsize = opts.entsize,
586 });
587 const shdr = &self.shdrs.items[index];
587 const off = self.findFreeSpace(opts.size, opts.alignment);588 const off = self.findFreeSpace(opts.size, opts.alignment);
588 log.debug("allocating '{s}' from 0x{x} to 0x{x} ", .{ opts.name, off, off + opts.size });589 log.debug("allocating '{s}' from 0x{x} to 0x{x} ", .{ opts.name, off, off + opts.size });
589 self.shdrs.appendAssumeCapacity(.{590 shdr.sh_offset = off;
590 .sh_name = sh_name,591 shdr.sh_size = opts.size;
591 .sh_type = opts.type,
592 .sh_flags = opts.flags,
593 .sh_addr = 0,
594 .sh_offset = off,
595 .sh_size = opts.size,
596 .sh_link = opts.link,
597 .sh_info = opts.info,
598 .sh_addralign = opts.alignment,
599 .sh_entsize = opts.entsize,
600 });
601 self.shdr_table_dirty = true;
602 return index;592 return index;
603}593}
604594
...@@ -3532,7 +3522,6 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3532,7 +3522,6 @@ fn initSyntheticSections(self: *Elf) !void {
3532 .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym),3522 .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym),
3533 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),3523 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),
3534 });3524 });
3535 self.shdr_table_dirty = true;
3536 }3525 }
3537}3526}
35383527
...@@ -4016,11 +4005,12 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 {...@@ -4016,11 +4005,12 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 {
4016 .sh_addr = 0,4005 .sh_addr = 0,
4017 .sh_offset = 0,4006 .sh_offset = 0,
4018 .sh_size = 0,4007 .sh_size = 0,
4019 .sh_link = 0,4008 .sh_link = opts.link,
4020 .sh_info = opts.info,4009 .sh_info = opts.info,
4021 .sh_addralign = opts.addralign,4010 .sh_addralign = opts.addralign,
4022 .sh_entsize = opts.entsize,4011 .sh_entsize = opts.entsize,
4023 };4012 };
4013 self.shdr_table_dirty = true;
4024 return index;4014 return index;
4025}4015}
40264016