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