Skip to content

Instantly share code, notes, and snippets.

@romainthomas
Created May 24, 2025 08:41
Show Gist options
  • Save romainthomas/1f7ba555c4439d9b4b457e293834ec5a to your computer and use it in GitHub Desktop.
Save romainthomas/1f7ba555c4439d9b4b457e293834ec5a to your computer and use it in GitHub Desktop.
Creating a DWARF stack-based variable with LLVM
Variable& Variable::set_stack_offset(uint64_t offset) {
llvm::BumpPtrAllocator& allocator = editor_->allocator();
auto* loc = new (allocator) llvm::DIELoc{};
loc->addValue(allocator, (llvm::dwarf::Attribute)0, llvm::dwarf::DW_FORM_data1,
llvm::DIEInteger(llvm::dwarf::DW_OP_fbreg));
loc->addValue(allocator, (llvm::dwarf::Attribute)0, llvm::dwarf::DW_FORM_sdata,
llvm::DIEInteger(-offset));
loc->computeSize(editor_->streamer().getAsmPrinter().getDwarfFormParams());
die_->addValue(allocator, llvm::dwarf::DW_AT_location,
llvm::dwarf::DW_FORM_exprloc, loc);
return *this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment