Skip to content

Commit

Permalink
debugging: slides: update dlsym example
Browse files Browse the repository at this point in the history
It is way more convenient to pass directly RTLD_NEXT to dlsym instead of
a handle returned by dlopen, especially for the LD_PRELOAD use case:
developers then don't care about the exact file providing the targeted
symbol

Reported-by: Luca Ceresoli <[email protected]>
Signed-off-by: Alexis Lothoré <[email protected]>
  • Loading branch information
Tropicao committed Jan 7, 2025
1 parent f0ea04a commit e3ec1eb
Showing 1 changed file with 10 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,17 @@ \subsection{LD\_PRELOAD}

ssize_t read(int fd, void *data, size_t size)
{
size_t (*read_func)(int, void *, size_t);
void *handle;
char *error;

handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
if (!handle) {
fprintf(stderr, "Can not find overriden library\n");
return 0;
}
dlerror();
read_func = dlsym(handle, "read");
error = dlerror();
if (error) {
fprintf(stderr, "Can not find overriden symbol: %s\n", error);
return 0;
}
fprintf(stderr, "Trying to read %lu bytes to %p from file descriptor %d\n", size, data, fd);
return read_func(fd, data, size);
size_t (*read_func)(int, void *, size_t);
char *error;

read_func = dlsym(RTLD_NEXT, "read");
if (!read_func) {
fprintf(stderr, "Can not find read symbol: %s\n", dlerror());
return 0;
}
fprintf(stderr, "Trying to read %lu bytes to %p from file descriptor %d\n", size, data, fd);
return read_func(fd, data, size);
}

\end{minted}
\end{block}
\end{frame}
Expand Down

0 comments on commit e3ec1eb

Please sign in to comment.