1da0c48c4Sopenharmony_ci/* Interfaces for libdwelf. DWARF ELF Low-level Functions. 2da0c48c4Sopenharmony_ci Copyright (C) 2014, 2015, 2016, 2018 Red Hat, Inc. 3da0c48c4Sopenharmony_ci This file is part of elfutils. 4da0c48c4Sopenharmony_ci 5da0c48c4Sopenharmony_ci This file is free software; you can redistribute it and/or modify 6da0c48c4Sopenharmony_ci it under the terms of either 7da0c48c4Sopenharmony_ci 8da0c48c4Sopenharmony_ci * the GNU Lesser General Public License as published by the Free 9da0c48c4Sopenharmony_ci Software Foundation; either version 3 of the License, or (at 10da0c48c4Sopenharmony_ci your option) any later version 11da0c48c4Sopenharmony_ci 12da0c48c4Sopenharmony_ci or 13da0c48c4Sopenharmony_ci 14da0c48c4Sopenharmony_ci * the GNU General Public License as published by the Free 15da0c48c4Sopenharmony_ci Software Foundation; either version 2 of the License, or (at 16da0c48c4Sopenharmony_ci your option) any later version 17da0c48c4Sopenharmony_ci 18da0c48c4Sopenharmony_ci or both in parallel, as here. 19da0c48c4Sopenharmony_ci 20da0c48c4Sopenharmony_ci elfutils is distributed in the hope that it will be useful, but 21da0c48c4Sopenharmony_ci WITHOUT ANY WARRANTY; without even the implied warranty of 22da0c48c4Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23da0c48c4Sopenharmony_ci General Public License for more details. 24da0c48c4Sopenharmony_ci 25da0c48c4Sopenharmony_ci You should have received copies of the GNU General Public License and 26da0c48c4Sopenharmony_ci the GNU Lesser General Public License along with this program. If 27da0c48c4Sopenharmony_ci not, see <http://www.gnu.org/licenses/>. */ 28da0c48c4Sopenharmony_ci 29da0c48c4Sopenharmony_ci#ifndef _LIBDWELF_H 30da0c48c4Sopenharmony_ci#define _LIBDWELF_H 1 31da0c48c4Sopenharmony_ci 32da0c48c4Sopenharmony_ci#include "libdw.h" 33da0c48c4Sopenharmony_ci 34da0c48c4Sopenharmony_ci#ifdef __cplusplus 35da0c48c4Sopenharmony_ciextern "C" { 36da0c48c4Sopenharmony_ci#endif 37da0c48c4Sopenharmony_ci 38da0c48c4Sopenharmony_ci/* DWARF ELF Low-level Functions (dwelf). 39da0c48c4Sopenharmony_ci Functions starting with dwelf_elf will take a (libelf) Elf object as 40da0c48c4Sopenharmony_ci first argument and might set elf_errno on error. Functions starting 41da0c48c4Sopenharmony_ci with dwelf_dwarf will take a (libdw) Dwarf object as first argument 42da0c48c4Sopenharmony_ci and might set dwarf_errno on error. */ 43da0c48c4Sopenharmony_ci 44da0c48c4Sopenharmony_ci/* Returns the name and the CRC32 of the separate debug file from the 45da0c48c4Sopenharmony_ci .gnu_debuglink section if found in the ELF. Return NULL if the ELF 46da0c48c4Sopenharmony_ci file didn't have a .gnu_debuglink section, had malformed data in the 47da0c48c4Sopenharmony_ci section or some other error occurred. */ 48da0c48c4Sopenharmony_ciextern const char *dwelf_elf_gnu_debuglink (Elf *elf, GElf_Word *crc); 49da0c48c4Sopenharmony_ci 50da0c48c4Sopenharmony_ci/* Returns the name and build ID from the .gnu_debugaltlink section if 51da0c48c4Sopenharmony_ci found in the ELF. On success, pointers to the name and build ID 52da0c48c4Sopenharmony_ci are written to *NAMEP and *BUILDID_P, and the positive length of 53da0c48c4Sopenharmony_ci the build ID is returned. Returns 0 if the ELF lacks a 54da0c48c4Sopenharmony_ci .gnu_debugaltlink section. Returns -1 in case of malformed data or 55da0c48c4Sopenharmony_ci other errors. */ 56da0c48c4Sopenharmony_ciextern ssize_t dwelf_dwarf_gnu_debugaltlink (Dwarf *dwarf, 57da0c48c4Sopenharmony_ci const char **namep, 58da0c48c4Sopenharmony_ci const void **build_idp); 59da0c48c4Sopenharmony_ci 60da0c48c4Sopenharmony_ci/* Returns the build ID as found in a NT_GNU_BUILD_ID note from either 61da0c48c4Sopenharmony_ci a SHT_NOTE section or from a PT_NOTE segment if the ELF file 62da0c48c4Sopenharmony_ci doesn't contain any section headers. On success a pointer to the 63da0c48c4Sopenharmony_ci build ID is written to *BUILDID_P, and the positive length of the 64da0c48c4Sopenharmony_ci build ID is returned. Returns 0 if the ELF lacks a NT_GNU_BUILD_ID 65da0c48c4Sopenharmony_ci note. Returns -1 in case of malformed data or other errors. */ 66da0c48c4Sopenharmony_ciextern ssize_t dwelf_elf_gnu_build_id (Elf *elf, const void **build_idp); 67da0c48c4Sopenharmony_ci 68da0c48c4Sopenharmony_ci/* Returns the size of the uncompressed data of a GNU compressed 69da0c48c4Sopenharmony_ci section. The section name should start with .zdebug (but this 70da0c48c4Sopenharmony_ci isn't checked by this function). If the section isn't compressed 71da0c48c4Sopenharmony_ci (the section data doesn't start with ZLIB) -1 is returned. If an 72da0c48c4Sopenharmony_ci error occurred -1 is returned and elf_errno is set. */ 73da0c48c4Sopenharmony_ciextern ssize_t dwelf_scn_gnu_compressed_size (Elf_Scn *scn); 74da0c48c4Sopenharmony_ci 75da0c48c4Sopenharmony_ci/* ELF/DWARF string table handling. */ 76da0c48c4Sopenharmony_citypedef struct Dwelf_Strtab Dwelf_Strtab; 77da0c48c4Sopenharmony_citypedef struct Dwelf_Strent Dwelf_Strent; 78da0c48c4Sopenharmony_ci 79da0c48c4Sopenharmony_ci/* Create a new ELF/DWARF string table object in memory. ELF string 80da0c48c4Sopenharmony_ci tables have a required zero length null string at offset zero. 81da0c48c4Sopenharmony_ci DWARF string tables don't require such a null entry (unless they 82da0c48c4Sopenharmony_ci are shared with an ELF string table). If NULLSTR is true then a 83da0c48c4Sopenharmony_ci null entry is always created (even if the string table is empty 84da0c48c4Sopenharmony_ci otherwise). */ 85da0c48c4Sopenharmony_ciextern Dwelf_Strtab *dwelf_strtab_init (bool nullstr); 86da0c48c4Sopenharmony_ci 87da0c48c4Sopenharmony_ci/* Add string STR to string table ST. Returns NULL if no memory could 88da0c48c4Sopenharmony_ci be allocated. The given STR is owned by the called and must be 89da0c48c4Sopenharmony_ci valid till dwelf_strtab_free is called. dwelf_strtab_finalize 90da0c48c4Sopenharmony_ci might copy the string into the final table and dwelf_strent_str 91da0c48c4Sopenharmony_ci might return it, or a reference to an identical copy/substring 92da0c48c4Sopenharmony_ci added to the string table. */ 93da0c48c4Sopenharmony_ciextern Dwelf_Strent *dwelf_strtab_add (Dwelf_Strtab *st, const char *str) 94da0c48c4Sopenharmony_ci __nonnull_attribute__ (1, 2); 95da0c48c4Sopenharmony_ci 96da0c48c4Sopenharmony_ci/* This is an optimized version of dwelf_strtab_add if the length of 97da0c48c4Sopenharmony_ci the string is already known. LEN is the length of STR including 98da0c48c4Sopenharmony_ci zero terminator. Calling dwelf_strtab_add (st, str) is similar to 99da0c48c4Sopenharmony_ci calling dwelf_strtab_len (st, str, strlen (str) + 1). */ 100da0c48c4Sopenharmony_ciextern Dwelf_Strent *dwelf_strtab_add_len (Dwelf_Strtab *st, 101da0c48c4Sopenharmony_ci const char *str, size_t len) 102da0c48c4Sopenharmony_ci __nonnull_attribute__ (1, 2); 103da0c48c4Sopenharmony_ci 104da0c48c4Sopenharmony_ci/* Finalize string table ST and store size and memory location 105da0c48c4Sopenharmony_ci information in DATA d_size and d_buf. DATA d_type will be set to 106da0c48c4Sopenharmony_ci ELF_T_BYTE, d_off will be zero, d_align will be 1 and d_version 107da0c48c4Sopenharmony_ci will be set to EV_CURRENT. If no memory could be allocated NULL is 108da0c48c4Sopenharmony_ci returned and DATA->d_buf will be set to NULL. Otherwise DATA will 109da0c48c4Sopenharmony_ci be returned. */ 110da0c48c4Sopenharmony_ciextern Elf_Data *dwelf_strtab_finalize (Dwelf_Strtab *st, 111da0c48c4Sopenharmony_ci Elf_Data *data) 112da0c48c4Sopenharmony_ci __nonnull_attribute__ (1, 2); 113da0c48c4Sopenharmony_ci 114da0c48c4Sopenharmony_ci/* Get offset in string table for string associated with entry. Only 115da0c48c4Sopenharmony_ci valid after dwelf_strtab_finalize has been called. */ 116da0c48c4Sopenharmony_ciextern size_t dwelf_strent_off (Dwelf_Strent *se) 117da0c48c4Sopenharmony_ci __nonnull_attribute__ (1); 118da0c48c4Sopenharmony_ci 119da0c48c4Sopenharmony_ci/* Return the string associated with the entry. */ 120da0c48c4Sopenharmony_ciextern const char *dwelf_strent_str (Dwelf_Strent *se) 121da0c48c4Sopenharmony_ci __nonnull_attribute__ (1); 122da0c48c4Sopenharmony_ci 123da0c48c4Sopenharmony_ci/* Free resources allocated for the string table. This invalidates 124da0c48c4Sopenharmony_ci any Dwelf_Strent references returned earlier. */ 125da0c48c4Sopenharmony_ciextern void dwelf_strtab_free (Dwelf_Strtab *st) 126da0c48c4Sopenharmony_ci __nonnull_attribute__ (1); 127da0c48c4Sopenharmony_ci 128da0c48c4Sopenharmony_ci/* Creates a read-only Elf handle from the given file handle. The 129da0c48c4Sopenharmony_ci file may be compressed and/or contain a linux kernel image header, 130da0c48c4Sopenharmony_ci in which case it is eagerly decompressed in full and the Elf handle 131da0c48c4Sopenharmony_ci is created as if created with elf_memory (). On decompression or 132da0c48c4Sopenharmony_ci file errors NULL is returned (and elf_errno will be set). If there 133da0c48c4Sopenharmony_ci was no error, but the file is not an ELF file, then an ELF_K_NONE 134da0c48c4Sopenharmony_ci Elf handle is returned (just like with elf_begin). The Elf handle 135da0c48c4Sopenharmony_ci should be closed with elf_end (). The file handle will not be 136da0c48c4Sopenharmony_ci closed. */ 137da0c48c4Sopenharmony_ciextern Elf *dwelf_elf_begin (int fd); 138da0c48c4Sopenharmony_ci 139da0c48c4Sopenharmony_ci/* Returns a human readable string for the given ELF header e_machine 140da0c48c4Sopenharmony_ci value, or NULL if the given number isn't currently known. */ 141da0c48c4Sopenharmony_ciextern const char *dwelf_elf_e_machine_string (int machine); 142da0c48c4Sopenharmony_ci 143da0c48c4Sopenharmony_ci#ifdef __cplusplus 144da0c48c4Sopenharmony_ci} 145da0c48c4Sopenharmony_ci#endif 146da0c48c4Sopenharmony_ci 147da0c48c4Sopenharmony_ci#endif /* libdwelf.h */ 148