Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Translate virtual addresses to binary addresses for shared libs as well.
Quoting https://refspecs.linuxbase.org/elf/elf.pdf: ``` Symbol table entries for different object file types have slightly different interpretations for the st_value member. - In relocatable files, st_value holds alignment constraints for a symbol whose section index is SHN_COMMON. - In relocatable files, st_value holds a section offset for a defined symbol. That is, st_value is an offset from the beginning of the section that st_shndx identifies. - In executable and shared object files, st_value holds a virtual address. To make these files' symbols more useful for the dynamic linker, the section offset (file interpretation) gives way to a virtual address (memory interpretation) for which the section number is irrelevant. ``` This is a problem in practice as well. I run into this while tracing shared libraries on Android with bpftrace. Some of them have text sections mmapped at different offset than file offset which results in probes being placed at wrong offsets.
- Loading branch information
1 parent
f35dae0
commit 007ee49
Showing
3 changed files
with
54 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env python | ||
# | ||
# USAGE: test_uprobe2.py | ||
# | ||
# Copyright 2020 Facebook, Inc | ||
# Licensed under the Apache License, Version 2.0 (the "License") | ||
|
||
from bcc import BPF | ||
from unittest import main, TestCase | ||
from subprocess import Popen, PIPE | ||
from tempfile import NamedTemporaryFile | ||
|
||
|
||
class TestUprobes(TestCase): | ||
def setUp(self): | ||
lib_text = b""" | ||
__attribute__((__visibility__("default"))) void fun() | ||
{ | ||
} | ||
""" | ||
self.bpf_text = """ | ||
int trace_fun_call(void *ctx) {{ | ||
return 1; | ||
}} | ||
""" | ||
# Compile and run the application | ||
self.ftemp = NamedTemporaryFile(delete=False) | ||
self.ftemp.close() | ||
comp = Popen([ | ||
"gcc", | ||
"-x", "c", | ||
"-shared", | ||
"-Wl,-Ttext-segment,0x2000000", | ||
"-o", self.ftemp.name, | ||
"-" | ||
], stdin=PIPE) | ||
comp.stdin.write(lib_text) | ||
comp.stdin.close() | ||
self.assertEqual(comp.wait(), 0) | ||
|
||
def test_attach1(self): | ||
b = BPF(text=self.bpf_text) | ||
b.attach_uprobe(name=self.ftemp.name, sym="fun", fn_name="trace_fun_call") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |