| ... | ... | @@ -541,9 +541,13 @@ const AllocateAllocSectionOpts = struct { |
| 541 | 541 | pub fn allocateAllocSection(self: *Elf, opts: AllocateAllocSectionOpts) error{OutOfMemory}!u16 { |
| 542 | 542 | const gpa = self.base.allocator; |
| 543 | 543 | 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]; |
| 547 | 551 | try self.phdr_to_shdr_table.putNoClobber(gpa, index, opts.phdr_index); |
| 548 | 552 | log.debug("allocating '{s}' in phdr({d}) from 0x{x} to 0x{x} (0x{x} - 0x{x})", .{ |
| 549 | 553 | opts.name, |
| ... | ... | @@ -553,19 +557,9 @@ pub fn allocateAllocSection(self: *Elf, opts: AllocateAllocSectionOpts) error{Ou |
| 553 | 557 | phdr.p_vaddr, |
| 554 | 558 | phdr.p_vaddr + phdr.p_memsz, |
| 555 | 559 | }); |
| 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; |
| 569 | 563 | return index; |
| 570 | 564 | } |
| 571 | 565 | |
| ... | ... | @@ -581,24 +575,20 @@ const AllocateNonAllocSectionOpts = struct { |
| 581 | 575 | }; |
| 582 | 576 | |
| 583 | 577 | fn 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]; |
| 587 | 588 | const off = self.findFreeSpace(opts.size, opts.alignment); |
| 588 | 589 | 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; |
| 602 | 592 | return index; |
| 603 | 593 | } |
| 604 | 594 | |
| ... | ... | @@ -3532,7 +3522,6 @@ fn initSyntheticSections(self: *Elf) !void { |
| 3532 | 3522 | .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym), |
| 3533 | 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 | } |
| 3538 | 3527 | |
| ... | ... | @@ -4016,11 +4005,12 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 { |
| 4016 | 4005 | .sh_addr = 0, |
| 4017 | 4006 | .sh_offset = 0, |
| 4018 | 4007 | .sh_size = 0, |
| 4019 | | .sh_link = 0, |
| 4008 | .sh_link = opts.link, |
| 4020 | 4009 | .sh_info = opts.info, |
| 4021 | 4010 | .sh_addralign = opts.addralign, |
| 4022 | 4011 | .sh_entsize = opts.entsize, |
| 4023 | 4012 | }; |
| 4013 | self.shdr_table_dirty = true; |
| 4024 | 4014 | return index; |
| 4025 | 4015 | } |
| 4026 | 4016 | |