dwarf_extractor.cpp 20 KB

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