### dwfl_begin Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Starts a new libdwfl session. Requires a structure of callbacks for handling ELF file and debug info lookups. ```APIDOC ## dwfl_begin ### Description Start a new session with the library. ### Parameters #### Path Parameters - callbacks (const Dwfl_Callbacks *) - Required - Callback structure (required) ### Returns `Dwfl *` session handle, or NULL on error ### Notes The callbacks structure must remain valid during the entire session ### Example ```c #include const Dwfl_Callbacks callbacks = { .find_elf = dwfl_linux_kernel_find_elf, .find_debuginfo = dwfl_standard_find_debuginfo, .section_address = dwfl_offline_section_address, }; Dwfl *dwfl = dwfl_begin(&callbacks); if (dwfl == NULL) { fprintf(stderr, "dwfl_begin failed: %s\n", dwfl_errmsg(dwfl_errno())); } // Use dwfl... dwfl_end(dwfl); ``` ``` -------------------------------- ### Initialize libdwfl Session Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Starts a new libdwfl session using provided callbacks. The callbacks structure must remain valid throughout the session. Ensure to check for NULL return and handle errors. ```c #include const Dwfl_Callbacks callbacks = { .find_elf = dwfl_linux_kernel_find_elf, .find_debuginfo = dwfl_standard_find_debuginfo, .section_address = dwfl_offline_section_address, }; Dwfl *dwfl = dwfl_begin(&callbacks); if (dwfl == NULL) { fprintf(stderr, "dwfl_begin failed: %s\n", dwfl_errmsg(dwfl_errno())); } // Use dwfl... dwfl_end(dwfl); ``` -------------------------------- ### dwfl_report_begin Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Starts the process of reporting modules and segments for the current debugging session. It clears any previously reported segments and marks existing modules for deletion unless they are re-reported. ```APIDOC ## dwfl_report_begin ### Description Starts reporting the current set of segments and modules. ### Notes Wipes all existing segments; existing modules are marked for deletion unless re-reported. ### Source libdwfl/libdwfl.h:129 ``` -------------------------------- ### dwarf_getalt Source: https://github.com/git/elfutils/blob/main/_autodocs/libdw-api.md Gets the alternate DWARF data set, typically loaded from a `.gnu_debugaltlink` section. libdw attempts to automatically load this on first use if available. ```APIDOC ## dwarf_getalt ### Description Get the alternate DWARF data set (from .gnu_debugaltlink). ### Method `Dwarf *dwarf_getalt(Dwarf *main)` ### Parameters #### Path Parameters - **main** (Dwarf *) - Required - Main DWARF session ### Returns `Dwarf *` alternate session, or NULL if none ### Notes libdw will automatically try to set this on first use of alt FORM ``` -------------------------------- ### gelf_getsyminfo Source: https://github.com/git/elfutils/blob/main/_autodocs/gelf-api.md Get additional symbol information. Returns a pointer to the destination buffer on success, or NULL on error. ```APIDOC ## gelf_getsyminfo ### Description Get additional symbol information. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Method - None (C function) ### Endpoint - None (C function) ### Parameters - **data** (Elf_Data *) - Symbol info data section - **ndx** (int) - Symbol index - **dst** (GElf_Syminfo *) - Destination buffer ### Returns - **GElf_Syminfo *** - Pointer to dst on success, NULL on error ``` -------------------------------- ### Get 64-bit ELF Header Source: https://github.com/git/elfutils/blob/main/_autodocs/libelf-api.md Retrieves a pointer to the ELF header structure for 64-bit binaries. Returns NULL if the ELF file is not a 64-bit binary. ```C Elf64_Ehdr *ehdr = elf64_getehdr(elf); ``` -------------------------------- ### Get 32-bit ELF Header Source: https://github.com/git/elfutils/blob/main/_autodocs/libelf-api.md Retrieves a pointer to the ELF header structure for 32-bit binaries. Returns NULL if the ELF file is not a 32-bit binary. ```C Elf32_Ehdr *ehdr = elf32_getehdr(elf); ``` -------------------------------- ### dwfl_errno Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Gets the error code from the last failing libdwfl call. The error code is kept per-thread. ```APIDOC ## dwfl_errno ### Description Get error code from last failing libdwfl call. ### Parameters None ### Returns `int` error code, 0 if no error ### Notes Value is kept per-thread ``` -------------------------------- ### Iterate and Get Symbol Table Entries Source: https://github.com/git/elfutils/blob/main/_autodocs/gelf-api.md Iterates through symbol table entries using gelf_getsym and prints symbol names and values. Ensure symdata is correctly initialized and symcount is accurate. ```c GElf_Sym sym; for (int i = 0; i < symcount; i++) { if (gelf_getsym(symdata, i, &sym) != NULL) { printf("Symbol: %s at 0x%lx\n", symname, sym.st_value); } } ``` -------------------------------- ### Get Number of Relocatable Bases for Module Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Retrieves the number of relocatable bases for a given module. Returns 0 for ET_EXEC, 1 for ET_DYN, and -1 on error. ```c int dwfl_module_relocations(Dwfl_Module *mod) ``` -------------------------------- ### Get Relocation Base Index for Address Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Obtains the relocation base index for a given address and adjusts the address to be relative to that base. Returns the base index or -1 on error. ```c int dwfl_module_relocate_address(Dwfl_Module *mod, Dwarf_Addr *address) ``` -------------------------------- ### Get ELF Section Name for Relocation Base Index Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Retrieves the ELF section name corresponding to a relocation base index. For ET_DYN, it returns "" with SHN_ABS; for ET_EXEC, it returns actual section names. Returns NULL on error. ```c const char *dwfl_module_relocation_info(Dwfl_Module *mod, unsigned int idx, GElf_Word *shndxp) ``` -------------------------------- ### Get Error Message Source: https://github.com/git/elfutils/blob/main/_autodocs/libdw-api.md Retrieves a human-readable error message for a given error code. Use -1 for a guaranteed string. ```c const char *dwarf_errmsg(int err); ``` -------------------------------- ### dwarf_begin Source: https://github.com/git/elfutils/blob/main/_autodocs/libdw-api.md Creates a handle for a new debug session from a file descriptor. It initializes a DWARF session for reading or read/write operations. ```APIDOC ## dwarf_begin ### Description Create a handle for a new debug session from a file descriptor. ### Method `Dwarf *dwarf_begin(int fildes, Dwarf_Cmd cmd)` ### Parameters #### Path Parameters - **fildes** (int) - Required - File descriptor opened for reading - **cmd** (Dwarf_Cmd) - Required - `DWARF_C_READ` (default) or `DWARF_C_RDWR` ### Returns `Dwarf *` opaque session handle, or NULL on error ### Errors Call `dwarf_errno()` for error details ### Example ```c #include #include int fd = open("binary", O_RDONLY); Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ); if (dwarf == NULL) { fprintf(stderr, "dwarf_begin failed: %s\n", dwarf_errmsg(dwarf_errno())); } // Use dwarf... dwarf_end(dwarf); close(fd); ``` ``` -------------------------------- ### Get Last Error Code Source: https://github.com/git/elfutils/blob/main/_autodocs/libdw-api.md Retrieves the error code from the last failing libdw call. The value is kept per-thread. ```c int dwarf_errno(void); ``` -------------------------------- ### elf_begin Source: https://github.com/git/elfutils/blob/main/_autodocs/libelf-api.md Opens an ELF file descriptor for reading or writing. It can handle file descriptors or memory regions and supports various commands for different operations. ```APIDOC ## elf_begin ### Description Opens an ELF file descriptor for reading or writing. It can handle file descriptors or memory regions and supports various commands for different operations. ### Method `Elf *elf_begin(int fildes, Elf_Cmd cmd, Elf *ref)` ### Parameters #### Path Parameters - **fildes** (int) - Required - File descriptor (-1 for memory) - **cmd** (Elf_Cmd) - Required - Command: `ELF_C_READ`, `ELF_C_RDWR`, `ELF_C_WRITE`, or mmap variants - **ref** (Elf *) - Optional - Reference ELF descriptor for archive mode, or NULL ### Returns `Elf *` opaque descriptor for the file, or NULL on error. ### Errors Call `elf_errno()` for error details. ### Source libelf/libelf.h:246 ### Example ```c #include #include int fd = open("binary", O_RDONLY); Elf *elf = elf_begin(fd, ELF_C_READ, NULL); if (elf == NULL) { fprintf(stderr, "elf_begin failed: %s\n", elf_errmsg(elf_errno())); } // Use elf... elf_end(elf); close(fd); ``` ``` -------------------------------- ### DisasmCtx_t *disasm_begin(Ebl *ebl, Elf *elf, DisasmGetSymCB_t symcb) Source: https://github.com/git/elfutils/blob/main/_autodocs/libebl-libasm-api.md Creates a disassembler context descriptor. This function initializes the necessary structures for subsequent disassembly operations. ```APIDOC ## disasm_begin ### Description Create disassembler context descriptor. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Parameters - **ebl** (Ebl *) - Backend handle (architecture) - **elf** (Elf *) - ELF descriptor containing code - **symcb** (DisasmGetSymCB_t) - Symbol lookup callback ### Returns `DisasmCtx_t *` disassembler context, or NULL on error ### Source libasm/libasm.h:182 ``` -------------------------------- ### gelf_getsymshndx Source: https://github.com/git/elfutils/blob/main/_autodocs/gelf-api.md Get symbol with extended section index. Returns a pointer to the destination symbol buffer on success, or NULL on error. ```APIDOC ## gelf_getsymshndx ### Description Get symbol with extended section index. For symbols with sh_link >= SHN_LORESERVE. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Method - None (C function) ### Endpoint - None (C function) ### Parameters - **symdata** (Elf_Data *) - Symbol table data - **shndxdata** (Elf_Data *) - Extended section index data (or NULL) - **ndx** (int) - Symbol index - **sym** (GElf_Sym *) - Destination symbol buffer - **xshndx** (Elf32_Word *) - Output: extended section index ### Returns - **GElf_Sym *** - Pointer to sym on success, NULL on error ``` -------------------------------- ### libebl-libasm-api.md Source: https://github.com/git/elfutils/blob/main/_autodocs/INDEX.txt Documentation for architecture backend support (libebl) and assembly/disassembly (libasm), covering backend operations, type naming, assembly context, section management, data writing, symbols, and disassembly. ```APIDOC ## libebl and libasm API Documentation ### Description This section details the APIs for `libebl` (architecture backend support) and `libasm` (assembly and disassembly). It covers operations for managing backends, handling assembly data, and performing disassembly. ### libebl Functions - **Backend Operations**: `ebl_openbackend`, `ebl_closebackend` - **Backend Information**: `ebl_get_elfmachine`, `ebl_get_elfclass` - **Type Naming**: `ebl_reloc_type_name`, `ebl_symbol_type_name` ### libasm Functions - **Assembly Context**: `asm_begin`, `asm_end`, `asm_abort` - **Section Management**: `asm_newscn`, `asm_newsubscn` - **Section Groups**: `asm_newscngrp`, `asm_scngrp_newsignature` - **Data Writing**: `asm_addint*`, `asm_addsleb128`, `asm_adduleb128` - **Symbols**: `asm_newsym`, `asm_newcomsym`, `asm_newabssym` - **Formatting**: `asm_align`, `asm_fill` - **Disassembly**: `disasm_begin`, `disasm_end`, `disasm_str`, `disasm_cb` ### Callbacks Includes documentation for callback types used in disassembly. ``` -------------------------------- ### Initialization and Errors Source: https://github.com/git/elfutils/blob/main/_autodocs/libelf-api.md Functions for initializing the library, handling errors, and setting fill bytes. ```APIDOC ## elf_version ### Description Coordinate ELF library and application versions. ### Signature `unsigned int elf_version(unsigned int version)` ### Parameters #### Path Parameters - **version** (unsigned int) - Desired version (typically `EV_CURRENT`) ### Returns `unsigned int` actual version library supports ### Notes Should be called before any other libelf functions ``` ```APIDOC ## elf_fill ### Description Set fill byte used for holes in data structures. ### Signature `void elf_fill(int fill)` ### Parameters #### Path Parameters - **fill** (int) - Fill byte value ``` ```APIDOC ## elf_errno ### Description Return error code from last failing libelf call. ### Signature `int elf_errno(void)` ### Returns `int` error code, 0 if no error ### Notes Value is kept per-thread ``` ```APIDOC ## elf_errmsg ### Description Return human-readable error message. ### Signature `const char *elf_errmsg(int error)` ### Parameters #### Path Parameters - **error** (int) - Error code (0 for last error, -1 for legal string guaranteed) ### Returns `const char *` error message string ``` -------------------------------- ### dwfl_report_begin_add Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Initiates reporting of additional modules without discarding previously reported ones. This is an alternative to `dwfl_report_begin` when existing module information should be preserved. ```APIDOC ## dwfl_report_begin_add ### Description Start reporting additional modules without removing old ones. ### Notes Like dwfl_report_begin but preserves existing modules. Calls to dwfl_report_end won't remove unreported modules. ### Source libdwfl/libdwfl.h:201 ``` -------------------------------- ### Initialize libelf Version Source: https://github.com/git/elfutils/blob/main/_autodocs/README.md Ensure compatibility by checking and initializing the libelf library version using elf_version() before use. Exit if a version mismatch occurs. ```c #include if (elf_version(EV_CURRENT) == EV_NONE) { fprintf(stderr, "libelf version mismatch\n"); exit(1); } ``` -------------------------------- ### Create New 64-bit ELF Header Source: https://github.com/git/elfutils/blob/main/_autodocs/libelf-api.md Creates a new ELF header for a 64-bit binary if one does not already exist. Returns NULL on error. ```C Elf64_Ehdr *new_ehdr = elf64_newehdr(elf); ``` -------------------------------- ### Create Move Info Source: https://github.com/git/elfutils/blob/main/_autodocs/gelf-api.md Creates the m_info value from symbol index and size. ```c GELF_M_INFO(sym, size) ``` -------------------------------- ### dwfl_core_file_report(Dwfl *dwfl, Elf *elf, const char *executable) Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Report modules from ET_CORE (core dump) file. ```APIDOC ## Function: dwfl_core_file_report(Dwfl *dwfl, Elf *elf, const char *executable) ### Description Report modules from ET_CORE (core dump) file. ### Parameters - **dwfl** (Dwfl *) - Session handle - **elf** (Elf *) - ELF descriptor of core dump - **executable** (const char *) - Executable path (or NULL) ### Returns int - number of modules reported, or -1 on error ``` -------------------------------- ### Disassemble Instructions to String Buffer Source: https://github.com/git/elfutils/blob/main/_autodocs/libebl-libasm-api.md Use `disasm_str` to disassemble code and write the output to a string buffer. The `startp` parameter is advanced past the disassembled instruction. ```c int disasm_str(DisasmCtx_t *ctx, const uint8_t **startp, const uint8_t *end, GElf_Addr addr, const char *fmt, char **bufp, size_t len, void *symcbarg) ``` -------------------------------- ### Symbol and Relocation Macros Source: https://github.com/git/elfutils/blob/main/_autodocs/gelf-api.md Provides C macros for extracting and creating symbol, relocation, and move information from ELF file structures. These macros simplify the process of working with symbol bindings, types, visibility, and relocation details. ```APIDOC ## Macro Functions for Symbol Manipulation ### Description GElf provides C macros for extracting and creating symbol information: - **GELF_ST_BIND(val)**: Extract symbol binding from st_info field. - **GELF_ST_TYPE(val)**: Extract symbol type from st_info field. - **GELF_ST_INFO(bind, type)**: Create st_info value from binding and type. - **GELF_ST_VISIBILITY(val)**: Extract visibility from st_other field. - **GELF_R_SYM(info)**: Extract symbol index from relocation r_info field. - **GELF_R_TYPE(info)**: Extract relocation type from r_info field. - **GELF_R_INFO(sym, type)**: Create r_info value from symbol index and relocation type. - **GELF_M_SYM(info)**: Extract symbol index from move m_info field. - **GELF_M_SIZE(info)**: Extract size from move m_info field. - **GELF_M_INFO(sym, size)**: Create m_info value from symbol index and size. ### Parameters None ### Returns Extracted values or created information based on the macro. ``` -------------------------------- ### Get ELF Header Source: https://github.com/git/elfutils/blob/main/_autodocs/gelf-api.md Retrieves the ELF header in a class-independent format. This function automatically handles both 32-bit and 64-bit files. Ensure the Elf descriptor is valid and a destination buffer is provided. ```c GElf_Ehdr ehdr; if (gelf_getehdr(elf, &ehdr) == NULL) { fprintf(stderr, "gelf_getehdr failed\n"); } printf("Entry point: 0x%lx\n", ehdr.e_entry); printf("Machine: %d\n", ehdr.e_machine); ``` -------------------------------- ### Link using pkg-config Source: https://github.com/git/elfutils/blob/main/_autodocs/README.md Command to link a C program with elfutils libraries using pkg-config to automatically determine compiler and linker flags. ```bash # Using pkg-config gcc program.c $(pkg-config --cflags --libs libelf libdw libdwfl) ``` -------------------------------- ### Analyze Modules and Symbols with Dwfl Source: https://github.com/git/elfutils/blob/main/_autodocs/README.md Uses libdwfl to report modules from a running process and find symbols at a given address. Requires process ID and address. ```c #include const Dwfl_Callbacks callbacks = { .find_elf = dwfl_linux_proc_find_elf, .find_debuginfo = dwfl_standard_find_debuginfo, }; Dwfl *dwfl = dwfl_begin(&callbacks); if (dwfl == NULL) { fprintf(stderr, "dwfl_begin failed\n"); } // Report modules from running process dwfl_report_begin(dwfl); dwfl_linux_proc_report(dwfl, getpid()); dwfl_report_end(dwfl, NULL, NULL); // Find symbol at address Dwarf_Addr bias; Dwfl_Module *mod = dwfl_addrmodule(dwfl, addr); if (mod) { GElf_Off offset; GElf_Sym sym; const char *name = dwfl_module_addrinfo(mod, addr, &offset, &sym, NULL, NULL, NULL); printf("Symbol: %s+0x%lx\n", name ? name : "unknown", offset); } dwfl_end(dwfl); ``` -------------------------------- ### Link with libelf, DWARF, and module support Source: https://github.com/git/elfutils/blob/main/_autodocs/README.md Command to link a C program with libelf, libdw, and libdwfl for module and process-level debugging. ```bash # With module/process support gcc program.c -lelf -ldw -ldwfl ``` -------------------------------- ### int disasm_str(DisasmCtx_t *ctx, const uint8_t **startp, const uint8_t *end, GElf_Addr addr, const char *fmt, char **bufp, size_t len, void *symcbarg) Source: https://github.com/git/elfutils/blob/main/_autodocs/libebl-libasm-api.md Disassembles instructions and writes the output to a string buffer. It advances the instruction pointer past the disassembled instructions. ```APIDOC ## disasm_str ### Description Disassemble instructions and write output to string buffer. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Parameters - **ctx** (DisasmCtx_t *) - Disassembler context - **startp** (const uint8_t **) - In/Out: instruction pointer - **end** (const uint8_t *) - End of code buffer - **addr** (GElf_Addr) - Current instruction address - **fmt** (const char *) - Format string (or NULL) - **bufp** (char **) - Output buffer pointer - **len** (size_t) - Available buffer space - **symcbarg** (void *) - User data for symbol callback ### Returns `int` 0 on success, -1 on error ### Notes startp is advanced past disassembled instruction ### Source libasm/libasm.h:189 ``` -------------------------------- ### dwarf_entrypc Source: https://github.com/git/elfutils/blob/main/_autodocs/libdw-api.md Retrieves the entry point address (DW_AT_entry_pc or DW_AT_low_pc) from a DIE. Outputs the address value. ```APIDOC ## dwarf_entrypc(Dwarf_Die *die, Dwarf_Addr *return_addr) ### Description Get entry point (DW_AT_entry_pc or DW_AT_low_pc) from DIE. ### Parameters #### Path Parameters - **die** (Dwarf_Die *) - Required - DIE to query - **return_addr** (Dwarf_Addr *) - Required - Output: address value ### Returns `int` 0 on success, -1 on error ``` -------------------------------- ### libdwfl-api.md Source: https://github.com/git/elfutils/blob/main/_autodocs/INDEX.txt Documentation for the module and process-level debugging library (libdwfl), with over 80 functions for session management, module reporting, module iteration, symbol lookup, and DWARF access. ```APIDOC ## libdwfl API Documentation ### Description This section describes the API for the `libdwfl` library, which facilitates module and process-level debugging. It offers functions for managing debugging sessions, reporting on modules, and accessing symbol and DWARF information. ### Functions - **Session Management**: `dwfl_begin`, `dwfl_end` - **Module Reporting**: `dwfl_report_begin`, `dwfl_report_module`, `dwfl_report_elf` - **Module Iteration**: `dwfl_getmodules`, `dwfl_addrmodule` - **Module Information**: `dwfl_module_info`, `dwfl_module_build_id` - **Build ID Operations**: `dwfl_module_report_build_id` - **Standard Callbacks**: `dwfl_standard_find_debuginfo` - **Linux-specific**: `dwfl_linux_kernel_report_*`, `dwfl_linux_proc_report` - **Core Dump Analysis**: `dwfl_core_file_report` - **ELF Access**: `dwfl_module_getelf`, `dwfl_module_getsym` - **Symbol Lookup**: `dwfl_module_addrinfo`, `dwfl_module_addrname` - **DWARF Access**: `dwfl_module_getdwarf`, `dwfl_addrdwarf` - **Source Lines**: `dwfl_getsrc`, `dwfl_module_getsrc_file` - **Relocation Handling**: `dwfl_module_relocations`, `dwfl_validate_address` ### Callbacks Includes documentation for the `dwfl` callbacks structure. ``` -------------------------------- ### Create Relocation Info Source: https://github.com/git/elfutils/blob/main/_autodocs/gelf-api.md Creates the r_info value from symbol index and relocation type. ```c GELF_R_INFO(sym, type) ``` -------------------------------- ### Create New 32-bit ELF Header Source: https://github.com/git/elfutils/blob/main/_autodocs/libelf-api.md Creates a new ELF header for a 32-bit binary if one does not already exist. Returns NULL on error. ```C Elf32_Ehdr *new_ehdr = elf32_newehdr(elf); ``` -------------------------------- ### libdw-api.md Source: https://github.com/git/elfutils/blob/main/_autodocs/INDEX.txt Documentation for the DWARF debugging information access library (libdw), featuring over 100 functions for session management, compilation unit operations, DIE traversal, attribute access, and source line tracking. ```APIDOC ## libdw API Documentation ### Description This section details the API for the `libdw` library, which is used for accessing DWARF debugging information. It provides extensive functionality for parsing and retrieving debugging data from ELF files. ### Functions - **Session Management**: `dwarf_begin`, `dwarf_begin_elf` - **Compilation Unit Operations**: `dwarf_nextcu`, `dwarf_get_units` - **DIE Traversal**: `dwarf_child`, `dwarf_siblingof`, `dwarf_peel_type` - **Attribute Access**: `dwarf_attr`, `dwarf_attr_integrate` - **Attribute Value Extraction**: `dwarf_formstring`, `dwarf_formudata` - **Simplified Accessors**: `dwarf_diename`, `dwarf_lowpc`, `dwarf_highpc` - **Location Information**: `dwarf_getlocation`, `dwarf_getlocations` - **Source Line Tracking**: `dwarf_getsrclines`, `dwarf_getsrcfiles` - **Line Information**: `dwarf_lineaddr`, `dwarf_lineno`, `dwarf_linesrc` - **Address Ranges**: `dwarf_haspc`, `dwarf_ranges` - **Type Information**: `dwarf_bytesize`, `dwarf_bitsize`, `dwarf_srclang` - **CFI Support**: `dwarf_getcfi`, `dwarf_next_cfi` ### Data Structures and Types Includes full documentation for types and structures related to DWARF information. ``` -------------------------------- ### Access DWARF Debug Information Source: https://github.com/git/elfutils/blob/main/_autodocs/README.md Initializes DWARF access and iterates through compilation units. Requires an initialized Elf descriptor. ```c #include Dwarf *dwarf = dwarf_begin_elf(elf, DWARF_C_READ, NULL); if (dwarf == NULL) { fprintf(stderr, "dwarf_begin_elf failed\n"); } // Iterate compilation units Dwarf_Off offset = 0; size_t header_size; Dwarf_Off abbrev_off; uint8_t addr_size, offset_size; while (dwarf_nextcu(dwarf, offset, &offset, &header_size, &abbrev_off, &addr_size, &offset_size) == 0) { // Process CU at previous offset } dwarf_end(dwarf); ``` -------------------------------- ### Create Symbol Info Source: https://github.com/git/elfutils/blob/main/_autodocs/gelf-api.md Creates the st_info value from binding and type components. ```c GELF_ST_INFO(bind, type) ``` -------------------------------- ### dwfl_module_getelf Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Fetches the main ELF file for a given module, including its allocated sections. It also provides the address bias. ```APIDOC ## dwfl_module_getelf ### Description Fetch module's main ELF file (with allocated sections). ### Parameters #### Path Parameters - **mod** (Dwfl_Module *) - Required - Module handle - **bias** (GElf_Addr *) - Required - Output: address bias ### Returns `Elf *` ELF descriptor, or NULL on error ### Notes *bias is difference between loaded addresses and symbol table addresses ``` -------------------------------- ### elf64_newphdr Source: https://github.com/git/elfutils/blob/main/_autodocs/libelf-api.md Creates a new program header table for a 64-bit ELF binary. It allocates space for a specified number of entries. ```APIDOC ## elf64_newphdr ### Description Create program header table for 64-bit binary. ### Parameters #### Path Parameters - **elf** (Elf *) - Required - ELF descriptor - **cnt** (size_t) - Required - Number of entries to create ### Returns `Elf64_Phdr *` pointer to new table, or NULL on error ``` -------------------------------- ### Open a DWARF Debug Session Source: https://github.com/git/elfutils/blob/main/_autodocs/libdw-api.md Create a handle for a new debug session from a file descriptor. Ensure the file descriptor is opened for reading and handle potential errors by checking the return value and using dwarf_errno(). ```c #include #include int fd = open("binary", O_RDONLY); Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ); if (dwarf == NULL) { fprintf(stderr, "dwarf_begin failed: %s\n", dwarf_errmsg(dwarf_errno())); } // Use dwarf... dwarf_end(dwarf); close(fd); ``` -------------------------------- ### dwarf_begin_elf Source: https://github.com/git/elfutils/blob/main/_autodocs/libdw-api.md Creates a debug session handle from an existing ELF descriptor. This function is useful when the ELF file has already been opened and parsed by libelf. ```APIDOC ## dwarf_begin_elf ### Description Create a debug session handle from an ELF descriptor. ### Method `Dwarf *dwarf_begin_elf(Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp)` ### Parameters #### Path Parameters - **elf** (Elf *) - Required - ELF descriptor (from libelf) - **cmd** (Dwarf_Cmd) - Required - `DWARF_C_READ` or `DWARF_C_RDWR` - **scngrp** (Elf_Scn *) - Optional - Section group (usually NULL) ### Returns `Dwarf *` session handle, or NULL on error ``` -------------------------------- ### dwfl_linux_kernel_report_kernel(Dwfl *dwfl) Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Report the running Linux kernel. ```APIDOC ## Function: dwfl_linux_kernel_report_kernel(Dwfl *dwfl) ### Description Report the running Linux kernel. ### Parameters - **dwfl** (Dwfl *) - Session handle ### Returns int - 0 on success, -1 if dwfl_report_module failed, errno if opening kernel failed ``` -------------------------------- ### dwfl_linux_kernel_report_offline(Dwfl *dwfl, const char *release, int (*predicate)(const char *, const char *)) Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Report kernel and modules from disk. ```APIDOC ## Function: dwfl_linux_kernel_report_offline(Dwfl *dwfl, const char *release, int (*predicate)(const char *, const char *)) ### Description Report kernel and modules from disk. ### Parameters - **dwfl** (Dwfl *) - Session handle - **release** (const char *) - Kernel version or path (NULL for uname -r) - **predicate** (int (*)(const char *, const char *)) - Module filter callback (or NULL) ### Returns int - 0 on success, -1 if dwfl_report_module failed, errno on failure ``` -------------------------------- ### Backend Access Functions Source: https://github.com/git/elfutils/blob/main/_autodocs/libebl-libasm-api.md Functions for opening and closing backend handles for ELF objects, machine types, or emulation names. ```APIDOC ## ebl_openbackend ### Description Get backend handle for object associated with ELF descriptor. ### Parameters #### Path Parameters - **elf** (Elf *) - Required - ELF descriptor ### Returns `Ebl *` backend handle, or NULL on error ``` ```APIDOC ## ebl_openbackend_machine ### Description Get backend handle for given machine type. ### Parameters #### Path Parameters - **machine** (GElf_Half) - Required - ELF machine type (e.g., EM_X86_64) ### Returns `Ebl *` backend handle, or NULL on error ``` ```APIDOC ## ebl_openbackend_emulation ### Description Get backend handle for given emulation name. ### Parameters #### Path Parameters - **emulation** (const char *) - Required - Emulation name ### Returns `Ebl *` backend handle, or NULL on error ``` ```APIDOC ## ebl_closebackend ### Description Free resources allocated for backend handle. ### Parameters #### Path Parameters - **bh** (Ebl *) - Required - Backend handle ``` -------------------------------- ### elf64_newehdr Source: https://github.com/git/elfutils/blob/main/_autodocs/libelf-api.md Creates a new ELF header for a 64-bit ELF binary if one does not already exist. ```APIDOC ## elf64_newehdr ### Description Creates a new ELF header for a 64-bit ELF binary if one does not already exist. ### Method `Elf64_Ehdr *elf64_newehdr(Elf *elf)` ### Parameters #### Path Parameters - **elf** (Elf *) - Required - ELF descriptor ### Returns `Elf64_Ehdr *` pointer to created header, or NULL on error. ### Source libelf/libelf.h:281 ``` -------------------------------- ### Link with libelf and DWARF support Source: https://github.com/git/elfutils/blob/main/_autodocs/README.md Command to link a C program with libelf and libdw for DWARF debugging information access. ```bash # With DWARF support gcc program.c -lelf -ldw ``` -------------------------------- ### dwfl_linux_kernel_report_modules(Dwfl *dwfl) Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Report all kernel modules from the running system. ```APIDOC ## Function: dwfl_linux_kernel_report_modules(Dwfl *dwfl) ### Description Report all kernel modules from the running system. ### Parameters - **dwfl** (Dwfl *) - Session handle ### Returns int - 0 on success, -1 if dwfl_report_module failed, errno on read failure ``` -------------------------------- ### Dump ELF Sections (32/64-bit) Source: https://github.com/git/elfutils/blob/main/_autodocs/gelf-api.md Iterates through and prints information about ELF sections. This function works for both 32-bit and 64-bit ELF files due to GElf's unified interface. ```c #include void dump_elf_sections(const char *filename) { Elf *elf = elf_begin(open(filename, O_RDONLY), ELF_C_READ, NULL); GElf_Ehdr ehdr; gelf_getehdr(elf, &ehdr); size_t shnum; elf_getshdrnum(elf, &shnum); for (size_t i = 0; i < shnum; i++) { Elf_Scn *scn = elf_getscn(elf, i); GElf_Shdr shdr; gelf_getshdr(scn, &shdr); printf("Section %zu: type=%u size=%lu\n", i, shdr.sh_type, shdr.sh_size); } elf_end(elf); } ``` -------------------------------- ### dwfl_linux_proc_report(Dwfl *dwfl, pid_t pid) Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Report modules from running process. ```APIDOC ## Function: dwfl_linux_proc_report(Dwfl *dwfl, pid_t pid) ### Description Report modules from running process. ### Parameters - **dwfl** (Dwfl *) - Session handle - **pid** (pid_t) - Process ID ### Returns int - 0 on success, -1 if dwfl_report_module failed, errno on failure ``` -------------------------------- ### dwfl_linux_proc_maps_report(Dwfl *dwfl, FILE *file) Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Report modules from /proc/[pid]/maps file stream. ```APIDOC ## Function: dwfl_linux_proc_maps_report(Dwfl *dwfl, FILE *file) ### Description Report modules from /proc/[pid]/maps file stream. ### Parameters - **dwfl** (Dwfl *) - Session handle - **file** (FILE *) - Open stream in /proc/PID/maps format ### Returns int - 0 on success, -1 on error ``` -------------------------------- ### dwfl_version Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Retrieves the implementation version string of libdwfl. ```APIDOC ## dwfl_version ### Description Get implementation version string suitable for printing. ### Parameters #### Path Parameters - dwfl (Dwfl *) - Session handle ### Returns `const char *` version string ``` -------------------------------- ### Assembly Context Management Source: https://github.com/git/elfutils/blob/main/_autodocs/libebl-libasm-api.md Functions for managing the assembler context, including creation, finalization, and abortion. ```APIDOC ## AsmCtx_t * asm_begin(const char *fname, Ebl *ebl, bool textp) ### Description Create output file and return assembler context descriptor. ### Parameters #### Path Parameters - **fname** (const char *) - Required - Output filename - **ebl** (Ebl *) - Required - Backend handle (architecture information) - **textp** (bool) - Required - true for assembler text format, false for object file ### Returns `AsmCtx_t *` assembler context, or NULL on error ### Source libasm/libasm.h:76 ``` ```APIDOC ## int asm_end(AsmCtx_t *ctx) ### Description Finalize output file and free resources. ### Parameters #### Path Parameters - **ctx** (AsmCtx_t *) - Required - Assembler context ### Returns `int` 0 on success, -1 on error ### Source libasm/libasm.h:82 ``` ```APIDOC ## int asm_abort(AsmCtx_t *ctx) ### Description Abort operation and free resources without writing. ### Parameters #### Path Parameters - **ctx** (AsmCtx_t *) - Required - Assembler context ### Returns `int` 0 on success, -1 on error ### Source libasm/libasm.h:79 ``` ```APIDOC ## Elf * asm_getelf(AsmCtx_t *ctx) ### Description Get ELF descriptor for the output file. ### Parameters #### Path Parameters - **ctx** (AsmCtx_t *) - Required - Assembler context ### Returns `Elf *` ELF descriptor, or NULL if using text output ### Source libasm/libasm.h:167 ``` -------------------------------- ### gelf-api.md Source: https://github.com/git/elfutils/blob/main/_autodocs/INDEX.txt Documentation for the Generic ELF access library (gelf), offering class-independent ELF access with over 60 functions for ELF header operations, program and section header access, symbol operations, and data conversion. ```APIDOC ## gelf API Documentation ### Description This section covers the API for the `gelf` library, which provides generic, class-independent access to ELF file structures. It includes functions for accessing headers, symbols, relocations, and dynamic information. ### Functions - **ELF Header Operations**: `gelf_getehdr`, `gelf_getclass` - **Program Header Access**: `gelf_getphdr`, `gelf_update_phdr` - **Section Header Access**: `gelf_getshdr`, `gelf_update_shdr` - **Symbol Operations**: `gelf_getsym`, `gelf_update_sym` - **Relocation Handling**: `gelf_getrel`, `gelf_getrela` - **Dynamic Section**: `gelf_getdyn`, `gelf_update_dyn` - **Version Information**: `gelf_getversym`, `gelf_getverneed` - **Data Conversion**: `gelf_xlatetom`, `gelf_xlatetof` ### Macros Includes documentation for macro functions used in symbol manipulation and other operations. ``` -------------------------------- ### elf32_newphdr Source: https://github.com/git/elfutils/blob/main/_autodocs/libelf-api.md Creates a new program header table for a 32-bit ELF binary. It allocates space for a specified number of entries. ```APIDOC ## elf32_newphdr ### Description Create program header table for 32-bit binary. ### Parameters #### Path Parameters - **elf** (Elf *) - Required - ELF descriptor - **cnt** (size_t) - Required - Number of entries to create ### Returns `Elf32_Phdr *` pointer to new table, or NULL on error ``` -------------------------------- ### int disasm_cb(DisasmCtx_t *ctx, const uint8_t **startp, const uint8_t *end, GElf_Addr addr, const char *fmt, DisasmOutputCB_t outcb, void *outcbarg, void *symcbarg) Source: https://github.com/git/elfutils/blob/main/_autodocs/libebl-libasm-api.md Disassembles instructions and outputs the result using a callback function. This is useful for processing disassembled code without writing to a fixed buffer. ```APIDOC ## disasm_cb ### Description Disassemble instructions and output using callback function. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Parameters - **ctx** (DisasmCtx_t *) - Disassembler context - **startp** (const uint8_t **) - In/Out: instruction pointer - **end** (const uint8_t *) - End of code buffer - **addr** (GElf_Addr) - Current instruction address - **fmt** (const char *) - Format string (or NULL) - **outcb** (DisasmOutputCB_t) - Output callback function - **outcbarg** (void *) - User data for output callback - **symcbarg** (void *) - User data for symbol callback ### Returns `int` 0 on success, -1 on error ### Source libasm/libasm.h:195 ``` -------------------------------- ### dwfl_module_addrinfo Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Locates the symbol associated with a given address and returns comprehensive details, including offset and ELF file information. ```APIDOC ## dwfl_module_addrinfo ### Description Find symbol associated with address and return detailed information. ### Parameters #### Path Parameters - **mod** (Dwfl_Module *) - Required - Module handle - **address** (GElf_Addr) - Required - Address to query - **offset** (GElf_Off *) - Required - Output: offset from symbol start - **sym** (GElf_Sym *) - Required - Output: symbol structure - **shndxp** (GElf_Word *) - Optional - Output: section index (or NULL) - **elfp** (Elf **) - Optional - Output: source ELF file (or NULL) - **bias** (Dwarf_Addr *) - Optional - Output: address bias (or NULL) ### Returns `const char *` symbol name, or NULL if not found ### Notes Prefers over dwfl_module_addrsym ``` -------------------------------- ### Open an ELF File Source: https://github.com/git/elfutils/blob/main/_autodocs/README.md Opens an ELF file for reading using libelf. Ensure to close the file descriptor and elf descriptor afterwards. ```c #include #include #include int fd = open("binary", O_RDONLY); Elf *elf = elf_begin(fd, ELF_C_READ, NULL); if (elf == NULL) { fprintf(stderr, "elf_begin failed: %s\n", elf_errmsg(elf_errno())); } // Use elf... elf_end(elf); close(fd); ``` -------------------------------- ### elf32_newehdr Source: https://github.com/git/elfutils/blob/main/_autodocs/libelf-api.md Creates a new ELF header for a 32-bit ELF binary if one does not already exist. ```APIDOC ## elf32_newehdr ### Description Creates a new ELF header for a 32-bit ELF binary if one does not already exist. ### Method `Elf32_Ehdr *elf32_newehdr(Elf *elf)` ### Parameters #### Path Parameters - **elf** (Elf *) - Required - ELF descriptor ### Returns `Elf32_Ehdr *` pointer to created header, or NULL on error. ### Source libelf/libelf.h:279 ``` -------------------------------- ### dwfl_getmodules Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Iterates through all loaded modules, invoking a provided callback function for each module. This is useful for processing modules sequentially. ```APIDOC ## dwfl_getmodules ### Description Iterate through modules, calling callback for each. ### Parameters #### Path Parameters - **dwfl** (Dwfl *) - Required - Session handle #### Query Parameters - **callback** (int (*)(Dwfl_Module *, void **, const char *, Dwarf_Addr, void *arg)) - Required - Callback function - **arg** (void *) - Required - User data passed to callback - **offset** (ptrdiff_t) - Required - Starting position (0 to begin) ### Returns `ptrdiff_t` offset to resume iteration, 0 when done, -1 on error ### Notes Callback returns DWARF_CB_OK to continue or other value to stop ``` -------------------------------- ### dwfl_nextcu Source: https://github.com/git/elfutils/blob/main/_autodocs/libdwfl-api.md Iterates through Compilation Units (CUs) across all modules in the session. ```APIDOC ## dwfl_nextcu ### Description Iterate through CUs across all modules. ### Parameters #### Path Parameters - **dwfl** (Dwfl *) - Required - Session handle - **lastcu** (Dwarf_Die *) - Required - Previous CU (NULL to start) - **bias** (Dwarf_Addr *) - Required - Output: address bias ### Returns - **Dwarf_Die *** - next CU DIE, or NULL at end ``` -------------------------------- ### Disassemble Instructions with Output Callback Source: https://github.com/git/elfutils/blob/main/_autodocs/libebl-libasm-api.md Use `disasm_cb` to disassemble code and send the output to a specified callback function. This is useful for custom output handling. ```c int disasm_cb(DisasmCtx_t *ctx, const uint8_t **startp, const uint8_t *end, GElf_Addr addr, const char *fmt, DisasmOutputCB_t outcb, void *outcbarg, void *symcbarg) ``` -------------------------------- ### dwarf_setalt Source: https://github.com/git/elfutils/blob/main/_autodocs/libdw-api.md Explicitly provides the alternate DWARF data set to be used with the main DWARF session. This allows manual control over which alternate debug data is associated. ```APIDOC ## dwarf_setalt ### Description Provide the alternate DWARF data set explicitly. ### Method `void dwarf_setalt(Dwarf *main, Dwarf *alt)` ### Parameters #### Path Parameters - **main** (Dwarf *) - Required - Main DWARF session - **alt** (Dwarf *) - Required - Alternate session data ``` -------------------------------- ### Backend Information Functions Source: https://github.com/git/elfutils/blob/main/_autodocs/libebl-libasm-api.md Functions to retrieve information about the current ELF backend. ```APIDOC ## ebl_get_elfmachine ### Description Get ELF machine type. ### Parameters #### Path Parameters - **ebl** (Ebl *) - Required - Backend handle ### Returns `int` ELF machine type (EM_* constant) ``` ```APIDOC ## ebl_get_elfclass ### Description Get ELF class (32-bit or 64-bit). ### Parameters #### Path Parameters - **ebl** (Ebl *) - Required - Backend handle ### Returns `int` ELFCLASS32 or ELFCLASS64 ``` ```APIDOC ## ebl_get_elfdata ### Description Get ELF data encoding (byte order). ### Parameters #### Path Parameters - **ebl** (Ebl *) - Required - Backend handle ### Returns `int` ELFDATA2LSB (little-endian) or ELFDATA2MSB (big-endian) ``` ```APIDOC ## ebl_backend_name ### Description Get backend name string. ### Parameters #### Path Parameters - **ebl** (Ebl *) - Required - Backend handle ### Returns `const char *` backend name ``` -------------------------------- ### gelf_getvernaux Source: https://github.com/git/elfutils/blob/main/_autodocs/gelf-api.md Retrieves version need auxiliary information from the provided version needs auxiliary data at a specific offset. ```APIDOC ## gelf_getvernaux ### Description Get version need auxiliary information. ### Parameters #### Path Parameters - **data** (Elf_Data *) - Required - Version needs auxiliary data - **offset** (int) - Required - Entry offset - **dst** (GElf_Vernaux *) - Required - Destination buffer ### Returns `GElf_Vernaux *` pointer to dst on success, NULL on error ```