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