7
7
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8
8
*
9
9
*/
10
+ #include " common.hpp"
10
11
#include " llvm/DebugInfo/Symbolize/DIPrinter.h"
11
12
#include " llvm/DebugInfo/Symbolize/Symbolize.h"
13
+ #include < link.h>
12
14
13
15
static llvm::symbolize::PrinterConfig GetPrinterConfig () {
14
16
llvm::symbolize::PrinterConfig Config;
@@ -20,6 +22,28 @@ static llvm::symbolize::PrinterConfig GetPrinterConfig() {
20
22
return Config;
21
23
}
22
24
25
+ static uintptr_t GetModuleBase (const char *ModuleName) {
26
+ uintptr_t Data = (uintptr_t )ModuleName;
27
+ int Result = dl_iterate_phdr (
28
+ [](struct dl_phdr_info *Info, size_t , void *Arg) {
29
+ uintptr_t *Data = (uintptr_t *)Arg;
30
+ const char *ModuleName = (const char *)(*Data);
31
+ if (strstr (Info->dlpi_name , ModuleName)) {
32
+ *Data = (uintptr_t )Info->dlpi_addr ;
33
+ return 1 ;
34
+ }
35
+ return 0 ;
36
+ },
37
+ (void *)&Data);
38
+
39
+ // If dl_iterate_phdr return 0, it means the module is main executable,
40
+ // its base address should be 0.
41
+ if (!Result) {
42
+ return 0 ;
43
+ }
44
+ return Data;
45
+ }
46
+
23
47
extern " C" {
24
48
25
49
void SymbolizeCode (const char *ModuleName, uint64_t ModuleOffset,
@@ -38,8 +62,8 @@ void SymbolizeCode(const char *ModuleName, uint64_t ModuleOffset,
38
62
llvm::symbolize::LLVMPrinter Printer (OS, EH, Config);
39
63
40
64
auto ResOrErr = Symbolizer.symbolizeInlinedCode (
41
- ModuleName,
42
- {ModuleOffset, llvm::object::SectionedAddress::UndefSection});
65
+ ModuleName, {ModuleOffset - GetModuleBase (ModuleName),
66
+ llvm::object::SectionedAddress::UndefSection});
43
67
44
68
if (!ResOrErr) {
45
69
return ;
0 commit comments