Phoenix
Object-oriented orthogonally persistent operating system
|
00001 /* 00002 * libelf.h - public header file for libelf. 00003 * Copyright (C) 1995 - 2008 Michael Riepe 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 00018 */ 00019 00020 /* @(#) $Id: libelf.h 172 2010-12-13 07:54:26Z vagran $ */ 00021 00022 #ifndef _LIBELF_H 00023 #define _LIBELF_H 00024 00025 #include <types.h> 00026 00027 #include <elf/sys_elf.h> 00028 00029 #if defined __GNUC__ && !defined __cplusplus 00030 #define DEPRECATED __attribute__((deprecated)) 00031 #else 00032 #define DEPRECATED /* nothing */ 00033 #endif 00034 00035 #ifdef __cplusplus 00036 extern "C" { 00037 #endif /* __cplusplus */ 00038 00039 /* 00040 * Commands 00041 */ 00042 typedef enum { 00043 ELF_C_NULL = 0, /* must be first, 0 */ 00044 ELF_C_READ, 00045 ELF_C_WRITE, 00046 ELF_C_CLR, 00047 ELF_C_SET, 00048 ELF_C_FDDONE, 00049 ELF_C_FDREAD, 00050 ELF_C_RDWR, 00051 ELF_C_NUM /* must be last */ 00052 } Elf_Cmd; 00053 00054 /* 00055 * Flags 00056 */ 00057 #define ELF_F_DIRTY 0x1 00058 #define ELF_F_LAYOUT 0x4 00059 /* 00060 * Allow sections to overlap when ELF_F_LAYOUT is in effect. 00061 * Note that this flag is NOT portable, and that it may render 00062 * the output file unusable. Use with extreme caution! 00063 */ 00064 #define ELF_F_LAYOUT_OVERLAP 0x10000000 00065 00066 /* 00067 * File types 00068 */ 00069 typedef enum { 00070 ELF_K_NONE = 0, /* must be first, 0 */ 00071 ELF_K_AR, 00072 ELF_K_COFF, 00073 ELF_K_ELF, 00074 ELF_K_NUM /* must be last */ 00075 } Elf_Kind; 00076 00077 /* 00078 * Data types 00079 */ 00080 typedef enum { 00081 ELF_T_BYTE = 0, /* must be first, 0 */ 00082 ELF_T_ADDR, 00083 ELF_T_DYN, 00084 ELF_T_EHDR, 00085 ELF_T_HALF, 00086 ELF_T_OFF, 00087 ELF_T_PHDR, 00088 ELF_T_RELA, 00089 ELF_T_REL, 00090 ELF_T_SHDR, 00091 ELF_T_SWORD, 00092 ELF_T_SYM, 00093 ELF_T_WORD, 00094 /* 00095 * New stuff for 64-bit. 00096 * 00097 * Most implementations add ELF_T_SXWORD after ELF_T_SWORD 00098 * which breaks binary compatibility with earlier versions. 00099 * If this causes problems for you, contact me. 00100 */ 00101 ELF_T_SXWORD, 00102 ELF_T_XWORD, 00103 /* 00104 * Symbol versioning. Sun broke binary compatibility (again!), 00105 * but I won't. 00106 */ 00107 ELF_T_VDEF, 00108 ELF_T_VNEED, 00109 ELF_T_NUM /* must be last */ 00110 } Elf_Type; 00111 00112 /* 00113 * Elf descriptor 00114 */ 00115 typedef struct Elf_s Elf; 00116 00117 /* 00118 * Section descriptor 00119 */ 00120 typedef struct Elf_Scn_s Elf_Scn; 00121 00122 /* 00123 * Archive member header 00124 */ 00125 typedef struct { 00126 char* ar_name; 00127 time_t ar_date; 00128 long ar_uid; 00129 long ar_gid; 00130 unsigned long ar_mode; 00131 off_t ar_size; 00132 char* ar_rawname; 00133 } Elf_Arhdr; 00134 00135 /* 00136 * Archive symbol table 00137 */ 00138 typedef struct { 00139 char* as_name; 00140 size_t as_off; 00141 unsigned long as_hash; 00142 } Elf_Arsym; 00143 00144 /* 00145 * Data descriptor 00146 */ 00147 typedef struct { 00148 void* d_buf; 00149 Elf_Type d_type; 00150 size_t d_size; 00151 off_t d_off; 00152 size_t d_align; 00153 unsigned d_version; 00154 } Elf_Data; 00155 00156 /* Portable memory interface */ 00157 00158 /* Allocate memory block. */ 00159 typedef void *(*Elf_Malloc)(size_t size); 00160 /* Reallocate memory block. */ 00161 typedef void *(*Elf_Mrealloc)(void *ptr, size_t size); 00162 /* Free memory block. */ 00163 typedef void (*Elf_Mfree)(void *ptr); 00164 00165 extern Elf_Malloc elf_malloc; 00166 extern Elf_Mrealloc elf_mrealloc; 00167 extern Elf_Mfree elf_mfree; 00168 00169 /* Portable file interface */ 00170 00171 struct Elf_File_s; 00172 typedef struct Elf_File_s Elf_File; 00173 00174 /* 'whence' parameter for Elf_Seek function */ 00175 typedef enum { 00176 ELF_SEEK_SET, 00177 ELF_SEEK_END, 00178 ELF_SEEK_CUR 00179 } Elf_Seek_Whence; 00180 00181 /* Seek to new current position. */ 00182 typedef size_t (*Elf_Seek)(Elf_File *file, size_t offset, Elf_Seek_Whence whence); 00183 /* Read file data. */ 00184 typedef size_t (*Elf_Read)(Elf_File *file, char *buffer, size_t len); 00185 /* Write file data. */ 00186 typedef size_t (*Elf_Write)(Elf_File *file, char *buffer, size_t len); 00187 /* Close file. All occupied memory should be freed. */ 00188 typedef void (*Elf_Close)(Elf_File *file); 00189 00190 /* 00191 * Portable file object. 00192 */ 00193 struct Elf_File_s { 00194 void *f_priv; /* Private data for underlying implementation. */ 00195 /* Public methods. */ 00196 Elf_Seek Seek; 00197 Elf_Read Read; 00198 Elf_Write Write; 00199 Elf_Close Close; 00200 }; 00201 00202 /* 00203 * Function declarations 00204 */ 00205 extern Elf *elf_begin(Elf_File *fd, Elf_Cmd __cmd, Elf *__ref); 00206 extern Elf *elf_memory(char *__image, size_t __size); 00207 extern int elf_cntl(Elf *__elf, Elf_Cmd __cmd); 00208 extern int elf_end(Elf *__elf); 00209 extern const char *elf_errmsg(int __err); 00210 extern int elf_errno(void); 00211 extern void elf_fill(int __fill); 00212 extern unsigned elf_flagdata(Elf_Data *__data, Elf_Cmd __cmd, unsigned __flags); 00213 extern unsigned elf_flagehdr(Elf *__elf, Elf_Cmd __cmd, unsigned __flags); 00214 extern unsigned elf_flagelf(Elf *__elf, Elf_Cmd __cmd, unsigned __flags); 00215 extern unsigned elf_flagphdr(Elf *__elf, Elf_Cmd __cmd, unsigned __flags); 00216 extern unsigned elf_flagscn(Elf_Scn *__scn, Elf_Cmd __cmd, unsigned __flags); 00217 extern unsigned elf_flagshdr(Elf_Scn *__scn, Elf_Cmd __cmd, unsigned __flags); 00218 extern size_t elf32_fsize(Elf_Type __type, size_t __count, unsigned __ver); 00219 extern Elf_Arhdr *elf_getarhdr(Elf *__elf); 00220 extern Elf_Arsym *elf_getarsym(Elf *__elf, size_t *__ptr); 00221 extern off_t elf_getbase(Elf *__elf); 00222 extern Elf_Data *elf_getdata(Elf_Scn *__scn, Elf_Data *__data); 00223 extern Elf32_Ehdr *elf32_getehdr(Elf *__elf); 00224 extern char *elf_getident(Elf *__elf, size_t *__ptr); 00225 extern Elf32_Phdr *elf32_getphdr(Elf *__elf); 00226 extern Elf_Scn *elf_getscn(Elf *__elf, size_t __index); 00227 extern Elf32_Shdr *elf32_getshdr(Elf_Scn *__scn); 00228 extern unsigned long elf_hash(const unsigned char *__name); 00229 extern Elf_Kind elf_kind(Elf *__elf); 00230 extern size_t elf_ndxscn(Elf_Scn *__scn); 00231 extern Elf_Data *elf_newdata(Elf_Scn *__scn); 00232 extern Elf32_Ehdr *elf32_newehdr(Elf *__elf); 00233 extern Elf32_Phdr *elf32_newphdr(Elf *__elf, size_t __count); 00234 extern Elf_Scn *elf_newscn(Elf *__elf); 00235 extern Elf_Cmd elf_next(Elf *__elf); 00236 extern Elf_Scn *elf_nextscn(Elf *__elf, Elf_Scn *__scn); 00237 extern size_t elf_rand(Elf *__elf, size_t __offset); 00238 extern Elf_Data *elf_rawdata(Elf_Scn *__scn, Elf_Data *__data); 00239 extern char *elf_rawfile(Elf *__elf, size_t *__ptr); 00240 extern char *elf_strptr(Elf *__elf, size_t __section, size_t __offset); 00241 extern off_t elf_update(Elf *__elf, Elf_Cmd __cmd); 00242 extern unsigned elf_version(unsigned __ver); 00243 extern Elf_Data *elf32_xlatetof(Elf_Data *__dst, const Elf_Data *__src, unsigned __encode); 00244 extern Elf_Data *elf32_xlatetom(Elf_Data *__dst, const Elf_Data *__src, unsigned __encode); 00245 00246 /* 00247 * Additional functions found on Solaris 00248 */ 00249 extern long elf32_checksum(Elf *__elf); 00250 00251 #if __LIBELF64 00252 /* 00253 * 64-bit ELF functions 00254 * Not available on all platforms 00255 */ 00256 extern Elf64_Ehdr *elf64_getehdr(Elf *__elf); 00257 extern Elf64_Ehdr *elf64_newehdr(Elf *__elf); 00258 extern Elf64_Phdr *elf64_getphdr(Elf *__elf); 00259 extern Elf64_Phdr *elf64_newphdr(Elf *__elf, size_t __count); 00260 extern Elf64_Shdr *elf64_getshdr(Elf_Scn *__scn); 00261 extern size_t elf64_fsize(Elf_Type __type, size_t __count, unsigned __ver); 00262 extern Elf_Data *elf64_xlatetof(Elf_Data *__dst, const Elf_Data *__src, unsigned __encode); 00263 extern Elf_Data *elf64_xlatetom(Elf_Data *__dst, const Elf_Data *__src, unsigned __encode); 00264 00265 /* 00266 * Additional functions found on Solaris 00267 */ 00268 extern long elf64_checksum(Elf *__elf); 00269 00270 #endif /* __LIBELF64 */ 00271 00272 /* 00273 * ELF format extensions 00274 * 00275 * Return -1 on failure, 0 on success. 00276 */ 00277 extern int elf_getphdrnum(Elf *__elf, size_t *__resultp); 00278 extern int elf_getshdrnum(Elf *__elf, size_t *__resultp); 00279 extern int elf_getshdrstrndx(Elf *__elf, size_t *__resultp); 00280 00281 /* 00282 * Convenience functions 00283 * 00284 * elfx_update_shstrndx is elf_getshstrndx's counterpart. 00285 * It should be used to set the e_shstrndx member. 00286 * There is no update function for e_shnum or e_phnum 00287 * because libelf handles them internally. 00288 */ 00289 extern int elfx_update_shstrndx(Elf *__elf, size_t __index); 00290 00291 /* 00292 * Experimental extensions: 00293 * 00294 * elfx_movscn() moves section `__scn' directly after section `__after'. 00295 * elfx_remscn() removes section `__scn'. Both functions update 00296 * the section indices; elfx_remscn() also adjusts the ELF header's 00297 * e_shnum member. The application is responsible for updating other 00298 * data (in particular, e_shstrndx and the section headers' sh_link and 00299 * sh_info members). 00300 * 00301 * elfx_movscn() returns the new index of the moved section. 00302 * elfx_remscn() returns the original index of the removed section. 00303 * A return value of zero indicates an error. 00304 */ 00305 extern size_t elfx_movscn(Elf *__elf, Elf_Scn *__scn, Elf_Scn *__after); 00306 extern size_t elfx_remscn(Elf *__elf, Elf_Scn *__scn); 00307 00308 /* 00309 * elf_delscn() is obsolete. Please use elfx_remscn() instead. 00310 */ 00311 extern size_t elf_delscn(Elf *__elf, Elf_Scn *__scn); 00312 00313 #ifdef __cplusplus 00314 } 00315 #endif /* __cplusplus */ 00316 00317 #endif /* _LIBELF_H */