### Build and Install libdwarf Source: https://github.com/davea42/libdwarf-code/blob/main/doc/libdwarf.dox Example commands for building, installing, and checking libdwarf when linking against a shared library. ```bash make make install make check ``` -------------------------------- ### Install Project with Ninja Source: https://github.com/davea42/libdwarf-code/blob/main/READMEwin-msys2.md Execute the installation process using the Ninja build tool after setting the installation prefix. ```bash ninja install ``` -------------------------------- ### Verify Dwarfdump Installation Source: https://github.com/davea42/libdwarf-code/blob/main/READMEwin-msys2.md Run the dwarfdump executable to confirm it is installed and accessible. A short message about missing arguments indicates a successful setup. ```bash dwarfdump.exe ``` -------------------------------- ### Install XZ for Distribution Builds on MSYS2 Source: https://github.com/davea42/libdwarf-code/blob/main/READMEwin-msys2.md Install the xz package, which is required for creating distributions. ```bash pacman -S mingw-w64-x86_64-xz ``` -------------------------------- ### Basic Meson Build Source: https://github.com/davea42/libdwarf-code/blob/main/README.md Standard commands to configure, build, install, and test libdwarf using Meson. ```bash meson /tmp/libdwarf-0.4.2 ninja ninja install ninja test ``` -------------------------------- ### Install Meson and CMake for MSYS2 Source: https://github.com/davea42/libdwarf-code/blob/main/READMEwin-msys2.md Install Meson and CMake build system tools for MSYS2. ```bash pacman -S mingw-w64-x86_64-meson pacman -S mingw-w64-x86_64-cmake pacman -S mingw-w64-x86_64-python3-pip ``` -------------------------------- ### Setup Meson Build Environment Source: https://github.com/davea42/libdwarf-code/blob/main/README.md Set up a Meson build environment, specifying the source and build directories, and setting build options. ```bash meson setup -Ddwarfexample=true . /home/davea/dwarf/code ``` -------------------------------- ### Ubuntu Package Installation Source: https://github.com/davea42/libdwarf-code/blob/main/README.md Installs necessary packages for building libdwarf on Ubuntu, including xz, pkgconf, zlib, and libzstd. zlib and libzstd are optional but required for reading compressed DWARF sections. ```bash sudo apt install xz pkgconf zlib1g zlib1g-dev libzstd1 sudo apt install libzstd-dev ``` -------------------------------- ### Install libdwarf Library and Headers Source: https://github.com/davea42/libdwarf-code/blob/main/src/lib/libdwarf/CMakeLists.txt Installs the libdwarf library targets, including archives, libraries, and public headers. It also sets up the export file for CMake. ```cmake install(TARGETS dwarf EXPORT libdwarfTargets ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") install(EXPORT libdwarfTargets FILE libdwarf-targets.cmake NAMESPACE libdwarf:: DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/libdwarf") ``` -------------------------------- ### Build Dwarf Example with Ninja Source: https://github.com/davea42/libdwarf-code/blob/main/READMEcmake.md Configures and builds the dwarfexample component using the Ninja generator. Assumes the source code is in '/path/to/code'. ```bash cmake -G Ninja -DBUILD_DWARFEXAMPLE=ON /path/to/code make ``` -------------------------------- ### Install pkg-config and Findzstd Files Source: https://github.com/davea42/libdwarf-code/blob/main/src/lib/libdwarf/CMakeLists.txt Installs the pkg-config file for libdwarf and the Findzstd.cmake module. These facilitate integration with other build systems and dependency management. ```cmake install(FILES "${PROJECT_BINARY_DIR}/src/lib/libdwarf/libdwarf.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") install(FILES "${PROJECT_SOURCE_DIR}/cmake/Findzstd.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/libdwarf") ``` -------------------------------- ### Install Build Dependencies on Ubuntu Source: https://github.com/davea42/libdwarf-code/blob/main/README.md Install necessary packages for building with Autotools/Configure on Ubuntu 20.04. ```bash sudo apt-get install autoconf libtool pkg-config ``` -------------------------------- ### GNU Configure Build Example Source: https://github.com/davea42/libdwarf-code/blob/main/README.md Demonstrates building libdwarf using GNU configure and make. It shows creating a separate build directory and running configure, make, and make check. The --disable-dependency-tracking option can resolve issues with missing .Po object files. ```bash rm -rf /tmp/build mkdir /tmp/build cd /tmp tar xf /libdwarf-0.4.2.tar.xz cd /tmp/build /tmp/libdwarf-0.4.2/configure make make check ``` -------------------------------- ### Project Script for Build Systems Source: https://github.com/davea42/libdwarf-code/blob/main/doc/libdwarf.dox Reference to a project script that provides examples for all three build systems when linking against a shared library. ```bash scripts/allsimplebuilds.sh ``` -------------------------------- ### Install Homebrew and Build Dependencies on MacOS Source: https://github.com/davea42/libdwarf-code/blob/main/README.md Install Homebrew and essential build tools for using Autotools/Configure on MacOS. ```bash /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew install autoconf automake libtool ``` -------------------------------- ### Show Section Groups Example Source: https://github.com/davea42/libdwarf-code/blob/main/doc/libdwarf.dox This C code example demonstrates how to display section groups present in an object file. It is useful for inspecting object file structure without the verbose output of dwarfdump. ```c #include #include #include int main(int argc, char **argv) { Dwarf_Debug dbg = NULL; Dwarf_Error error; int fd = -1; int ret = 1; const char *filepath = NULL; Dwarf_Section **sections = NULL; Dwarf_Section *sec = NULL; Dwarf_Signed i = 0; Dwarf_Signed count = 0; if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } filepath = argv[1]; fd = open(filepath, O_RDONLY, 0); if (fd < 0) { fprintf(stderr, "Error opening file %s: %s\n", filepath, strerror(errno)); return 1; } if (dwarf_init(fd, DW_ACCESS_READ, NULL, NULL, &dbg, &error) != DW_DLV_OK) { fprintf(stderr, "dwarf_init failed: %s\n", dwarf_errmsg(error)); goto cleanup; } if (dwarf_get_section_list(dbg, §ions, &count, &error) != DW_DLV_OK) { fprintf(stderr, "dwarf_get_section_list failed: %s\n", dwarf_errmsg(error)); goto cleanup; } printf("Sections in %s:\n", filepath); for (i = 0; i < count; ++i) { sec = sections[i]; printf(" %s\n", sec->s_name); } ret = 0; cleanup: if (dbg != NULL) { dwarf_finish(dbg, &error); } if (fd >= 0) { close(fd); } if (sections != NULL) { dwarf_dealloc(sections); } return ret; } ``` -------------------------------- ### Define Dwarf Example Interface Library Source: https://github.com/davea42/libdwarf-code/blob/main/src/bin/dwarfexample/CMakeLists.txt Defines an interface library for dwarf examples, setting compile definitions and options, and linking against the dwarf library. ```cmake add_library(dwarf_example INTERFACE) target_compile_definitions(dwarf_example INTERFACE CONFPREFIX={CMAKE_INSTALL_PREFIX}/lib ${DW_LIBDWARF_STATIC} _GNU_SOURCE) target_compile_options(dwarf_example INTERFACE ${DW_FWALL}) target_link_libraries(dwarf_example INTERFACE dwarf) ``` -------------------------------- ### FreeBSD Package Installation Source: https://github.com/davea42/libdwarf-code/blob/main/README.md Installs required packages for building libdwarf on FreeBSD, such as bash, xz, python3, gmake, liblz4, and zstd. Note that libzstd might require specific include/library paths. ```bash pkg install bash xz python3 gmake liblz4 zstd ``` -------------------------------- ### Install MSYS2 Base Development Tools and Libraries Source: https://github.com/davea42/libdwarf-code/blob/main/READMEwin-msys2.md Install essential packages for building and development on MSYS2, including compilers, build tools, and common libraries. ```bash pacman -Suy pacman -S base-devel git autoconf automake libtool pacman -S mingw-w64-x86_64-python3 pacman -S mingw-w64-x86_64-toolchain pacman -S mingw-w64-x86_64-zlib pacman -S mingw-w64-x86_64-zstd pacman -S mingw-w64-x86_64-doxygen ``` -------------------------------- ### Install dwarfgen man page conditionally Source: https://github.com/davea42/libdwarf-code/blob/main/doc/CMakeLists.txt Installs the dwarfgen man page if the BUILD_DWARFGEN flag is enabled. This ensures man pages are installed only when the corresponding tool is built. ```cmake if ( BUILD_DWARFGEN ) install(FILES dwarfgen.1 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") endif() ``` -------------------------------- ### Install dwarfdump man page conditionally Source: https://github.com/davea42/libdwarf-code/blob/main/doc/CMakeLists.txt Installs the dwarfdump man page if the BUILD_DWARFDUMP flag is enabled. This ensures man pages are installed only when the corresponding tool is built. ```cmake if ( BUILD_DWARFDUMP ) install(FILES dwarfdump.1 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") endif() ``` -------------------------------- ### List Installed Packages on MSYS2 Source: https://github.com/davea42/libdwarf-code/blob/main/READMEwin-msys2.md Command to list all installed packages in MSYS2. ```bash pacman -Q ``` -------------------------------- ### Build with Unix Makefiles and Run Tests Source: https://github.com/davea42/libdwarf-code/blob/main/READMEcmake.md Configures and builds the project using 'Unix Makefiles', enabling testing. Assumes the source code is in '/path/to/code'. ```bash cmake -G "Unix Makefiles" -DDO_TESTING=ON /path/to/code make # To list the tests ctest -N # To run all the tests (their names start with # the letters 'self'). ctest -R self ``` -------------------------------- ### Standard Linux Build with Autotools Source: https://github.com/davea42/libdwarf-code/blob/main/README.md Steps to build libdwarf on Linux using Autotools/Configure, including generating build scripts and running make. ```bash cd /path/to/code sh autogen.sh cd /tmp/build /path/to/code/configure make make check ``` -------------------------------- ### Run All Simple Builds Script Source: https://github.com/davea42/libdwarf-code/blob/main/READMEmacos.md Executes a script to perform all simple build configurations from the source directory. ```bash sh scripts/allsimplebuilds.sh ``` -------------------------------- ### Set CMake Install Prefix Source: https://github.com/davea42/libdwarf-code/blob/main/READMEwin-msys2.md Configure the CMake build system to install executables to a specific prefix, typically a directory in your system's PATH. ```bash -DCMAKE_INSTALL_PREFIX=$HOME/bin ``` -------------------------------- ### Show Compilation Options with Make Source: https://github.com/davea42/libdwarf-code/blob/main/READMEcmake.md Displays the exact compilation and linking options used when building with 'Unix Makefiles'. ```bash make VERBOSE=1 ``` -------------------------------- ### Install Dependencies via MacPorts Source: https://github.com/davea42/libdwarf-code/blob/main/READMEmacos.md Installs zlib, zstd, and meson, and sets the default Python version to 3.11 using the MacPorts package manager. ```bash sudo port install zlib sudo port install zstd sudo port install meson sudo port select --set python3 python311 ``` -------------------------------- ### Initializing libdwarf with Error Handler Source: https://github.com/davea42/libdwarf-code/blob/main/doc/libdwarf.dox Shows the recommended approach for initializing libdwarf, passing NULL for the error handler and error argument. It also demonstrates how to deallocate error information if initialization fails. ```c Dwarf_Debug dbg = 0; const char *path = "myobjectfile"; char *true_path = 0; unsigned int true_pathlen = 0; Dwarf_Handler errhand = 0; Dwarf_Ptr errarg = 0; Dwarf_Error error = 0; int res = 0; res = dwarf_init_path(path,true_path,true_pathlen, DW_GROUPNUMBER_ANY,errhand,errarg,&dbg,&error); ``` ```c dwarf_dealloc_error(dbg,error); ``` -------------------------------- ### Build Simplereader Executable Source: https://github.com/davea42/libdwarf-code/blob/main/src/bin/dwarfexample/CMakeLists.txt Builds the simplereader executable from simplereader.c and links it with the dwarf_example library. ```cmake add_executable(simplereader simplereader.c) set_folder(simplereader src/bin/dwarfexample) target_link_libraries(simplereader PRIVATE dwarf_example) ``` -------------------------------- ### Build ShowSectionGroups Executable Source: https://github.com/davea42/libdwarf-code/blob/main/src/bin/dwarfexample/CMakeLists.txt Builds the showsectiongroups executable from showsectiongroups.c and links it with the dwarf_example library. ```cmake add_executable(showsectiongroups showsectiongroups.c) set_folder(jitreader src/bin/dwarfexample) target_link_libraries(showsectiongroups PRIVATE dwarf_example) ``` -------------------------------- ### Build JITReader Executable Source: https://github.com/davea42/libdwarf-code/blob/main/src/bin/dwarfexample/CMakeLists.txt Builds the jitreader executable from jitreader.c and links it with the dwarf_example library. ```cmake add_executable(jitreader jitreader.c) set_folder(jitreader src/bin/dwarfexample) target_link_libraries(jitreader PRIVATE dwarf_example) ``` -------------------------------- ### Load Section Content Source: https://github.com/davea42/libdwarf-code/blob/main/doc/jitaccess.dox Gets a pointer to an array of bytes representing the content of a specific section. ```c Get a pointer to an array of bytes that represent the section. Parameters obj - Your data section_index - Zero-based section index. return_data - Place the address of this section content into *return_data . error - Pointer to an integer for returning libdwarf-defined error numbers. Return DW_DLV_OK - No error. DW_DLV_ERROR - Error. Use 'error' to indicate a libdwarf-defined error number. DW_DLV_NO_ENTRY - No such section. ``` -------------------------------- ### Get Source Language Name Source: https://github.com/davea42/libdwarf-code/blob/main/doc/libdwarf.dox Function to retrieve the source language name, useful for DWARF6 attributes. ```c dwarf_srclanglname(); ``` -------------------------------- ### Configure Meson Build Options Source: https://github.com/davea42/libdwarf-code/blob/main/README.md View and modify build options for an existing Meson build configuration. ```bash meson configure . ``` -------------------------------- ### Get Section Count Source: https://github.com/davea42/libdwarf-code/blob/main/doc/jitaccess.dox Retrieves the number of sections in an object file, including an index zero section. ```c Get the number of sections in the object file, including the index zero section with no content. Parameters obj - Your data Return Number of sections. ``` -------------------------------- ### Faster Meson Build with Options Source: https://github.com/davea42/libdwarf-code/blob/main/README.md Commands for a faster build using multiple jobs and setting CFLAGS/CXXFLAGS for debugging and pipe optimization. ```bash export CFLAGS="-g -pipe" export CXXFLAGS="-g -pipe" meson /tmp/libdwarf-0.4.2 -Ddwarfexample=true ninja -j8 ninja install ninja test ``` -------------------------------- ### Create Source Distribution with make distcheck Source: https://github.com/davea42/libdwarf-code/blob/main/README.md Use this command to create a source distribution archive. Ensure a build is performed beforehand. ```bash make distcheck ``` -------------------------------- ### Avoidance of unbraced if-else Source: https://github.com/davea42/libdwarf-code/blob/main/CODINGSTYLE.md Highlights a 'Yuck!' example demonstrating the incorrect and discouraged practice of using unbraced if-else statements. ```c if (condition) /* Yuck */ do_this(); /* Yuck */ else /* Yuck */ do_the_other(); /* Yuck */ ```