1 /* Interface for libelf. 2 Copyright (C) 1998, 1999, 2000, 2002, 2004 Red Hat, Inc. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, version 2. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program; if not, write to the Free Software Foundation, 15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 17 #ifndef _LIBELF_H 18 #define _LIBELF_H 1 19 20 #include <sys/types.h> 21 22 /* Get the ELF types. */ 23 #include <elf.h> 24 25 26 /* Known translation types. */ 27 typedef enum 28 { 29 ELF_T_BYTE, /* unsigned char */ 30 ELF_T_ADDR, /* Elf32_Addr, Elf64_Addr, ... */ 31 ELF_T_DYN, /* Dynamic section record. */ 32 ELF_T_EHDR, /* ELF header. */ 33 ELF_T_HALF, /* Elf32_Half, Elf64_Half, ... */ 34 ELF_T_OFF, /* Elf32_Off, Elf64_Off, ... */ 35 ELF_T_PHDR, /* Program header. */ 36 ELF_T_RELA, /* Relocation entry with addend. */ 37 ELF_T_REL, /* Relocation entry. */ 38 ELF_T_SHDR, /* Section header. */ 39 ELF_T_SWORD, /* Elf32_Sword, Elf64_Sword, ... */ 40 ELF_T_SYM, /* Symbol record. */ 41 ELF_T_WORD, /* Elf32_Word, Elf64_Word, ... */ 42 ELF_T_XWORD, /* Elf32_Xword, Elf64_Xword, ... */ 43 ELF_T_SXWORD, /* Elf32_Sxword, Elf64_Sxword, ... */ 44 ELF_T_VDEF, /* Elf32_Verdef, Elf64_Verdef, ... */ 45 ELF_T_VDAUX, /* Elf32_Verdaux, Elf64_Verdaux, ... */ 46 ELF_T_VNEED, /* Elf32_Verneed, Elf64_Verneed, ... */ 47 ELF_T_VNAUX, /* Elf32_Vernaux, Elf64_Vernaux, ... */ 48 ELF_T_NHDR, /* Elf32_Nhdr, Elf64_Nhdr, ... */ 49 ELF_T_SYMINFO, /* Elf32_Syminfo, Elf64_Syminfo, ... */ 50 ELF_T_MOVE, /* Elf32_Move, Elf64_Move, ... */ 51 ELF_T_LIB, /* Elf32_Lib, Elf64_Lib, ... */ 52 /* Keep this the last entry. */ 53 ELF_T_NUM 54 } Elf_Type; 55 56 /* Descriptor for data to be converted to or from memory format. */ 57 typedef struct 58 { 59 void *d_buf; /* Pointer to the actual data. */ 60 Elf_Type d_type; /* Type of this piece of data. */ 61 unsigned int d_version; /* ELF version. */ 62 size_t d_size; /* Size in bytes. */ 63 off_t d_off; /* Offset into section. */ 64 size_t d_align; /* Alignment in section. */ 65 } Elf_Data; 66 67 68 /* Commands for `...'. */ 69 typedef enum 70 { 71 ELF_C_NULL, /* Nothing, terminate, or compute only. */ 72 ELF_C_READ, /* Read .. */ 73 ELF_C_RDWR, /* Read and write .. */ 74 ELF_C_WRITE, /* Write .. */ 75 ELF_C_CLR, /* Clear flag. */ 76 ELF_C_SET, /* Set flag. */ 77 ELF_C_FDDONE, /* Signal that file descriptor will not be 78 used anymore. */ 79 ELF_C_FDREAD, /* Read rest of data so that file descriptor 80 is not used anymore. */ 81 /* The following are extensions. */ 82 ELF_C_READ_MMAP, /* Read, but mmap the file if possible. */ 83 ELF_C_RDWR_MMAP, /* Read and write, with mmap. */ 84 ELF_C_WRITE_MMAP, /* Write, with mmap. */ 85 ELF_C_READ_MMAP_PRIVATE, /* Read, but memory is writable, results are 86 not written to the file. */ 87 ELF_C_EMPTY, /* Copy basic file data but not the content. */ 88 /* Keep this the last entry. */ 89 ELF_C_NUM 90 } Elf_Cmd; 91 92 93 /* Flags for the ELF structures. */ 94 enum 95 { 96 ELF_F_DIRTY = 0x1, 97 #define ELF_F_DIRTY ELF_F_DIRTY 98 ELF_F_LAYOUT = 0x4, 99 #define ELF_F_LAYOUT ELF_F_LAYOUT 100 ELF_F_PERMISSIVE = 0x8 101 #define ELF_F_PERMISSIVE ELF_F_PERMISSIVE 102 }; 103 104 105 /* Identification values for recognized object files. */ 106 typedef enum 107 { 108 ELF_K_NONE, /* Unknown. */ 109 ELF_K_AR, /* Archive. */ 110 ELF_K_COFF, /* Stupid old COFF. */ 111 ELF_K_ELF, /* ELF file. */ 112 /* Keep this the last entry. */ 113 ELF_K_NUM 114 } Elf_Kind; 115 116 117 /* Archive member header. */ 118 typedef struct 119 { 120 char *ar_name; /* Name of archive member. */ 121 time_t ar_date; /* File date. */ 122 uid_t ar_uid; /* User ID. */ 123 gid_t ar_gid; /* Group ID. */ 124 mode_t ar_mode; /* File mode. */ 125 off_t ar_size; /* File size. */ 126 char *ar_rawname; /* Original name of archive member. */ 127 } Elf_Arhdr; 128 129 130 /* Archive symbol table entry. */ 131 typedef struct 132 { 133 char *as_name; /* Symbol name. */ 134 size_t as_off; /* Offset for this file in the archive. */ 135 unsigned long int as_hash; /* Hash value of the name. */ 136 } Elf_Arsym; 137 138 139 /* Descriptor for the ELF file. */ 140 typedef struct Elf Elf; 141 142 /* Descriptor for ELF file section. */ 143 typedef struct Elf_Scn Elf_Scn; 144 145 146 #ifdef __cplusplus 147 extern "C" { 148 #endif 149 150 /* Return descriptor for ELF file to work according to CMD. */ 151 extern Elf *elf_begin (int __fildes, Elf_Cmd __cmd, Elf *__ref); 152 153 /* Create a clone of an existing ELF descriptor. */ 154 extern Elf *elf_clone (Elf *__elf, Elf_Cmd __cmd); 155 156 /* Create descriptor for memory region. */ 157 extern Elf *elf_memory (char *__image, size_t __size); 158 159 /* Advance archive descriptor to next element. */ 160 extern Elf_Cmd elf_next (Elf *__elf); 161 162 /* Free resources allocated for ELF. */ 163 extern int elf_end (Elf *__elf); 164 165 /* Update ELF descriptor and write file to disk. */ 166 extern off_t elf_update (Elf *__elf, Elf_Cmd __cmd); 167 168 /* Determine what kind of file is associated with ELF. */ 169 extern Elf_Kind elf_kind (Elf *__elf) __attribute__ ((__pure__)); 170 171 /* Get the base offset for an object file. */ 172 extern off_t elf_getbase (Elf *__elf); 173 174 175 /* Retrieve file identification data. */ 176 extern char *elf_getident (Elf *__elf, size_t *__ptr); 177 178 /* Retrieve class-dependent object file header. */ 179 extern Elf32_Ehdr *elf32_getehdr (Elf *__elf); 180 /* Similar but this time the binary calls is ELFCLASS64. */ 181 extern Elf64_Ehdr *elf64_getehdr (Elf *__elf); 182 183 /* Create ELF header if none exists. */ 184 extern Elf32_Ehdr *elf32_newehdr (Elf *__elf); 185 /* Similar but this time the binary calls is ELFCLASS64. */ 186 extern Elf64_Ehdr *elf64_newehdr (Elf *__elf); 187 188 /* Retrieve class-dependent program header table. */ 189 extern Elf32_Phdr *elf32_getphdr (Elf *__elf); 190 /* Similar but this time the binary calls is ELFCLASS64. */ 191 extern Elf64_Phdr *elf64_getphdr (Elf *__elf); 192 193 /* Create ELF program header. */ 194 extern Elf32_Phdr *elf32_newphdr (Elf *__elf, size_t __cnt); 195 /* Similar but this time the binary calls is ELFCLASS64. */ 196 extern Elf64_Phdr *elf64_newphdr (Elf *__elf, size_t __cnt); 197 198 199 /* Get section at INDEX. */ 200 extern Elf_Scn *elf_getscn (Elf *__elf, size_t __index); 201 202 /* Get index of section. */ 203 extern size_t elf_ndxscn (Elf_Scn *__scn); 204 205 /* Get section with next section index. */ 206 extern Elf_Scn *elf_nextscn (Elf *__elf, Elf_Scn *__scn); 207 208 /* Create a new section and append it at the end of the table. */ 209 extern Elf_Scn *elf_newscn (Elf *__elf); 210 211 /* Get the number of sections in the ELF file. If the file uses more 212 sections than can be represented in the e_shnum field of the ELF 213 header the information from the sh_size field in the zeroth section 214 header is used. */ 215 extern int elf_getshnum (Elf *__elf, size_t *__dst); 216 217 218 /* Get the section index of the section header string table in the ELF 219 file. If the index cannot be represented in the e_shnum field of 220 the ELF header the information from the sh_link field in the zeroth 221 section header is used. */ 222 extern int elf_getshstrndx (Elf *__elf, size_t *__dst); 223 224 225 /* Retrieve section header of ELFCLASS32 binary. */ 226 extern Elf32_Shdr *elf32_getshdr (Elf_Scn *__scn); 227 /* Similar for ELFCLASS64. */ 228 extern Elf64_Shdr *elf64_getshdr (Elf_Scn *__scn); 229 230 231 /* Set or clear flags for ELF file. */ 232 extern unsigned int elf_flagelf (Elf *__elf, Elf_Cmd __cmd, 233 unsigned int __flags); 234 /* Similarly for the ELF header. */ 235 extern unsigned int elf_flagehdr (Elf *__elf, Elf_Cmd __cmd, 236 unsigned int __flags); 237 /* Similarly for the ELF program header. */ 238 extern unsigned int elf_flagphdr (Elf *__elf, Elf_Cmd __cmd, 239 unsigned int __flags); 240 /* Similarly for the given ELF section. */ 241 extern unsigned int elf_flagscn (Elf_Scn *__scn, Elf_Cmd __cmd, 242 unsigned int __flags); 243 /* Similarly for the given ELF data. */ 244 extern unsigned int elf_flagdata (Elf_Data *__data, Elf_Cmd __cmd, 245 unsigned int __flags); 246 /* Similarly for the given ELF section header. */ 247 extern unsigned int elf_flagshdr (Elf_Scn *__scn, Elf_Cmd __cmd, 248 unsigned int __flags); 249 250 251 /* Get data from section while translating from file representation 252 to memory representation. */ 253 extern Elf_Data *elf_getdata (Elf_Scn *__scn, Elf_Data *__data); 254 255 /* Get uninterpreted section content. */ 256 extern Elf_Data *elf_rawdata (Elf_Scn *__scn, Elf_Data *__data); 257 258 /* Create new data descriptor for section SCN. */ 259 extern Elf_Data *elf_newdata (Elf_Scn *__scn); 260 261 262 /* Return pointer to string at OFFSET in section INDEX. */ 263 extern char *elf_strptr (Elf *__elf, size_t __index, size_t __offset); 264 265 266 /* Return header of archive. */ 267 extern Elf_Arhdr *elf_getarhdr (Elf *__elf); 268 269 /* Select archive element at OFFSET. */ 270 extern size_t elf_rand (Elf *__elf, size_t __offset); 271 272 /* Get symbol table of archhive. */ 273 extern Elf_Arsym *elf_getarsym (Elf *__elf, size_t *__ptr); 274 275 276 /* Control ELF descriptor. */ 277 extern int elf_cntl (Elf *__elf, Elf_Cmd __cmd); 278 279 /* Retrieve uninterpreted file contents. */ 280 extern char *elf_rawfile (Elf *__elf, size_t *__ptr); 281 282 283 /* Return size of array of COUNT elements of the type denoted by TYPE 284 in the external representation. The binary class is taken from ELF. 285 The result is based on version VERSION of the ELF standard. */ 286 extern size_t elf32_fsize (Elf_Type __type, size_t __count, 287 unsigned int __version) 288 __attribute__ ((__const__)); 289 /* Similar but this time the binary calls is ELFCLASS64. */ 290 extern size_t elf64_fsize (Elf_Type __type, size_t __count, 291 unsigned int __version) 292 __attribute__ ((__const__)); 293 294 295 /* Convert data structure from the representation in the file represented 296 by ELF to their memory representation. */ 297 extern Elf_Data *elf32_xlatetom (Elf_Data *__dest, const Elf_Data *__src, 298 unsigned int __encode); 299 /* Same for 64 bit class. */ 300 extern Elf_Data *elf64_xlatetom (Elf_Data *__dest, const Elf_Data *__src, 301 unsigned int __encode); 302 303 /* Convert data structure from to the representation in memory 304 represented by ELF file representation. */ 305 extern Elf_Data *elf32_xlatetof (Elf_Data *__dest, const Elf_Data *__src, 306 unsigned int __encode); 307 /* Same for 64 bit class. */ 308 extern Elf_Data *elf64_xlatetof (Elf_Data *__dest, const Elf_Data *__src, 309 unsigned int __encode); 310 311 312 /* Return error code of last failing function call. This value is kept 313 separately for each thread. */ 314 extern int elf_errno (void); 315 316 /* Return error string for ERROR. If ERROR is zero, return error string 317 for most recent error or NULL is none occurred. If ERROR is -1 the 318 behaviour is similar to the last case except that not NULL but a legal 319 string is returned. */ 320 extern const char *elf_errmsg (int __error); 321 322 323 /* Coordinate ELF library and application versions. */ 324 extern unsigned int elf_version (unsigned int __version); 325 326 /* Set fill bytes used to fill holes in data structures. */ 327 extern void elf_fill (int __fill); 328 329 /* Compute hash value. */ 330 extern unsigned long int elf_hash (const char *__string) 331 __attribute__ ((__pure__)); 332 333 334 /* Compute simple checksum from permanent parts of the ELF file. */ 335 extern long int elf32_checksum (Elf *__elf); 336 /* Similar but this time the binary calls is ELFCLASS64. */ 337 extern long int elf64_checksum (Elf *__elf); 338 339 #ifdef __cplusplus 340 } 341 #endif 342 343 #endif /* libelf.h */ 344