authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-19 18:01:37+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-21 22:44:27+02:00
log9530b95afee9964ee88293cf36e0db882ab301e6
tree00085b37272579fdfc9db1ca14ba07a354738a42
parentcec475531022b0ed13e708f4991c197b2b838b2c

macho: refactor common codepath for collecting bindings from TableSection


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

src/link/MachO.zig+34-57
...@@ -3092,6 +3092,37 @@ fn collectRebaseData(self: *MachO, rebase: *Rebase) !void {...@@ -3092,6 +3092,37 @@ fn collectRebaseData(self: *MachO, rebase: *Rebase) !void {
3092 try rebase.finalize(gpa);3092 try rebase.finalize(gpa);
3093}3093}
30943094
3095fn collectBindDataFromTableSection(self: *MachO, sect_id: u8, bind: anytype, table: anytype) !void {
3096 const segment_index = self.sections.items(.segment_index)[sect_id];
3097
3098 try bind.entries.ensureUnusedCapacity(self.base.allocator, table.entries.items.len);
3099
3100 for (table.entries.items, 0..) |entry, i| {
3101 if (!table.lookup.contains(entry)) continue;
3102
3103 const bind_sym = self.getSymbol(entry);
3104 if (!bind_sym.undf()) continue;
3105
3106 const offset = i * @sizeOf(u64);
3107
3108 log.debug(" | bind at {x}, import('{s}') in dylib({d})", .{
3109 offset,
3110 self.getSymbolName(entry),
3111 @divTrunc(@bitCast(i16, bind_sym.n_desc), macho.N_SYMBOL_RESOLVER),
3112 });
3113 if (bind_sym.weakRef()) {
3114 log.debug(" | marking as weak ref ", .{});
3115 }
3116
3117 bind.entries.appendAssumeCapacity(.{
3118 .target = entry,
3119 .offset = offset,
3120 .segment_id = segment_index,
3121 .addend = 0,
3122 });
3123 }
3124}
3125
3095fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void {3126fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void {
3096 const gpa = self.base.allocator;3127 const gpa = self.base.allocator;
3097 const slice = self.sections.slice();3128 const slice = self.sections.slice();
...@@ -3134,67 +3165,13 @@ fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void {...@@ -3134,67 +3165,13 @@ fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void {
3134 }3165 }
31353166
3136 // Gather GOT pointers3167 // Gather GOT pointers
3137 try bind.entries.ensureUnusedCapacity(gpa, self.got_table.entries.items.len);3168 try self.collectBindDataFromTableSection(self.got_section_index.?, bind, self.got_table);
3138 const segment_index = self.sections.items(.segment_index)[self.got_section_index.?];
3139 for (self.got_table.entries.items, 0..) |entry, i| {
3140 if (!self.got_table.lookup.contains(entry)) continue;
3141 const sym = self.getSymbol(entry);
3142 if (!sym.undf()) continue;
3143 const offset = i * @sizeOf(u64);
3144 const bind_sym = self.getSymbol(entry);
3145 const bind_sym_name = self.getSymbolName(entry);
3146 const dylib_ordinal = @divTrunc(
3147 @bitCast(i16, bind_sym.n_desc),
3148 macho.N_SYMBOL_RESOLVER,
3149 );
3150 log.debug(" | bind at {x}, import('{s}') in dylib({d})", .{
3151 offset,
3152 bind_sym_name,
3153 dylib_ordinal,
3154 });
3155 bind.entries.appendAssumeCapacity(.{
3156 .target = entry,
3157 .offset = offset,
3158 .segment_id = segment_index,
3159 .addend = 0,
3160 });
3161 }
3162
3163 try bind.finalize(gpa, self);3169 try bind.finalize(gpa, self);
3164}3170}
31653171
3166fn collectLazyBindData(self: *MachO, bind: anytype) !void {3172fn collectLazyBindData(self: *MachO, bind: anytype) !void {
3167 const gpa = self.base.allocator;3173 try self.collectBindDataFromTableSection(self.la_symbol_ptr_section_index.?, bind, self.stub_table);
31683174 try bind.finalize(self.base.allocator, self);
3169 try bind.entries.ensureUnusedCapacity(gpa, self.stub_table.entries.items.len);
3170 const segment_index = self.sections.items(.segment_index)[self.la_symbol_ptr_section_index.?];
3171 for (self.stub_table.entries.items, 0..) |entry, i| {
3172 if (!self.stub_table.lookup.contains(entry)) continue;
3173 const bind_sym = self.getSymbol(entry);
3174 assert(bind_sym.undf());
3175 const bind_sym_name = self.getSymbolName(entry);
3176 const offset = i * @sizeOf(u64);
3177 const dylib_ordinal = @divTrunc(
3178 @bitCast(i16, bind_sym.n_desc),
3179 macho.N_SYMBOL_RESOLVER,
3180 );
3181 log.debug(" | bind at {x}, import('{s}') in dylib({d})", .{
3182 offset,
3183 bind_sym_name,
3184 dylib_ordinal,
3185 });
3186 if (bind_sym.weakRef()) {
3187 log.debug(" | marking as weak ref ", .{});
3188 }
3189 bind.entries.appendAssumeCapacity(.{
3190 .target = entry,
3191 .offset = offset,
3192 .segment_id = segment_index,
3193 .addend = 0,
3194 });
3195 }
3196
3197 try bind.finalize(gpa, self);
3198}3175}
31993176
3200fn collectExportData(self: *MachO, trie: *Trie) !void {3177fn collectExportData(self: *MachO, trie: *Trie) !void {