Skip to content

Commit a93c514

Browse files
montywivuvova
authored andcommitted
Fixed my_addr_resolve
When a server is compiled with -fPIE, my_addr_resolve needs to subtract the info.dli_fbase from symbol addresses in memory for addr2line to recognize them. When a server is compiled without -fPIE, my_addr_resolve should not do it. Unfortunately not all compilers define __PIE__ when -fPIE was used (e.g. older gcc doesn't), so we have to resort to run-time detection.
1 parent 942a5a8 commit a93c514

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

mysys/my_addr_resolve.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ static pid_t pid;
170170
static char addr2line_binary[1024];
171171
static char output[1024];
172172
static struct pollfd poll_fds;
173+
static void *addr_offset;
173174

174175
int start_addr2line_fork(const char *binary_path)
175176
{
@@ -297,7 +298,6 @@ static int addr_resolve(void *ptr, my_addr_loc *loc)
297298
int my_addr_resolve(void *ptr, my_addr_loc *loc)
298299
{
299300
Dl_info info;
300-
int error;
301301

302302
if (!dladdr(ptr, &info))
303303
return 1;
@@ -307,7 +307,7 @@ int my_addr_resolve(void *ptr, my_addr_loc *loc)
307307
/*
308308
We use dli_fname in case the path is longer than the length of
309309
our static string. We don't want to allocate anything
310-
dynamicaly here as we are in a "crashed" state.
310+
dynamically here as we are in a "crashed" state.
311311
*/
312312
if (start_addr2line_fork(info.dli_fname))
313313
{
@@ -318,10 +318,22 @@ int my_addr_resolve(void *ptr, my_addr_loc *loc)
318318
}
319319
/* Save result for future comparisons. */
320320
strnmov(addr2line_binary, info.dli_fname, sizeof(addr2line_binary));
321+
322+
/*
323+
Check if we should use info.dli_fbase as an offset or not
324+
for the base program. This is depending on if the compilation is
325+
done with PIE or not.
326+
*/
327+
addr_offset= info.dli_fbase;
328+
#ifndef __PIE__
329+
if (strcmp(info.dli_fname, my_progname) == 0 &&
330+
addr_resolve((void*) my_addr_resolve, loc) == 0 &&
331+
strcmp(loc->func, "my_addr_resolve") == 0)
332+
addr_offset= 0;
333+
#endif
321334
}
322-
if (!(error= addr_resolve((void*) (ptr - info.dli_fbase), loc)))
323-
return 0;
324-
return error;
335+
336+
return addr_resolve((void*) (ptr - addr_offset), loc);
325337
}
326338

327339

0 commit comments

Comments
 (0)