libelf.h revision 210345
1164190Sjkoshy/*- 2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy 3164190Sjkoshy * All rights reserved. 4164190Sjkoshy * 5164190Sjkoshy * Redistribution and use in source and binary forms, with or without 6164190Sjkoshy * modification, are permitted provided that the following conditions 7164190Sjkoshy * are met: 8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright 9164190Sjkoshy * notice, this list of conditions and the following disclaimer. 10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 11164190Sjkoshy * notice, this list of conditions and the following disclaimer in the 12164190Sjkoshy * documentation and/or other materials provided with the distribution. 13164190Sjkoshy * 14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17164190Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24164190Sjkoshy * SUCH DAMAGE. 25164190Sjkoshy * 26164190Sjkoshy * $FreeBSD: head/lib/libelf/libelf.h 210345 2010-07-21 12:14:50Z kaiw $ 27164190Sjkoshy */ 28164190Sjkoshy 29164190Sjkoshy#ifndef _LIBELF_H_ 30164190Sjkoshy#define _LIBELF_H_ 31164190Sjkoshy 32164190Sjkoshy#include <sys/types.h> 33164190Sjkoshy#include <sys/elf32.h> 34164190Sjkoshy#include <sys/elf64.h> 35164190Sjkoshy#include <sys/queue.h> 36164190Sjkoshy 37164190Sjkoshy/* Library private data structures */ 38164190Sjkoshytypedef struct _Elf Elf; 39164190Sjkoshytypedef struct _Elf_Scn Elf_Scn; 40164190Sjkoshy 41164190Sjkoshy/* File types */ 42164190Sjkoshytypedef enum { 43164190Sjkoshy ELF_K_NONE = 0, 44164190Sjkoshy ELF_K_AR, /* `ar' archives */ 45164190Sjkoshy ELF_K_COFF, /* COFF files (unsupported) */ 46164190Sjkoshy ELF_K_ELF, /* ELF files */ 47164190Sjkoshy ELF_K_NUM 48164190Sjkoshy} Elf_Kind; 49164190Sjkoshy 50164190Sjkoshy#define ELF_K_FIRST ELF_K_NONE 51164190Sjkoshy#define ELF_K_LAST ELF_K_NUM 52164190Sjkoshy 53164190Sjkoshy/* Data types */ 54164190Sjkoshytypedef enum { 55164190Sjkoshy ELF_T_ADDR, 56164190Sjkoshy ELF_T_BYTE, 57164190Sjkoshy ELF_T_CAP, 58164190Sjkoshy ELF_T_DYN, 59164190Sjkoshy ELF_T_EHDR, 60164190Sjkoshy ELF_T_HALF, 61164190Sjkoshy ELF_T_LWORD, 62164190Sjkoshy ELF_T_MOVE, 63164190Sjkoshy ELF_T_MOVEP, 64164190Sjkoshy ELF_T_NOTE, 65164190Sjkoshy ELF_T_OFF, 66164190Sjkoshy ELF_T_PHDR, 67164190Sjkoshy ELF_T_REL, 68164190Sjkoshy ELF_T_RELA, 69164190Sjkoshy ELF_T_SHDR, 70164190Sjkoshy ELF_T_SWORD, 71164190Sjkoshy ELF_T_SXWORD, 72164190Sjkoshy ELF_T_SYMINFO, 73164190Sjkoshy ELF_T_SYM, 74164190Sjkoshy ELF_T_VDEF, 75164190Sjkoshy ELF_T_VNEED, 76164190Sjkoshy ELF_T_WORD, 77164190Sjkoshy ELF_T_XWORD, 78210331Skaiw ELF_T_GNUHASH, /* GNU style hash tables. */ 79164190Sjkoshy ELF_T_NUM 80164190Sjkoshy} Elf_Type; 81164190Sjkoshy 82164190Sjkoshy#define ELF_T_FIRST ELF_T_ADDR 83210340Skaiw#define ELF_T_LAST ELF_T_GNUHASH 84164190Sjkoshy 85164190Sjkoshy/* Commands */ 86164190Sjkoshytypedef enum { 87164190Sjkoshy ELF_C_NULL = 0, 88164190Sjkoshy ELF_C_CLR, 89164190Sjkoshy ELF_C_FDDONE, 90164190Sjkoshy ELF_C_FDREAD, 91164190Sjkoshy ELF_C_RDWR, 92164190Sjkoshy ELF_C_READ, 93164190Sjkoshy ELF_C_SET, 94164190Sjkoshy ELF_C_WRITE, 95164190Sjkoshy ELF_C_NUM 96164190Sjkoshy} Elf_Cmd; 97164190Sjkoshy 98164190Sjkoshy#define ELF_C_FIRST ELF_C_NULL 99164190Sjkoshy#define ELF_C_LAST ELF_C_NUM 100164190Sjkoshy 101164190Sjkoshy/* 102164190Sjkoshy * An `Elf_Data' structure describes data in an 103164190Sjkoshy * ELF section. 104164190Sjkoshy */ 105164190Sjkoshytypedef struct _Elf_Data { 106164190Sjkoshy /* 107164190Sjkoshy * `Public' members that are part of the ELF(3) API. 108164190Sjkoshy */ 109164190Sjkoshy uint64_t d_align; 110164190Sjkoshy void *d_buf; 111164190Sjkoshy uint64_t d_off; 112164190Sjkoshy uint64_t d_size; 113164190Sjkoshy Elf_Type d_type; 114164190Sjkoshy unsigned int d_version; 115164190Sjkoshy 116164190Sjkoshy /* 117164190Sjkoshy * Members that are not part of the public API. 118164190Sjkoshy */ 119164190Sjkoshy Elf_Scn *d_scn; /* containing section */ 120164190Sjkoshy unsigned int d_flags; 121164190Sjkoshy STAILQ_ENTRY(_Elf_Data) d_next; 122164190Sjkoshy} Elf_Data; 123164190Sjkoshy 124164190Sjkoshy/* 125164190Sjkoshy * An `Elf_Arhdr' structure describes an archive 126164190Sjkoshy * header. 127164190Sjkoshy */ 128164190Sjkoshytypedef struct { 129164190Sjkoshy time_t ar_date; 130164190Sjkoshy char *ar_name; /* archive member name */ 131164190Sjkoshy gid_t ar_gid; 132164190Sjkoshy mode_t ar_mode; 133164190Sjkoshy char *ar_rawname; /* 'raw' member name */ 134164190Sjkoshy size_t ar_size; 135164190Sjkoshy uid_t ar_uid; 136164190Sjkoshy} Elf_Arhdr; 137164190Sjkoshy 138164190Sjkoshy/* 139164190Sjkoshy * An `Elf_Arsym' describes an entry in the archive 140164190Sjkoshy * symbol table. 141164190Sjkoshy */ 142164190Sjkoshytypedef struct { 143164190Sjkoshy off_t as_off; /* byte offset to member's header */ 144164190Sjkoshy unsigned long as_hash; /* elf_hash() value for name */ 145164190Sjkoshy char *as_name; /* null terminated symbol name */ 146164190Sjkoshy} Elf_Arsym; 147164190Sjkoshy 148164190Sjkoshy/* 149164190Sjkoshy * Error numbers. 150164190Sjkoshy */ 151164190Sjkoshy 152164190Sjkoshyenum Elf_Error { 153164190Sjkoshy ELF_E_NONE, /* No error */ 154164190Sjkoshy ELF_E_ARCHIVE, /* Malformed ar(1) archive */ 155164190Sjkoshy ELF_E_ARGUMENT, /* Invalid argument */ 156164190Sjkoshy ELF_E_CLASS, /* Mismatched ELF class */ 157164190Sjkoshy ELF_E_DATA, /* Invalid data descriptor */ 158164190Sjkoshy ELF_E_HEADER, /* Missing or malformed ELF header */ 159164190Sjkoshy ELF_E_IO, /* I/O error */ 160164190Sjkoshy ELF_E_LAYOUT, /* Layout constraint violation */ 161164190Sjkoshy ELF_E_MODE, /* Wrong mode for ELF descriptor */ 162164190Sjkoshy ELF_E_RANGE, /* Value out of range */ 163164190Sjkoshy ELF_E_RESOURCE, /* Resource exhaustion */ 164164190Sjkoshy ELF_E_SECTION, /* Invalid section descriptor */ 165164190Sjkoshy ELF_E_SEQUENCE, /* API calls out of sequence */ 166164190Sjkoshy ELF_E_UNIMPL, /* Feature is unimplemented */ 167164190Sjkoshy ELF_E_VERSION, /* Unknown API version */ 168164190Sjkoshy ELF_E_NUM /* Max error number */ 169164190Sjkoshy}; 170164190Sjkoshy 171164190Sjkoshy/* 172164190Sjkoshy * Flags defined by the API. 173164190Sjkoshy */ 174164190Sjkoshy 175164190Sjkoshy#define ELF_F_LAYOUT 0x001U /* application will layout the file */ 176164190Sjkoshy#define ELF_F_DIRTY 0x002U /* a section or ELF file is dirty */ 177164190Sjkoshy 178164190Sjkoshy__BEGIN_DECLS 179164190SjkoshyElf *elf_begin(int _fd, Elf_Cmd _cmd, Elf *_elf); 180164190Sjkoshyint elf_cntl(Elf *_elf, Elf_Cmd _cmd); 181164190Sjkoshyint elf_end(Elf *_elf); 182164190Sjkoshyconst char *elf_errmsg(int _error); 183164190Sjkoshyint elf_errno(void); 184164190Sjkoshyvoid elf_fill(int _fill); 185164190Sjkoshyunsigned int elf_flagdata(Elf_Data *_data, Elf_Cmd _cmd, unsigned int _flags); 186164190Sjkoshyunsigned int elf_flagehdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags); 187164190Sjkoshyunsigned int elf_flagelf(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags); 188164190Sjkoshyunsigned int elf_flagphdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags); 189164190Sjkoshyunsigned int elf_flagscn(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags); 190164190Sjkoshyunsigned int elf_flagshdr(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags); 191164190SjkoshyElf_Arhdr *elf_getarhdr(Elf *_elf); 192164190SjkoshyElf_Arsym *elf_getarsym(Elf *_elf, size_t *_ptr); 193164190Sjkoshyoff_t elf_getbase(Elf *_elf); 194164190SjkoshyElf_Data *elf_getdata(Elf_Scn *, Elf_Data *); 195164190Sjkoshychar *elf_getident(Elf *_elf, size_t *_ptr); 196210345Skaiwint elf_getphdrnum(Elf *_elf, size_t *_dst); 197210345Skaiwint elf_getphnum(Elf *_elf, size_t *_dst); /* Deprecated */ 198164190SjkoshyElf_Scn *elf_getscn(Elf *_elf, size_t _index); 199210345Skaiwint elf_getshdrnum(Elf *_elf, size_t *_dst); 200210345Skaiwint elf_getshnum(Elf *_elf, size_t *_dst); /* Deprecated */ 201210345Skaiwint elf_getshdrstrndx(Elf *_elf, size_t *_dst); 202210345Skaiwint elf_getshstrndx(Elf *_elf, size_t *_dst); /* Deprecated */ 203164190Sjkoshyunsigned long elf_hash(const char *_name); 204164190SjkoshyElf_Kind elf_kind(Elf *_elf); 205164190SjkoshyElf *elf_memory(char *_image, size_t _size); 206164190Sjkoshysize_t elf_ndxscn(Elf_Scn *_scn); 207164190SjkoshyElf_Data *elf_newdata(Elf_Scn *_scn); 208164190SjkoshyElf_Scn *elf_newscn(Elf *_elf); 209164190SjkoshyElf_Scn *elf_nextscn(Elf *_elf, Elf_Scn *_scn); 210164190SjkoshyElf_Cmd elf_next(Elf *_elf); 211164190Sjkoshyoff_t elf_rand(Elf *_elf, off_t _off); 212164190SjkoshyElf_Data *elf_rawdata(Elf_Scn *_scn, Elf_Data *_data); 213164190Sjkoshychar *elf_rawfile(Elf *_elf, size_t *_size); 214164190Sjkoshyint elf_setshstrndx(Elf *_elf, size_t _shnum); 215164190Sjkoshychar *elf_strptr(Elf *_elf, size_t _section, size_t _offset); 216164190Sjkoshyoff_t elf_update(Elf *_elf, Elf_Cmd _cmd); 217164190Sjkoshyunsigned int elf_version(unsigned int _version); 218164190Sjkoshy 219164190Sjkoshylong elf32_checksum(Elf *_elf); 220164190Sjkoshysize_t elf32_fsize(Elf_Type _type, size_t _count, 221164190Sjkoshy unsigned int _version); 222164190SjkoshyElf32_Ehdr *elf32_getehdr(Elf *_elf); 223164190SjkoshyElf32_Phdr *elf32_getphdr(Elf *_elf); 224164190SjkoshyElf32_Shdr *elf32_getshdr(Elf_Scn *_scn); 225164190SjkoshyElf32_Ehdr *elf32_newehdr(Elf *_elf); 226164190SjkoshyElf32_Phdr *elf32_newphdr(Elf *_elf, size_t _count); 227164190SjkoshyElf_Data *elf32_xlatetof(Elf_Data *_dst, const Elf_Data *_src, 228164190Sjkoshy unsigned int _enc); 229164190SjkoshyElf_Data *elf32_xlatetom(Elf_Data *_dst, const Elf_Data *_src, 230164190Sjkoshy unsigned int _enc); 231164190Sjkoshy 232164190Sjkoshylong elf64_checksum(Elf *_elf); 233164190Sjkoshysize_t elf64_fsize(Elf_Type _type, size_t _count, 234164190Sjkoshy unsigned int _version); 235164190SjkoshyElf64_Ehdr *elf64_getehdr(Elf *_elf); 236164190SjkoshyElf64_Phdr *elf64_getphdr(Elf *_elf); 237164190SjkoshyElf64_Shdr *elf64_getshdr(Elf_Scn *_scn); 238164190SjkoshyElf64_Ehdr *elf64_newehdr(Elf *_elf); 239164190SjkoshyElf64_Phdr *elf64_newphdr(Elf *_elf, size_t _count); 240164190SjkoshyElf_Data *elf64_xlatetof(Elf_Data *_dst, const Elf_Data *_src, 241164190Sjkoshy unsigned int _enc); 242164190SjkoshyElf_Data *elf64_xlatetom(Elf_Data *_dst, const Elf_Data *_src, 243164190Sjkoshy unsigned int _enc); 244164190Sjkoshy 245164190Sjkoshy#if defined(LIBELF_TEST_HOOKS) 246164190Sjkoshyint _libelf_get_elf_class(Elf *_elf); 247164190Sjkoshyint _libelf_get_max_error(void); 248164190Sjkoshyconst char *_libelf_get_no_error_message(void); 249164190Sjkoshyconst char *_libelf_get_unknown_error_message(void); 250164190Sjkoshyvoid _libelf_set_elf_class(Elf *_elf, int _class); 251164190Sjkoshyvoid _libelf_set_error(int _error); 252164190Sjkoshy#endif /* LIBELF_TEST_HOOKS */ 253164190Sjkoshy__END_DECLS 254164190Sjkoshy 255164190Sjkoshy#endif /* _LIBELF_H_ */ 256