dwarf_extractor.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Copyright (C) 2021 Ant Group. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "lldb/API/SBBlock.h"
  6. #include "lldb/API/SBCompileUnit.h"
  7. #include "lldb/API/SBCommandReturnObject.h"
  8. #include "lldb/API/SBCommandInterpreter.h"
  9. #include "lldb/API/SBBreakpointLocation.h"
  10. #include "lldb/API/SBDebugger.h"
  11. #include "lldb/API//SBFunction.h"
  12. #include "lldb/API//SBModule.h"
  13. #include "lldb/API//SBProcess.h"
  14. #include "lldb/API//SBStream.h"
  15. #include "lldb/API//SBSymbol.h"
  16. #include "lldb/API//SBTarget.h"
  17. #include "lldb/API//SBThread.h"
  18. #include "lldb/API/SBDeclaration.h"
  19. #include "dwarf_extractor.h"
  20. #include "../aot_llvm.h"
  21. #include "bh_log.h"
  22. #include "../../aot/aot_runtime.h"
  23. #include "llvm/BinaryFormat/Dwarf.h"
  24. using namespace lldb;
  25. typedef struct dwarf_extractor {
  26. SBDebugger debugger;
  27. SBTarget target;
  28. SBModule module;
  29. } dwarf_extractor;
  30. #define TO_HANDLE(extractor) (dwarf_extractor_handle_t)(extractor)
  31. #define TO_EXTACTOR(handle) (dwarf_extractor *)(handle)
  32. static bool is_debugger_initialized;
  33. dwarf_extractor_handle_t
  34. create_dwarf_extractor(AOTCompData *comp_data, char *file_name)
  35. {
  36. char *arch = NULL;
  37. char *platform = NULL;
  38. dwarf_extractor *extractor = NULL;
  39. //__attribute__((constructor)) may be better?
  40. if (!is_debugger_initialized) {
  41. SBError error = SBDebugger::InitializeWithErrorHandling();
  42. if (error.Fail()) {
  43. LOG_ERROR("Init Dwarf Debugger failed");
  44. return TO_HANDLE(NULL);
  45. }
  46. is_debugger_initialized = true;
  47. }
  48. SBError error;
  49. SBFileSpec exe_file_spec(file_name, true);
  50. if (!(extractor = new dwarf_extractor())) {
  51. LOG_ERROR("Create Dwarf Extractor error: failed to allocate memory");
  52. goto fail3;
  53. }
  54. extractor->debugger = SBDebugger::Create();
  55. if (!extractor->debugger.IsValid()) {
  56. LOG_ERROR("Create Dwarf Debugger failed");
  57. goto fail2;
  58. }
  59. extractor->target = extractor->debugger.CreateTarget(
  60. file_name, arch, platform, false, error);
  61. if (!error.Success()) {
  62. LOG_ERROR("Create Dwarf target failed:%s", error.GetCString());
  63. goto fail1;
  64. }
  65. if (!extractor->target.IsValid()) {
  66. LOG_ERROR("Create Dwarf target not valid");
  67. goto fail1;
  68. }
  69. extractor->module = extractor->target.FindModule(exe_file_spec);
  70. comp_data->extractor = TO_HANDLE(extractor);
  71. return TO_HANDLE(extractor);
  72. fail1:
  73. SBDebugger::Destroy(extractor->debugger);
  74. fail2:
  75. wasm_runtime_free(extractor);
  76. fail3:
  77. return TO_HANDLE(NULL);
  78. }
  79. void
  80. destroy_dwarf_extractor(dwarf_extractor_handle_t handle)
  81. {
  82. dwarf_extractor *extractor = TO_EXTACTOR(handle);
  83. if (!extractor)
  84. return;
  85. extractor->debugger.DeleteTarget(extractor->target);
  86. SBDebugger::Destroy(extractor->debugger);
  87. delete extractor;
  88. SBDebugger::Terminate();
  89. is_debugger_initialized = false;
  90. }
  91. LLVMMetadataRef
  92. dwarf_gen_file_info(const AOTCompContext *comp_ctx)
  93. {
  94. dwarf_extractor *extractor;
  95. int units_number;
  96. LLVMMetadataRef file_info = NULL;
  97. const char *file_name;
  98. const char *dir_name;
  99. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  100. return NULL;
  101. units_number = extractor->module.GetNumCompileUnits();
  102. if (units_number > 0) {
  103. SBCompileUnit compile_unit = extractor->module.GetCompileUnitAtIndex(0);
  104. auto filespec = compile_unit.GetFileSpec();
  105. file_name = filespec.GetFilename();
  106. dir_name = filespec.GetDirectory();
  107. if (file_name || dir_name) {
  108. file_info = LLVMDIBuilderCreateFile(
  109. comp_ctx->debug_builder, file_name,
  110. file_name ? strlen(file_name) : 0, dir_name,
  111. dir_name ? strlen(dir_name) : 0);
  112. }
  113. }
  114. return file_info;
  115. }
  116. #if 0
  117. void
  118. dwarf_gen_mock_vm_info(AOTCompContext *comp_ctx)
  119. {
  120. LLVMMetadataRef file_info = NULL;
  121. LLVMMetadataRef comp_unit = NULL;
  122. file_info = LLVMDIBuilderCreateFile(comp_ctx->debug_builder,
  123. "ant_runtime_mock.c", 18, ".", 1);
  124. comp_unit = LLVMDIBuilderCreateCompileUnit(
  125. comp_ctx->debug_builder, LLVMDWARFSourceLanguageC, file_info,
  126. "WAMR AoT compiler", 12, 0, NULL, 0, 1, NULL, 0, LLVMDWARFEmissionFull, 0, 0,
  127. 0, "/", 1, "", 0);
  128. LLVMTypeRef ParamTys[] = {
  129. LLVMVoidType(),
  130. };
  131. LLVMTypeRef FuncTy = LLVMFunctionType(LLVMVoidType(), ParamTys, 0, 0);
  132. LLVMValueRef Function =
  133. LLVMAddFunction(comp_ctx->module, "ant_runtime_mock", FuncTy);
  134. LLVMMetadataRef ParamTypes[0];
  135. LLVMMetadataRef FunctionTy = LLVMDIBuilderCreateSubroutineType(
  136. comp_ctx->debug_builder, file_info, ParamTypes, 0, LLVMDIFlagZero);
  137. /* 0x0015 is subroutine_type */
  138. LLVMMetadataRef ReplaceableFunctionMetadata =
  139. LLVMDIBuilderCreateReplaceableCompositeType(
  140. comp_ctx->debug_builder, 0x15, "ant_runtime_mock", 16, file_info,
  141. file_info, 2, 0, 0, 0, LLVMDIFlagFwdDecl, "", 0);
  142. LLVMMetadataRef FunctionMetadata = LLVMDIBuilderCreateFunction(
  143. comp_ctx->debug_builder, file_info, "ant_runtime_mock", 16,
  144. "ant_runtime_mock", 16, file_info, 2, FunctionTy, true, true, 2, LLVMDIFlagZero,
  145. false);
  146. LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata,
  147. FunctionMetadata);
  148. LLVMSetSubprogram(Function, FunctionMetadata);
  149. comp_ctx->vm_debug_comp_unit = comp_unit;
  150. comp_ctx->vm_debug_file = file_info;
  151. comp_ctx->vm_debug_func = FunctionMetadata;
  152. }
  153. #endif
  154. LLVMMetadataRef
  155. dwarf_gen_comp_unit_info(const AOTCompContext *comp_ctx)
  156. {
  157. dwarf_extractor *extractor;
  158. int units_number;
  159. LLVMMetadataRef comp_unit = NULL;
  160. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  161. return NULL;
  162. units_number = extractor->module.GetNumCompileUnits();
  163. if (units_number > 0) {
  164. SBCompileUnit compile_unit = extractor->module.GetCompileUnitAtIndex(0);
  165. auto lang_type = compile_unit.GetLanguage();
  166. comp_unit = LLVMDIBuilderCreateCompileUnit(
  167. comp_ctx->debug_builder, LLDB_TO_LLVM_LANG_TYPE(lang_type),
  168. comp_ctx->debug_file, "WAMR AoT compiler", 12, 0, NULL, 0, 1, NULL,
  169. 0, LLVMDWARFEmissionFull, 0, 0, 0, "/", 1, "", 0);
  170. }
  171. return comp_unit;
  172. }
  173. static LLVMDWARFTypeEncoding
  174. lldb_get_basic_type_encoding(BasicType basic_type)
  175. {
  176. LLVMDWARFTypeEncoding encoding = 0;
  177. switch (basic_type) {
  178. case eBasicTypeUnsignedChar:
  179. encoding = llvm::dwarf::DW_ATE_unsigned_char;
  180. break;
  181. case eBasicTypeSignedChar:
  182. encoding = llvm::dwarf::DW_ATE_signed_char;
  183. break;
  184. case eBasicTypeUnsignedInt:
  185. case eBasicTypeUnsignedLong:
  186. case eBasicTypeUnsignedLongLong:
  187. case eBasicTypeUnsignedWChar:
  188. case eBasicTypeUnsignedInt128:
  189. case eBasicTypeUnsignedShort:
  190. encoding = llvm::dwarf::DW_ATE_unsigned;
  191. break;
  192. case eBasicTypeInt:
  193. case eBasicTypeLong:
  194. case eBasicTypeLongLong:
  195. case eBasicTypeWChar:
  196. case eBasicTypeInt128:
  197. case eBasicTypeShort:
  198. encoding = llvm::dwarf::DW_ATE_signed;
  199. break;
  200. case eBasicTypeBool:
  201. encoding = llvm::dwarf::DW_ATE_boolean;
  202. break;
  203. case eBasicTypeHalf:
  204. case eBasicTypeFloat:
  205. case eBasicTypeDouble:
  206. case eBasicTypeLongDouble:
  207. encoding = llvm::dwarf::DW_ATE_float;
  208. break;
  209. default:
  210. break;
  211. }
  212. return encoding;
  213. }
  214. static LLVMMetadataRef
  215. lldb_type_to_type_dbi(const AOTCompContext *comp_ctx, SBType &type)
  216. {
  217. LLVMMetadataRef type_info = NULL;
  218. BasicType basic_type = type.GetBasicType();
  219. uint64_t bit_size = type.GetByteSize() * 8;
  220. LLVMDIBuilderRef DIB = comp_ctx->debug_builder;
  221. LLVMDWARFTypeEncoding encoding;
  222. if (basic_type != eBasicTypeInvalid) {
  223. encoding = lldb_get_basic_type_encoding(basic_type);
  224. type_info = LLVMDIBuilderCreateBasicType(
  225. DIB, type.GetName(), strlen(type.GetName()), bit_size, encoding,
  226. LLVMDIFlagZero);
  227. }
  228. else if (type.IsPointerType()) {
  229. SBType pointee_type = type.GetPointeeType();
  230. type_info = LLVMDIBuilderCreatePointerType(
  231. DIB, lldb_type_to_type_dbi(comp_ctx, pointee_type), bit_size, 0, 0,
  232. "", 0);
  233. }
  234. return type_info;
  235. }
  236. static LLVMMetadataRef
  237. lldb_function_to_function_dbi(const AOTCompContext *comp_ctx,
  238. SBSymbolContext &sc,
  239. const AOTFuncContext *func_ctx)
  240. {
  241. SBFunction function(sc.GetFunction());
  242. const char *function_name = function.GetName();
  243. const char *link_name = function.GetName();
  244. SBTypeList function_args = function.GetType().GetFunctionArgumentTypes();
  245. SBType return_type = function.GetType().GetFunctionReturnType();
  246. const size_t num_function_args = function_args.GetSize();
  247. dwarf_extractor *extractor;
  248. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  249. return NULL;
  250. LLVMDIBuilderRef DIB = comp_ctx->debug_builder;
  251. LLVMMetadataRef File = comp_ctx->debug_file; /* a fallback */
  252. LLVMMetadataRef ParamTypes[num_function_args + 1];
  253. ParamTypes[0] = lldb_type_to_type_dbi(comp_ctx, return_type);
  254. for (uint32_t function_arg_idx = 0; function_arg_idx < num_function_args;
  255. ++function_arg_idx) {
  256. SBType function_arg_type =
  257. function_args.GetTypeAtIndex(function_arg_idx);
  258. if (function_arg_type.IsValid()) {
  259. ParamTypes[function_arg_idx + 1] =
  260. lldb_type_to_type_dbi(comp_ctx, function_arg_type);
  261. if (ParamTypes[function_arg_idx + 1] == NULL) {
  262. LOG_WARNING(
  263. "func %s arg %" PRIu32
  264. " has a type not implemented by lldb_type_to_type_dbi",
  265. function_name, function_arg_idx);
  266. }
  267. }
  268. else {
  269. LOG_WARNING("func %s arg %" PRIu32 ": GetTypeAtIndex failed",
  270. function_name, function_arg_idx);
  271. ParamTypes[function_arg_idx + 1] = NULL;
  272. }
  273. }
  274. auto compile_unit = sc.GetCompileUnit();
  275. auto file_spec = compile_unit.GetFileSpec();
  276. const char *file_name = file_spec.GetFilename();
  277. const char *dir_name = file_spec.GetDirectory();
  278. LLVMMetadataRef file_info = NULL;
  279. if (file_name || dir_name) {
  280. file_info =
  281. LLVMDIBuilderCreateFile(comp_ctx->debug_builder, file_name,
  282. file_name ? strlen(file_name) : 0, dir_name,
  283. dir_name ? strlen(dir_name) : 0);
  284. }
  285. if (file_info) {
  286. File = file_info;
  287. }
  288. LLVMMetadataRef FunctionTy = LLVMDIBuilderCreateSubroutineType(
  289. DIB, File, ParamTypes, num_function_args + 1, LLVMDIFlagZero);
  290. auto line_entry = sc.GetLineEntry();
  291. LLVMMetadataRef ReplaceableFunctionMetadata =
  292. LLVMDIBuilderCreateReplaceableCompositeType(
  293. DIB, 0x15, function_name, strlen(function_name), File, File,
  294. line_entry.GetLine(), 0, 0, 0, LLVMDIFlagFwdDecl, "", 0);
  295. LLVMMetadataRef FunctionMetadata = LLVMDIBuilderCreateFunction(
  296. DIB, File, function_name, strlen(function_name), link_name,
  297. strlen(link_name), File, line_entry.GetLine(), FunctionTy, true, true,
  298. line_entry.GetLine(), LLVMDIFlagZero, false);
  299. LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata,
  300. FunctionMetadata);
  301. LLVMSetSubprogram(func_ctx->func, FunctionMetadata);
  302. LLVMMetadataRef ParamExpression =
  303. LLVMDIBuilderCreateExpression(DIB, NULL, 0);
  304. auto variable_list =
  305. function.GetBlock().GetVariables(extractor->target, true, false, false);
  306. unsigned int variable_offset = 0;
  307. if (num_function_args != variable_list.GetSize()) {
  308. // A hack to detect C++ "this" pointer.
  309. //
  310. // REVISIT: is there a more reliable way?
  311. // At the DWARF level, we can probably look at DW_AT_object_pointer
  312. // and DW_AT_artificial. I'm not sure how it can be done via the
  313. // LLDB API though.
  314. if (num_function_args + 1 == variable_list.GetSize()) {
  315. SBValue variable(variable_list.GetValueAtIndex(0));
  316. const char *varname = variable.GetName();
  317. if (varname != NULL && !strcmp(varname, "this")) {
  318. variable_offset = 1;
  319. }
  320. }
  321. if (!variable_offset) {
  322. LOG_ERROR("function args number dismatch!:function %s %s value "
  323. "number=%d, function args=%d",
  324. function_name, function.GetMangledName(),
  325. variable_list.GetSize(), num_function_args);
  326. }
  327. }
  328. LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation(
  329. comp_ctx->context, line_entry.GetLine(), 0, FunctionMetadata, NULL);
  330. // TODO:change to void * or WasmExenv * ?
  331. LLVMMetadataRef voidtype =
  332. LLVMDIBuilderCreateBasicType(DIB, "void", 4, 0, 0, LLVMDIFlagZero);
  333. LLVMMetadataRef voidpionter =
  334. LLVMDIBuilderCreatePointerType(DIB, voidtype, 64, 0, 0, "void *", 6);
  335. LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
  336. DIB, FunctionMetadata, "exenv", 5, 1,
  337. File, // starts form 1, and 1 is exenv,
  338. line_entry.GetLine(), voidpionter, true, LLVMDIFlagZero);
  339. LLVMValueRef Param = LLVMGetParam(func_ctx->func, 0);
  340. LLVMBasicBlockRef block_curr = LLVMGetEntryBasicBlock(func_ctx->func);
  341. LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar, ParamExpression,
  342. ParamLocation, block_curr);
  343. for (uint32_t function_arg_idx = 0; function_arg_idx < num_function_args;
  344. ++function_arg_idx) {
  345. uint32_t variable_idx = variable_offset + function_arg_idx;
  346. SBValue variable(variable_list.GetValueAtIndex(variable_idx));
  347. if (variable.IsValid() && ParamTypes[function_arg_idx + 1] != NULL) {
  348. SBDeclaration dec(variable.GetDeclaration());
  349. auto valtype = variable.GetType();
  350. LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation(
  351. comp_ctx->context, dec.GetLine(), dec.GetColumn(),
  352. FunctionMetadata, NULL);
  353. const char *varname = variable.GetName();
  354. LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
  355. DIB, FunctionMetadata, varname, varname ? strlen(varname) : 0,
  356. variable_idx + 1 + 1,
  357. File, // starts form 1, and 1 is exenv,
  358. dec.GetLine(), ParamTypes[function_arg_idx + 1], true,
  359. LLVMDIFlagZero);
  360. LLVMValueRef Param = LLVMGetParam(func_ctx->func, variable_idx + 1);
  361. LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar,
  362. ParamExpression, ParamLocation,
  363. block_curr);
  364. }
  365. }
  366. return FunctionMetadata;
  367. }
  368. LLVMMetadataRef
  369. dwarf_gen_func_info(const AOTCompContext *comp_ctx,
  370. const AOTFuncContext *func_ctx)
  371. {
  372. LLVMMetadataRef func_info = NULL;
  373. dwarf_extractor *extractor;
  374. uint64_t vm_offset;
  375. AOTFunc *func = func_ctx->aot_func;
  376. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  377. return NULL;
  378. // A code address in DWARF for WebAssembly is the offset of an
  379. // instruction relative within the Code section of the WebAssembly file.
  380. // For this reason Section::GetFileAddress() must return zero for the
  381. // Code section. (refert to ObjectFileWasm.cpp)
  382. vm_offset = func->code - comp_ctx->comp_data->wasm_module->buf_code;
  383. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  384. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  385. | eSymbolContextLineEntry));
  386. if (sc.IsValid()) {
  387. SBFunction function(sc.GetFunction());
  388. if (function.IsValid()) {
  389. func_info = lldb_function_to_function_dbi(comp_ctx, sc, func_ctx);
  390. }
  391. }
  392. return func_info;
  393. }
  394. void
  395. dwarf_get_func_name(const AOTCompContext *comp_ctx,
  396. const AOTFuncContext *func_ctx, char *name, int len)
  397. {
  398. LLVMMetadataRef func_info = NULL;
  399. dwarf_extractor *extractor;
  400. uint64_t vm_offset;
  401. AOTFunc *func = func_ctx->aot_func;
  402. name[0] = '\0';
  403. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  404. return;
  405. // A code address in DWARF for WebAssembly is the offset of an
  406. // instruction relative within the Code section of the WebAssembly file.
  407. // For this reason Section::GetFileAddress() must return zero for the
  408. // Code section. (refert to ObjectFileWasm.cpp)
  409. vm_offset = func->code - comp_ctx->comp_data->wasm_module->buf_code;
  410. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  411. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  412. | eSymbolContextLineEntry));
  413. if (sc.IsValid()) {
  414. SBFunction function(sc.GetFunction());
  415. if (function.IsValid()) {
  416. bh_strcpy_s(name, len, function.GetName());
  417. }
  418. }
  419. }
  420. LLVMMetadataRef
  421. dwarf_gen_location(const AOTCompContext *comp_ctx,
  422. const AOTFuncContext *func_ctx, uint64_t vm_offset)
  423. {
  424. LLVMMetadataRef location_info = NULL;
  425. dwarf_extractor *extractor;
  426. AOTFunc *func = func_ctx->aot_func;
  427. if (func_ctx->debug_func == NULL)
  428. return NULL;
  429. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  430. return NULL;
  431. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  432. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  433. | eSymbolContextLineEntry));
  434. if (sc.IsValid()) {
  435. // TODO:need to check if the vm_offset is belong to
  436. SBFunction function(sc.GetFunction());
  437. if (function.IsValid()) {
  438. uint64_t start = func_ctx->aot_func->code
  439. - comp_ctx->comp_data->wasm_module->buf_code;
  440. uint64_t end = func_ctx->aot_func->code
  441. - comp_ctx->comp_data->wasm_module->buf_code
  442. + func_ctx->aot_func->code_size;
  443. if (function.GetStartAddress().GetOffset() <= start
  444. && end <= function.GetEndAddress().GetOffset()) {
  445. auto line_entry = sc.GetLineEntry();
  446. location_info = LLVMDIBuilderCreateDebugLocation(
  447. comp_ctx->context, line_entry.GetLine(),
  448. line_entry.GetColumn(), func_ctx->debug_func, NULL);
  449. // LOG_VERBOSE("Gen the location l:%d, c:%d at %lx",
  450. // line_entry.GetLine(), line_entry.GetColumn(), vm_offset);
  451. }
  452. else
  453. LOG_WARNING("the offset and function is not matched");
  454. }
  455. }
  456. return location_info;
  457. }
  458. LLVMMetadataRef
  459. dwarf_gen_func_ret_location(const AOTCompContext *comp_ctx,
  460. const AOTFuncContext *func_ctx)
  461. {
  462. LLVMMetadataRef func_info = NULL;
  463. dwarf_extractor *extractor;
  464. uint64_t vm_offset;
  465. AOTFunc *func = func_ctx->aot_func;
  466. LLVMMetadataRef location_info = NULL;
  467. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  468. return NULL;
  469. // A code address in DWARF for WebAssembly is the offset of an
  470. // instruction relative within the Code section of the WebAssembly file.
  471. // For this reason Section::GetFileAddress() must return zero for the
  472. // Code section. (refert to ObjectFileWasm.cpp)
  473. vm_offset = (func->code + func->code_size - 1)
  474. - comp_ctx->comp_data->wasm_module->buf_code;
  475. location_info = dwarf_gen_location(comp_ctx, func_ctx, vm_offset);
  476. return location_info;
  477. }