DWARF(3)
π NAME π
okay so this is the DWARF(3) page from the Library Functions Manual, and it's literally all about how libdwarf lets you access debugging information in object files. bestie, that's the whole vibe. π«Ά
π LIBRARY π
DWARF Access Library (libdwarf, -ldwarf) β yes, that's the glow-up name and the link line, keep both in your notes. β¨
π¦ SYNOPSIS π¦
#include <libdwarf.h>
πΈ DESCRIPTION πΈ
so the DWARF Access Library (libdwarf, -ldwarf) gives applications functions that let them read and write debugging information in object files. the format of the debugging information you can get at through this API is defined by the DWARF standard β peep dwarf(4) for that. π
the DWARF(3) API has two parts, and yes both are main-character energy:
- a consumer API set that lets applications read existing debug information in a program object. the functions that make up the DWARF consumer API are described in the section βDWARF Consumer APIβ below.
- a producer API set that lets applications add debug information to a program object. the functions that make up the DWARF producer API are described in the section βDWARF Producer APIβ below.
each function referenced below is further described in its own manual page. no cap, every single one has its own little moment. π
β¨ Namespace use β¨
the DWARF library uses these prefixes, and they are not interchangeable, okay?
DWARF_*β used for error numbers and constants.DW_*β used for constants.Dwarf_*β used for types.dwarf_*β used for functions and macros that make up the API.
π§ Data Types π§
the DWARF(3) API uses these data types, and honestly they're giving very specific little descriptors:
Dwarf_Abbrevβ describes DWARF abbreviations.Dwarf_Addrβ a program address in the target object.Dwarf_Arangeβ describes address ranges.Dwarf_Attribute,Dwarf_P_Attributeβ describes attributes of debugging information entries.Dwarf_Boolβ used for boolean states.Dwarf_Cie,Dwarf_P_Cieβ describes call information that is common to several frames.Dwarf_Debug,Dwarf_P_Debugβ an opaque type describing a debug context.Dwarf_Die,Dwarf_P_Dieβ a debugging information entry.Dwarf_Fde,Dwarf_P_Fdeβ a frame descriptor.Dwarf_Funcβ a descriptor representing a function.Dwarf_Globalβ a descriptor representing a global name.Dwarf_Halfβ a 16-bit wide unsigned numeric type.Dwarf_Handlerβ a pointer to an error handling function.Dwarf_Lineβ a descriptor for a source line.Dwarf_Offβ an unsigned file offset.Dwarf_P_Exprβ a descriptor for a location expression.Dwarf_Ptrβ a virtual address used by an application.Dwarf_Signedβ a 64-bit wide signed numeric type.Dwarf_Smallβ an 8-bit wide unsigned numeric type.Dwarf_Typeβ a descriptor representing a user-specified type.Dwarf_Unsignedβ a 64-bit wide unsigned numeric type.Dwarf_Varβ a descriptor representing a static variable.Dwarf_Weakβ a descriptor representing a weak name.
π Error Handling π
library functions that encounter an error will return with a value other than DW_DLV_OK. that's the signal, bestie. π¨
the DWARF Access Library (libdwarf, -ldwarf) allows applications to specify three levels of error handling:
- most library functions take a parameter of type
Dwarf_Errorthat specifies a location to store an error descriptor in case of an error. if an error occurs during the execution of an API, and if this parameter is non-NULL, then an error descriptor is written to the location specified. - otherwise, if the error parameter was
NULL, but if an error handler was defined for the debug context in use usingdwarf_init(3)ordwarf_seterrhand(3), then the library will invoke the specified error handler with an error descriptor as argument. - otherwise, if a library wide error handler was specified using
dwarf_seterrhand(3), it is called.
error descriptors may be used with dwarf_errmsg(3) or dwarf_errno(3). π
π The DWARF Consumer API π
the DWARF consumer API permits applications to read DWARF information in an object file. literally its whole thing. π¦
the major functional groups of functions in the consumer API are listed below.
Abbreviations
okay so first up, the abbrev squad β theyβre giving βlet me look up that abbreviation real quickβ energy π
dwarf_get_abbrev()β Retrieve abbreviation information at a given offset. lowkey essential.dwarf_get_abbrev_children_flag()β Check if an abbreviation has child elements. yes, weβre asking if it has kids πdwarf_get_abbrev_code()β Retrieve the abbreviation code for an abbreviation entry descriptor.dwarf_get_abbrev_entry()β Retrieve abbreviation information for an abbreviation entry descriptor.dwarf_get_abbrev_tag()β Retrieve the tag for an abbreviation entry. iconic.
Addresses
address stuff!! we love a good memory map moment πΊοΈβ¨
dwarf_get_address_size()β Return the number of bytes needed to represent an address.dwarf_get_arange()β Search for an address range descriptor covering an address.dwarf_get_arange_cu_header_offset()β Retrieve the offsets associated with an address range descriptor.dwarf_get_arange_info()β Extract address range information from a descriptor.dwarf_get_aranges()β Retrieve program address space mappings.dwarf_get_cu_die_offset()β Retrieve the offset associated with a compilation unit for an address range descriptor.dwarf_get_ranges(),dwarf_get_ranges_a()β Retrieve information about non-contiguous address ranges for a debugging information entry.
Attributes
the attribute girlies are here and they have so many forms π
dwarf_arrayorder()β Retrieve the value of aDW_AT_orderingattribute.dwarf_attr()β Retrieve an attribute descriptor.dwarf_attrlist()β Retrieve attribute descriptors for a debugging information entry.dwarf_attroffset()β Retrieve the section-relative offset of an attribute descriptor.dwarf_attrval_flag()β Retrieve aDW_AT_FORM_flagvalue.dwarf_attrval_signed()β Retrieve an attribute's value as a signed integral quantity.dwarf_attrval_string()β Retrieve an attribute's value as a NUL-terminated string.dwarf_attrval_unsigned()β Retrieve an attribute's value as an unsigned integral quantity.dwarf_bitoffset()β Retrieve the value of aDW_AT_bit_offsetattribute.dwarf_bitsize()β Retrieve the value of aDW_AT_bit_sizeattribute.dwarf_bytesize()β Retrieve the value of aDW_AT_byte_sizeattribute.dwarf_formaddr()β Return the value of an ADDRESS-class attribute.dwarf_formblock()β Return the value of a BLOCK-class attribute. (yes thatβs the whole description, no notes π)dwarf_formexprloc()β Return information about a location expression.dwarf_formflag()β Retrieve information about a BOOLEAN-class attribute.dwarf_formref(),dwarf_global_formref()β Retrieve offsets for REFERENCE-class attributes.dwarf_formsdata(),dwarf_formudata()β Retrieve the value of a CONSTANT-class attribute.dwarf_formsig8()β Return the type signature for a DWARF type.dwarf_formstring()β Retrieve information about a STRING-class attribute.dwarf_get_form_class()β Retrieve the form class for an attribute.dwarf_hasattr()β Check for the presence of an attribute.dwarf_hasform()β Check if an attribute has the given form.dwarf_whatattr()β Retrieve the attribute code for an attribute.dwarf_whatform(),dwarf_whatform_direct()β Retrieve the form of an attribute.
Call Information Entries and Frame Descriptor Entries
CIE and FDE besties!! frame info is literally the backbone of stack unwinding, no cap π«Ά
dwarf_get_cie_index()β Retrieve the index for a CIE descriptor.dwarf_get_cie_info()β Retrieve information from a CIE descriptor.dwarf_get_cie_of_fde()β Retrieve a CIE descriptor.dwarf_get_fde_at_pc()β Retrieve an FDE descriptor for an address.dwarf_get_fde_info_for_all_regs()β Retrieve register rule row.dwarf_get_fde_info_for_all_regs3()β Retrieve register rule row (revised API).dwarf_get_fde_info_for_cfa_reg3()β Retrieve a CFA register rule.dwarf_get_fde_info_for_reg()β Retrieve a register rule.dwarf_get_fde_info_for_reg3()β Retrieve a register rule (revised API).dwarf_get_fde_instr_bytes()β Retrieve instructions from an FDE descriptor.dwarf_get_fde_list(),dwarf_get_fde_list_eh()β Retrieve frame information.dwarf_get_fde_n()β Retrieve an FDE descriptor.dwarf_get_fde_range()β Retrieve range information from an FDE descriptor.
Compilation Units
compilation units are the main characters of debug info, periodt π
dwarf_get_cu_die_offset_given_cu_header_offset(),dwarf_get_cu_die_offset_given_cu_header_offset_b()β Retrieve the offset of the debugging information entry for a compilation or type unit.dwarf_next_cu_header(),dwarf_next_cu_header_b(),dwarf_next_cu_header_c()β Step through compilation units in a debug context.
Debugging Information Entries
DIEs!! (debugging information entries, not the other thing π) theyβre everywhere and we love them π
dwarf_child()β Returns the child of a debugging information entry.dwarf_die_abbrev_code()β Returns the abbreviation code for a debugging information entry.dwarf_die_CU_offset(),dwarf_die_CU_offset_range()β Retrieve offsets and lengths for a compilation unit.dwarf_diename()β Returns theDW_AT_nameattribute for a debugging information entry.dwarf_dieoffset()β Retrieves the offset for a debugging information entry.dwarf_get_die_infotypes_flag()β Indicate the originating section for a debugging information entry.dwarf_highpc(),dwarf_highpc_b()β Return the highest PC value for a debugging information entry.dwarf_lowpc()β Return the lowest PC value for a debugging information entry.dwarf_offdie(),dwarf_offdie_b()β Retrieve a debugging information entry given an offset.dwarf_siblingof(),dwarf_siblingof_b()β Retrieve the sibling descriptor for a debugging information entry.dwarf_srclang()β Retrieve the source language attribute for a debugging information entry.dwarf_tag()β Retrieve the tag for a debugging information entry.
Functions
function girlies rise!! weβre getting names and offsets and all the good stuff β¨
dwarf_func_cu_offset()β Retrieves the offset for the compilation unit for a function.dwarf_func_die_offset()β Retrieves the offset for the debugging information entry for a function.dwarf_funcname()β Retrieves the name of a function.dwarf_func_name_offsets()β Retrieve both the name and offsets for a function.dwarf_get_funcs()β Retrieve information about static functions.
Globals
global variables said βnotice meβ and honestly? valid π
dwarf_get_globals()β Retrieve a list of globals.dwarf_global_cu_offset()β Return the offset for compilation unit for a global.dwarf_global_die_offset()β Return the offset for the debugging information entry for a global.dwarf_global_name_offsets()β Return the name and offsets for a global.dwarf_globname()β Return the name for a global.
Initialization and Finalization
okay so before you do literally anything, you gotta init. Functions dwarf_elf_init() and dwarf_init() may be used for initialization. And when youβre done and you wanna release resources? The function dwarf_finish() may be used to release resources. clean up after yourself bestie π§Ήπ
The functions dwarf_object_init() and dwarf_object_finish() are the β¨vibeβ¨ β they let an app plug in its own alternate low-level file access routines instead of the default ones. Slay, honestly.
Line Numbers π
All the line-info girlies, gather round:
dwarf_lineaddr()β pulls the program address that belongs to a source line.dwarf_linebeginstatement()β checks whether a source line lands right at the beginning of a statement.dwarf_lineblock()β checks whether a source line sits at the start of a basic block.dwarf_lineendsequence()β checks if this source line is the end of a sequence of instructions.dwarf_lineno()β grabs the line number off a line descriptor.dwarf_lineoff()β grabs the column number off a line descriptor.dwarf_linesrc()β gets the source file for a line descriptor.dwarf_line_srcfileno()β gets the index of the source file for a line descriptor.dwarf_srcfiles()β returns the source files for a compilation unit.dwarf_srclines()β returns the line number information for a compilation unit.
Location Lists π
dwarf_get_loclist_entry()β retrieves a location list entry.dwarf_loclist(),dwarf_loclist_n()β retrieve location expressions.dwarf_loclist_from_expr(),dwarf_loclist_from_expr_a(),dwarf_loclist_from_expr_b()β translate a location expression into a location descriptor.
Error Handling π
dwarf_errmsg()β retrieves a human-readable error message (finally, words instead of vibes).dwarf_errno()β retrieves an error number from an error descriptor.dwarf_seterrarg()β sets the argument that gets passed to a callback error handler.dwarf_seterrhand()β sets the callback handler that gets called in case of an error.
Frame Handling π¦
dwarf_expand_frame_instructions()β translates frame instruction bytes.dwarf_set_frame_cfa_value()β sets the CFA parameter for the internal register rule table.dwarf_set_frame_rule_initial_value()β sets the initial value of the register rules in that internal register rule table.dwarf_set_frame_rule_table_size()β sets the max number of columns in the register rule table.dwarf_set_frame_same_value()β sets the register number that represents the βsame valueβ rule.dwarf_set_frame_undefined_value()β sets the register number that represents the βundefinedβ rule.
Macros π
dwarf_find_macro_value_start()β returns the macro value part of a macro string.dwarf_get_macro_details()β retrieves macro information.
Memory Management π§Ή
okay so this one's important bestie, listen up π β in the DWARF consumer API, the memory management rules are not one-size-fits-all. They differ between functions.
Sometimes the memory areas the library hands back to your application get freed by calling specific API functions. Other times, the deallocation function dwarf_dealloc() is enough. The individual manual pages for the API's functions document the specific memory management rules you're supposed to follow β so like, read those, no cap.
The function dwarf_dealloc() is used to mark memory arenas as unused. And on top of that, these functions release specific types of DWARF resources: dwarf_fde_cie_list_dealloc(), dwarf_funcs_dealloc(), dwarf_globals_dealloc(), dwarf_pubtypes_dealloc(), dwarf_ranges_dealloc(), dwarf_srclines_dealloc(), dwarf_types_dealloc(), dwarf_vars_dealloc(), and dwarf_weaks_dealloc().
Symbol Constants π€
Want symbolic names for DWARF constants? These functions ate:
dwarf_get_ACCESS_name(), dwarf_get_AT_name(), dwarf_get_ATE_name(), dwarf_get_CC_name(), dwarf_get_CFA_name(), dwarf_get_CHILDREN_name(), dwarf_get_DS_name(), dwarf_get_DSC_name(), dwarf_get_EH_name(), dwarf_get_END_name(), dwarf_get_FORM_name(), dwarf_get_ID_name(), dwarf_get_INL_name(), dwarf_get_LANG_name(), dwarf_get_LNE_name(), dwarf_get_LNS_name(), dwarf_get_MACINFO_name(), dwarf_get_OP_name(), dwarf_get_ORD_name(), dwarf_get_TAG_name(), dwarf_get_VIRTUALITY_name(), and dwarf_get_VIS_name().
Types πΈ
dwarf_get_pubtypes(),dwarf_get_types()β retrieve descriptors for user-defined types.dwarf_next_types_section()β steps through β.debug_typesβ sections in a debug context.dwarf_pubtype_cu_offset(),dwarf_type_cu_offset()β return the offset for the compilation unit for a type.dwarf_pubtype_die_offset(),dwarf_type_die_offset()β return the offset for the debugging information entry for a type.dwarf_pubtypename(),dwarf_typename()β retrieve the name of a type.dwarf_pubtype_name_offsets(),dwarf_type_name_offsets()β retrieve the name and offsets for a type.
Variables π
dwarf_get_vars()β retrieve descriptors for static variables.dwarf_var_cu_offset()β return the offset for the compilation unit for a variable.dwarf_var_die_offset()β return the offset for the debugging information entry for a variable.dwarf_varname()β retrieve the name of a variable.dwarf_var_name_offsets()β retrieve the name and offsets for a variable.
Weak Symbols π«Ά
dwarf_get_weaks()β retrieve information about weak symbols.dwarf_weak_cu_offset()β return the offset for the compilation unit for a weak symbol.dwarf_weak_die_offset()β return the offset for the debugging information entry for a weak symbol.dwarf_weakname()β retrieve the name of a weak symbol.dwarf_weak_name_offsets()β retrieve the name and offsets for a weak symbol.
Miscellaneous π
dwarf_get_elf()β retrieves the ELF descriptor for a debug context β seeelf(3).dwarf_get_str()β retrieves a NUL-terminated string from the DWARF string section.dwarf_set_reloc_application()β controls whether relocations are handled by the DWARF Access Library (libdwarf,-ldwarf).
β¨ The DWARF Producer API β¨
okay so the DWARF producer API is the whole era where your app gets to add DWARF information to an object file π . Itβs giving βIβm the one writing the debug tea,β and honestly? iconic.
The major functional groups of functions in the producer API are listed below, bestie:
π Attribute Management π
The following functions are used to attach attributes to a debugging information entry β yes, all of them, collect them like little debug PokΓ©mon:
dwarf_add_AT_comp_dir()dwarf_add_AT_const_value_signedint()dwarf_add_AT_const_value_string()dwarf_add_AT_const_value_unsignedint()dwarf_add_AT_dataref()dwarf_add_AT_flag()dwarf_add_AT_location_expr()dwarf_add_AT_name()dwarf_add_AT_producer()dwarf_add_AT_ref_address()dwarf_add_AT_reference()dwarf_add_AT_signed_const()dwarf_add_AT_string()dwarf_add_AT_targ_address()dwarf_add_AT_targ_address_b()dwarf_add_AT_unsigned_const()
π¦ Debugging Information Entry Management π¦
DIE management, because DWARF loves an acronym and we love a main character:
dwarf_add_die_to_debug()β Set the root debugging information entry for a DWARF producer instance.dwarf_die_link()β Links debugging information entries.dwarf_new_die()β Allocate a new debugging information entry.
πΈ Initialization and Finalization πΈ
The functions dwarf_producer_init() and dwarf_producer_init_b() are used to initialize a producer instance. Thatβs the setup era, the βget ready with meβ of the producer API.
When done, applications release resources using the function dwarf_producer_finish(). Clean up after yourself, bestie β no cap.
π Relocations and Sections π
dwarf_get_relocation_info()β Retrieve a relocation array from a producer instance.dwarf_get_relocation_info_count()β Return the number of relocation arrays for a producer instance.dwarf_get_section_bytes()β Retrieve the ELF byte stream for a section.dwarf_reset_section_bytes()β Reset internal state for a producer instance.dwarf_transform_to_disk_form()β Prepare byte streams for writing out.
π Macros π
dwarf_def_macro()β Add a macro definition.dwarf_end_macro_file(),dwarf_start_macro_file()β Record macro file related information.dwarf_undef_macro()β Note the removal of a macro definition.dwarf_vendor_ext()β Enables storing macro information as specified in the DWARF standard.
π§ Symbols, Expressions, Addresses and Offsets π§
This is the big group-chat of producer functions, and every single one is here:
dwarf_add_arange(),dwarf_add_arange_b()β Add address range information.dwarf_add_directory_decl()β Add information about an include directory to a producer instance.dwarf_add_fde_inst()β Add an operation to a frame descriptor entry.dwarf_add_file_decl()β Add information about a source file to a producer instance.dwarf_add_frame_cie()β Add call information to a frame descriptor.dwarf_add_frame_fde(),dwarf_add_frame_fde_b()β Link a frame descriptor to a producer instance.dwarf_add_funcname()β Add information about a function to a producer instance.dwarf_add_line_entry()β Record mapping information between machine addresses and a source line.dwarf_add_expr_addr(),dwarf_add_expr_addr_b()β Add aDW_OP_addropcode to a location expression.dwarf_add_expr_gen()β Add an operator to a location expression.dwarf_add_pubname()β Add information about a global name to a producer instance.dwarf_add_typename()β Add information about a type to a producer instance.dwarf_add_varname()β Add information about a static variable to a producer instance.dwarf_add_weakname()β Add information about a weak symbol to a producer instance.dwarf_expr_current_offset()β Retrieve the current size of a location expression.dwarf_expr_into_block()β Convert a location expression into a byte stream.dwarf_fde_cfa_offset()β Append aDW_CFA_offsetoperation to a frame descriptor.dwarf_lne_end_sequence(),dwarf_lne_set_address()β Note address ranges for source lines.dwarf_new_expr()β Allocate a location expression descriptor.dwarf_new_fde()β Allocate a frame descriptor.
β Miscellaneous β
The function dwarf_producer_set_isa() sets the instruction set architecture for the producer instance. Thatβs the little βwhat ISA are we in?β button, and itβs slaying.
π COMPATIBILITY π
This implementation is believed to be source compatible with the SGI/GNU DWARF(3) library, version 20110113.
Known differences with the SGI/GNU library include:
- The memory management scheme used differs, in a backward-compatible way. See βMemory Managementβ above, for coding guidelines for portable applications.
- There is provision for setting a library-wide error handler in addition to the per-debug context handlers supported by the SGI/GNU API, see the subsection βError Handlingβ above.
π Extensions π
The following APIs are extensions specific to this implementation:
dwarf_attroffset()dwarf_next_types_section()dwarf_producer_set_isa()
π«Ά SEE ALSO π«Ά
π STANDARDS π
The DWARF standard is defined by The DWARF Debugging Information Format, Version 4, http://www.dwarfstd.org/.
π HISTORY π
The DWARF(3) API originated at Silicon Graphics Inc.
A BSD-licensed implementation of a subset of the API was written by John Birrell (<jb@FreeBSD.org>) for the FreeBSD project. The implementation was subsequently revised and completed by Kai Wang (<kaiwang27@users.sourceforge.net>).
Manual pages for this implementation were written by Joseph Koshy (<jkoshy@users.sourceforge.net>) and Kai Wang (<kaiwang27@users.sourceforge.net>).
π okay thatβs the whole DWARF(3) tea β source/version: Debian; date: December 21, 2014; page: DWARF(3) π«Άβ¨
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: libdwarf 15.0.0
- Copyright (c) 2011 Joseph Koshy. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms