authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-04 12:37:45+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-04 16:58:45+01:00
logd67de87baa9ebc45f51e3a83fed761e03367f904
tree47512c95ce088c2d6ea5269b1fb3b9c7ab334847
parent73d76b26a22fca71636bd2c995de4919a1b6d343

dsym: add helper for accessing dwarf seg pointer


1 files changed, 11 insertions(+), 8 deletions(-)

src/link/MachO/DebugSymbols.zig+11-8
...@@ -126,7 +126,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, gpa: Allocator) !void {...@@ -126,7 +126,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, gpa: Allocator) !void {
126fn allocateSection(self: *DebugSymbols, sectname: []const u8, size: u64, alignment: u16) !u8 {126fn allocateSection(self: *DebugSymbols, sectname: []const u8, size: u64, alignment: u16) !u8 {
127 const gpa = self.base.base.allocator;127 const gpa = self.base.base.allocator;
128128
129 const segment = &self.segments.items[self.dwarf_segment_cmd_index.?];129 const segment = self.getDwarfSegmentPtr();
130 var sect = macho.section_64{130 var sect = macho.section_64{
131 .sectname = makeStaticString(sectname),131 .sectname = makeStaticString(sectname),
132 .segname = segment.segname,132 .segname = segment.segname,
...@@ -136,8 +136,6 @@ fn allocateSection(self: *DebugSymbols, sectname: []const u8, size: u64, alignme...@@ -136,8 +136,6 @@ fn allocateSection(self: *DebugSymbols, sectname: []const u8, size: u64, alignme
136 const alignment_pow_2 = try math.powi(u32, 2, alignment);136 const alignment_pow_2 = try math.powi(u32, 2, alignment);
137 const off = self.findFreeSpace(size, alignment_pow_2);137 const off = self.findFreeSpace(size, alignment_pow_2);
138138
139 assert(off + size <= segment.fileoff + segment.filesize); // TODO expand
140
141 log.debug("found {s},{s} section free space 0x{x} to 0x{x}", .{139 log.debug("found {s},{s} section free space 0x{x} to 0x{x}", .{
142 sect.segName(),140 sect.segName(),
143 sect.sectName(),141 sect.sectName(),
...@@ -169,7 +167,7 @@ fn detectAllocCollision(self: *DebugSymbols, start: u64, size: u64) ?u64 {...@@ -169,7 +167,7 @@ fn detectAllocCollision(self: *DebugSymbols, start: u64, size: u64) ?u64 {
169}167}
170168
171pub fn findFreeSpace(self: *DebugSymbols, object_size: u64, min_alignment: u64) u64 {169pub fn findFreeSpace(self: *DebugSymbols, object_size: u64, min_alignment: u64) u64 {
172 const segment = self.segments.items[self.dwarf_segment_cmd_index.?];170 const segment = self.getDwarfSegmentPtr();
173 var offset: u64 = segment.fileoff;171 var offset: u64 = segment.fileoff;
174 while (self.detectAllocCollision(offset, object_size)) |item_end| {172 while (self.detectAllocCollision(offset, object_size)) |item_end| {
175 offset = mem.alignForwardGeneric(u64, item_end, min_alignment);173 offset = mem.alignForwardGeneric(u64, item_end, min_alignment);
...@@ -248,7 +246,7 @@ pub fn flushModule(self: *DebugSymbols, allocator: Allocator, options: link.Opti...@@ -248,7 +246,7 @@ pub fn flushModule(self: *DebugSymbols, allocator: Allocator, options: link.Opti
248 }246 }
249247
250 {248 {
251 const dwarf_segment = &self.segments.items[self.dwarf_segment_cmd_index.?];249 const dwarf_segment = self.getDwarfSegmentPtr();
252 const debug_strtab_sect = &self.sections.items[self.debug_str_section_index.?];250 const debug_strtab_sect = &self.sections.items[self.debug_str_section_index.?];
253 if (self.debug_string_table_dirty or self.dwarf.strtab.items.len != debug_strtab_sect.size) {251 if (self.debug_string_table_dirty or self.dwarf.strtab.items.len != debug_strtab_sect.size) {
254 const allocated_size = self.allocatedSize(debug_strtab_sect.offset);252 const allocated_size = self.allocatedSize(debug_strtab_sect.offset);
...@@ -326,7 +324,7 @@ fn finalizeDwarfSegment(self: *DebugSymbols) void {...@@ -326,7 +324,7 @@ fn finalizeDwarfSegment(self: *DebugSymbols) void {
326 const last_seg = self.base.getLinkeditSegmentPtr();324 const last_seg = self.base.getLinkeditSegmentPtr();
327 break :blk last_seg.vmaddr + last_seg.vmsize;325 break :blk last_seg.vmaddr + last_seg.vmsize;
328 };326 };
329 const dwarf_segment = &self.segments.items[self.dwarf_segment_cmd_index.?];327 const dwarf_segment = self.getDwarfSegmentPtr();
330 const aligned_size = mem.alignForwardGeneric(328 const aligned_size = mem.alignForwardGeneric(
331 u64,329 u64,
332 dwarf_segment.filesize,330 dwarf_segment.filesize,
...@@ -419,7 +417,7 @@ fn writeHeader(self: *DebugSymbols, ncmds: u32, sizeofcmds: u32) !void {...@@ -419,7 +417,7 @@ fn writeHeader(self: *DebugSymbols, ncmds: u32, sizeofcmds: u32) !void {
419}417}
420418
421pub fn allocatedSize(self: *DebugSymbols, start: u64) u64 {419pub fn allocatedSize(self: *DebugSymbols, start: u64) u64 {
422 const seg = self.segments.items[self.dwarf_segment_cmd_index.?];420 const seg = self.getDwarfSegmentPtr();
423 assert(start >= seg.fileoff);421 assert(start >= seg.fileoff);
424 var min_pos: u64 = std.math.maxInt(u64);422 var min_pos: u64 = std.math.maxInt(u64);
425 for (self.sections.items) |section| {423 for (self.sections.items) |section| {
...@@ -549,7 +547,12 @@ pub fn getSectionIndexes(self: *DebugSymbols, segment_index: u8) struct { start:...@@ -549,7 +547,12 @@ pub fn getSectionIndexes(self: *DebugSymbols, segment_index: u8) struct { start:
549 return .{ .start = start, .end = start + nsects };547 return .{ .start = start, .end = start + nsects };
550}548}
551549
552pub fn getLinkeditSegmentPtr(self: *DebugSymbols) *macho.segment_command_64 {550fn getDwarfSegmentPtr(self: *DebugSymbols) *macho.segment_command_64 {
551 const index = self.dwarf_segment_cmd_index.?;
552 return &self.segments.items[index];
553}
554
555fn getLinkeditSegmentPtr(self: *DebugSymbols) *macho.segment_command_64 {
553 const index = self.linkedit_segment_cmd_index.?;556 const index = self.linkedit_segment_cmd_index.?;
554 return &self.segments.items[index];557 return &self.segments.items[index];
555}558}