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 {
541541pub fn allocateAllocSection(self: *Elf, opts: AllocateAllocSectionOpts) error{OutOfMemory}!u16 {
542542 const gpa = self.base.allocator;
543543 const phdr = &self.phdrs.items[opts.phdr_index];
544 const index = @as(u16, @intCast(self.shdrs.items.len));
545 try self.shdrs.ensureUnusedCapacity(gpa, 1);
546 const sh_name = try self.shstrtab.insert(gpa, opts.name);
544 const index = try self.addSection(.{
545 .name = opts.name,
546 .type = opts.type,
547 .flags = opts.flags,
548 .addralign = opts.alignment,
549 });
550 const shdr = &self.shdrs.items[index];
547551 try self.phdr_to_shdr_table.putNoClobber(gpa, index, opts.phdr_index);
548552 log.debug("allocating '{s}' in phdr({d}) from 0x{x} to 0x{x} (0x{x} - 0x{x})", .{
549553 opts.name,
......@@ -553,19 +557,9 @@ pub fn allocateAllocSection(self: *Elf, opts: AllocateAllocSectionOpts) error{Ou
553557 phdr.p_vaddr,
554558 phdr.p_vaddr + phdr.p_memsz,
555559 });
556 self.shdrs.appendAssumeCapacity(.{
557 .sh_name = sh_name,
558 .sh_type = opts.type,
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;
560 shdr.sh_addr = phdr.p_vaddr;
561 shdr.sh_offset = phdr.p_offset;
562 shdr.sh_size = phdr.p_memsz;
569563 return index;
570564}
571565
......@@ -581,24 +575,20 @@ const AllocateNonAllocSectionOpts = struct {
581575};
582576
583577fn allocateNonAllocSection(self: *Elf, opts: AllocateNonAllocSectionOpts) error{OutOfMemory}!u16 {
584 const index = @as(u16, @intCast(self.shdrs.items.len));
585 try self.shdrs.ensureUnusedCapacity(self.base.allocator, 1);
586 const sh_name = try self.shstrtab.insert(self.base.allocator, opts.name);
578 const index = try self.addSection(.{
579 .name = 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];
587588 const off = self.findFreeSpace(opts.size, opts.alignment);
588589 log.debug("allocating '{s}' from 0x{x} to 0x{x} ", .{ opts.name, off, off + opts.size });
589 self.shdrs.appendAssumeCapacity(.{
590 .sh_name = sh_name,
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;
590 shdr.sh_offset = off;
591 shdr.sh_size = opts.size;
602592 return index;
603593}
604594
......@@ -3532,7 +3522,6 @@ fn initSyntheticSections(self: *Elf) !void {
35323522 .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym),
35333523 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),
35343524 });
3535 self.shdr_table_dirty = true;
35363525 }
35373526}
35383527
......@@ -4016,11 +4005,12 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 {
40164005 .sh_addr = 0,
40174006 .sh_offset = 0,
40184007 .sh_size = 0,
4019 .sh_link = 0,
4008 .sh_link = opts.link,
40204009 .sh_info = opts.info,
40214010 .sh_addralign = opts.addralign,
40224011 .sh_entsize = opts.entsize,
40234012 };
4013 self.shdr_table_dirty = true;
40244014 return index;
40254015}
40264016