@@ -110,54 +110,57 @@ static void rebind_symbols_for_image(struct rebindings_entry *rebindings,
110
110
if (dladdr (header , & info ) == 0 ) {
111
111
return ;
112
112
}
113
- uintptr_t cur = ( uintptr_t ) header + sizeof ( mach_header_t );
113
+
114
114
segment_command_t * cur_seg_cmd ;
115
115
segment_command_t * linkedit_segment = NULL ;
116
- section_t * lazy_symbols = NULL ;
117
- section_t * non_lazy_symbols = NULL ;
118
116
struct symtab_command * symtab_cmd = NULL ;
119
117
struct dysymtab_command * dysymtab_cmd = NULL ;
118
+
119
+ uintptr_t cur = (uintptr_t )header + sizeof (mach_header_t );
120
120
for (uint i = 0 ; i < header -> ncmds ; i ++ , cur += cur_seg_cmd -> cmdsize ) {
121
121
cur_seg_cmd = (segment_command_t * )cur ;
122
122
if (cur_seg_cmd -> cmd == LC_SEGMENT_ARCH_DEPENDENT ) {
123
123
if (strcmp (cur_seg_cmd -> segname , SEG_LINKEDIT ) == 0 ) {
124
124
linkedit_segment = cur_seg_cmd ;
125
- continue ;
126
- }
127
- if (strcmp (cur_seg_cmd -> segname , SEG_DATA ) != 0 ) {
128
- continue ;
129
- }
130
- for (uint j = 0 ; j < cur_seg_cmd -> nsects ; j ++ ) {
131
- section_t * sect =
132
- (section_t * )(cur + sizeof (segment_command_t )) + j ;
133
- if ((sect -> flags & SECTION_TYPE ) == S_LAZY_SYMBOL_POINTERS ) {
134
- lazy_symbols = sect ;
135
- }
136
- if ((sect -> flags & SECTION_TYPE ) == S_NON_LAZY_SYMBOL_POINTERS ) {
137
- non_lazy_symbols = sect ;
138
- }
139
125
}
140
126
} else if (cur_seg_cmd -> cmd == LC_SYMTAB ) {
141
127
symtab_cmd = (struct symtab_command * )cur_seg_cmd ;
142
128
} else if (cur_seg_cmd -> cmd == LC_DYSYMTAB ) {
143
129
dysymtab_cmd = (struct dysymtab_command * )cur_seg_cmd ;
144
130
}
145
131
}
132
+
146
133
if (!symtab_cmd || !dysymtab_cmd || !linkedit_segment ||
147
134
!dysymtab_cmd -> nindirectsyms ) {
148
135
return ;
149
136
}
137
+
150
138
// Find base symbol/string table addresses
151
139
uintptr_t linkedit_base = (uintptr_t )slide + linkedit_segment -> vmaddr - linkedit_segment -> fileoff ;
152
140
nlist_t * symtab = (nlist_t * )(linkedit_base + symtab_cmd -> symoff );
153
141
char * strtab = (char * )(linkedit_base + symtab_cmd -> stroff );
142
+
154
143
// Get indirect symbol table (array of uint32_t indices into symbol table)
155
144
uint32_t * indirect_symtab = (uint32_t * )(linkedit_base + dysymtab_cmd -> indirectsymoff );
156
- if (lazy_symbols ) {
157
- perform_rebinding_with_section (rebindings , lazy_symbols , slide , symtab , strtab , indirect_symtab );
158
- }
159
- if (non_lazy_symbols ) {
160
- perform_rebinding_with_section (rebindings , non_lazy_symbols , slide , symtab , strtab , indirect_symtab );
145
+
146
+ cur = (uintptr_t )header + sizeof (mach_header_t );
147
+ for (uint i = 0 ; i < header -> ncmds ; i ++ , cur += cur_seg_cmd -> cmdsize ) {
148
+ cur_seg_cmd = (segment_command_t * )cur ;
149
+ if (cur_seg_cmd -> cmd == LC_SEGMENT_ARCH_DEPENDENT ) {
150
+ if (strcmp (cur_seg_cmd -> segname , SEG_DATA ) != 0 ) {
151
+ continue ;
152
+ }
153
+ for (uint j = 0 ; j < cur_seg_cmd -> nsects ; j ++ ) {
154
+ section_t * sect =
155
+ (section_t * )(cur + sizeof (segment_command_t )) + j ;
156
+ if ((sect -> flags & SECTION_TYPE ) == S_LAZY_SYMBOL_POINTERS ) {
157
+ perform_rebinding_with_section (rebindings , sect , slide , symtab , strtab , indirect_symtab );
158
+ }
159
+ if ((sect -> flags & SECTION_TYPE ) == S_NON_LAZY_SYMBOL_POINTERS ) {
160
+ perform_rebinding_with_section (rebindings , sect , slide , symtab , strtab , indirect_symtab );
161
+ }
162
+ }
163
+ }
161
164
}
162
165
}
163
166
@@ -193,4 +196,4 @@ int rebind_symbols(struct rebinding rebindings[], size_t rebindings_nel) {
193
196
}
194
197
}
195
198
return retval ;
196
- }
199
+ }
0 commit comments