1*0a6a1f1dSLionel Sambuc /* $NetBSD: libelf.h,v 1.4 2015/09/29 22:14:14 christos Exp $ */ 2*0a6a1f1dSLionel Sambuc 3*0a6a1f1dSLionel Sambuc /*- 4*0a6a1f1dSLionel Sambuc * Copyright (c) 2006,2008-2010 Joseph Koshy 5*0a6a1f1dSLionel Sambuc * All rights reserved. 6*0a6a1f1dSLionel Sambuc * 7*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without 8*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions 9*0a6a1f1dSLionel Sambuc * are met: 10*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright 11*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer. 12*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 13*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the 14*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution. 15*0a6a1f1dSLionel Sambuc * 16*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*0a6a1f1dSLionel Sambuc * SUCH DAMAGE. 27*0a6a1f1dSLionel Sambuc * 28*0a6a1f1dSLionel Sambuc * Id: libelf.h 2366 2011-12-29 06:12:14Z jkoshy 29*0a6a1f1dSLionel Sambuc */ 30*0a6a1f1dSLionel Sambuc 31*0a6a1f1dSLionel Sambuc #ifndef _LIBELF_H_ 32*0a6a1f1dSLionel Sambuc #define _LIBELF_H_ 33*0a6a1f1dSLionel Sambuc 34*0a6a1f1dSLionel Sambuc #if HAVE_NBTOOL_CONFIG_H 35*0a6a1f1dSLionel Sambuc # include "nbtool_config.h" 36*0a6a1f1dSLionel Sambuc #endif 37*0a6a1f1dSLionel Sambuc 38*0a6a1f1dSLionel Sambuc 39*0a6a1f1dSLionel Sambuc #if HAVE_NBTOOL_CONFIG_H 40*0a6a1f1dSLionel Sambuc # include <nbinclude/sys/exec_elf.h> 41*0a6a1f1dSLionel Sambuc #elif defined(__NetBSD__) || defined(__minix) 42*0a6a1f1dSLionel Sambuc # include <sys/types.h> 43*0a6a1f1dSLionel Sambuc # include <sys/exec_elf.h> 44*0a6a1f1dSLionel Sambuc #elif defined(__FreeBSD__) 45*0a6a1f1dSLionel Sambuc # include <sys/types.h> 46*0a6a1f1dSLionel Sambuc # include <sys/elf32.h> 47*0a6a1f1dSLionel Sambuc # include <sys/elf64.h> 48*0a6a1f1dSLionel Sambuc #else 49*0a6a1f1dSLionel Sambuc # error "Unsupported platform" 50*0a6a1f1dSLionel Sambuc #endif 51*0a6a1f1dSLionel Sambuc 52*0a6a1f1dSLionel Sambuc /* Library private data structures */ 53*0a6a1f1dSLionel Sambuc typedef struct _Elf Elf; 54*0a6a1f1dSLionel Sambuc typedef struct _Elf_Scn Elf_Scn; 55*0a6a1f1dSLionel Sambuc 56*0a6a1f1dSLionel Sambuc /* File types */ 57*0a6a1f1dSLionel Sambuc typedef enum { 58*0a6a1f1dSLionel Sambuc ELF_K_NONE = 0, 59*0a6a1f1dSLionel Sambuc ELF_K_AR, /* `ar' archives */ 60*0a6a1f1dSLionel Sambuc ELF_K_COFF, /* COFF files (unsupported) */ 61*0a6a1f1dSLionel Sambuc ELF_K_ELF, /* ELF files */ 62*0a6a1f1dSLionel Sambuc ELF_K_NUM 63*0a6a1f1dSLionel Sambuc } Elf_Kind; 64*0a6a1f1dSLionel Sambuc 65*0a6a1f1dSLionel Sambuc #define ELF_K_FIRST ELF_K_NONE 66*0a6a1f1dSLionel Sambuc #define ELF_K_LAST ELF_K_NUM 67*0a6a1f1dSLionel Sambuc 68*0a6a1f1dSLionel Sambuc /* Data types */ 69*0a6a1f1dSLionel Sambuc typedef enum { 70*0a6a1f1dSLionel Sambuc ELF_T_ADDR, 71*0a6a1f1dSLionel Sambuc ELF_T_BYTE, 72*0a6a1f1dSLionel Sambuc ELF_T_CAP, 73*0a6a1f1dSLionel Sambuc ELF_T_DYN, 74*0a6a1f1dSLionel Sambuc ELF_T_EHDR, 75*0a6a1f1dSLionel Sambuc ELF_T_HALF, 76*0a6a1f1dSLionel Sambuc ELF_T_LWORD, 77*0a6a1f1dSLionel Sambuc ELF_T_MOVE, 78*0a6a1f1dSLionel Sambuc ELF_T_MOVEP, 79*0a6a1f1dSLionel Sambuc ELF_T_NOTE, 80*0a6a1f1dSLionel Sambuc ELF_T_OFF, 81*0a6a1f1dSLionel Sambuc ELF_T_PHDR, 82*0a6a1f1dSLionel Sambuc ELF_T_REL, 83*0a6a1f1dSLionel Sambuc ELF_T_RELA, 84*0a6a1f1dSLionel Sambuc ELF_T_SHDR, 85*0a6a1f1dSLionel Sambuc ELF_T_SWORD, 86*0a6a1f1dSLionel Sambuc ELF_T_SXWORD, 87*0a6a1f1dSLionel Sambuc ELF_T_SYMINFO, 88*0a6a1f1dSLionel Sambuc ELF_T_SYM, 89*0a6a1f1dSLionel Sambuc ELF_T_VDEF, 90*0a6a1f1dSLionel Sambuc ELF_T_VNEED, 91*0a6a1f1dSLionel Sambuc ELF_T_WORD, 92*0a6a1f1dSLionel Sambuc ELF_T_XWORD, 93*0a6a1f1dSLionel Sambuc ELF_T_GNUHASH, /* GNU style hash tables. */ 94*0a6a1f1dSLionel Sambuc ELF_T_NUM 95*0a6a1f1dSLionel Sambuc } Elf_Type; 96*0a6a1f1dSLionel Sambuc 97*0a6a1f1dSLionel Sambuc #define ELF_T_FIRST ELF_T_ADDR 98*0a6a1f1dSLionel Sambuc #define ELF_T_LAST ELF_T_GNUHASH 99*0a6a1f1dSLionel Sambuc 100*0a6a1f1dSLionel Sambuc /* Commands */ 101*0a6a1f1dSLionel Sambuc typedef enum { 102*0a6a1f1dSLionel Sambuc ELF_C_NULL = 0, 103*0a6a1f1dSLionel Sambuc ELF_C_CLR, 104*0a6a1f1dSLionel Sambuc ELF_C_FDDONE, 105*0a6a1f1dSLionel Sambuc ELF_C_FDREAD, 106*0a6a1f1dSLionel Sambuc ELF_C_RDWR, 107*0a6a1f1dSLionel Sambuc ELF_C_READ, 108*0a6a1f1dSLionel Sambuc ELF_C_SET, 109*0a6a1f1dSLionel Sambuc ELF_C_WRITE, 110*0a6a1f1dSLionel Sambuc ELF_C_NUM 111*0a6a1f1dSLionel Sambuc } Elf_Cmd; 112*0a6a1f1dSLionel Sambuc 113*0a6a1f1dSLionel Sambuc #define ELF_C_FIRST ELF_C_NULL 114*0a6a1f1dSLionel Sambuc #define ELF_C_LAST ELF_C_NUM 115*0a6a1f1dSLionel Sambuc 116*0a6a1f1dSLionel Sambuc /* 117*0a6a1f1dSLionel Sambuc * An `Elf_Data' structure describes data in an 118*0a6a1f1dSLionel Sambuc * ELF section. 119*0a6a1f1dSLionel Sambuc */ 120*0a6a1f1dSLionel Sambuc typedef struct _Elf_Data { 121*0a6a1f1dSLionel Sambuc /* 122*0a6a1f1dSLionel Sambuc * `Public' members that are part of the ELF(3) API. 123*0a6a1f1dSLionel Sambuc */ 124*0a6a1f1dSLionel Sambuc uint64_t d_align; 125*0a6a1f1dSLionel Sambuc void *d_buf; 126*0a6a1f1dSLionel Sambuc uint64_t d_off; 127*0a6a1f1dSLionel Sambuc uint64_t d_size; 128*0a6a1f1dSLionel Sambuc Elf_Type d_type; 129*0a6a1f1dSLionel Sambuc unsigned int d_version; 130*0a6a1f1dSLionel Sambuc } Elf_Data; 131*0a6a1f1dSLionel Sambuc 132*0a6a1f1dSLionel Sambuc /* 133*0a6a1f1dSLionel Sambuc * An `Elf_Arhdr' structure describes an archive 134*0a6a1f1dSLionel Sambuc * header. 135*0a6a1f1dSLionel Sambuc */ 136*0a6a1f1dSLionel Sambuc typedef struct { 137*0a6a1f1dSLionel Sambuc time_t ar_date; 138*0a6a1f1dSLionel Sambuc char *ar_name; /* archive member name */ 139*0a6a1f1dSLionel Sambuc gid_t ar_gid; 140*0a6a1f1dSLionel Sambuc mode_t ar_mode; 141*0a6a1f1dSLionel Sambuc char *ar_rawname; /* 'raw' member name */ 142*0a6a1f1dSLionel Sambuc size_t ar_size; 143*0a6a1f1dSLionel Sambuc uid_t ar_uid; 144*0a6a1f1dSLionel Sambuc 145*0a6a1f1dSLionel Sambuc /* 146*0a6a1f1dSLionel Sambuc * Members that are not part of the public API. 147*0a6a1f1dSLionel Sambuc */ 148*0a6a1f1dSLionel Sambuc int ar_flags; 149*0a6a1f1dSLionel Sambuc } Elf_Arhdr; 150*0a6a1f1dSLionel Sambuc 151*0a6a1f1dSLionel Sambuc /* 152*0a6a1f1dSLionel Sambuc * An `Elf_Arsym' describes an entry in the archive 153*0a6a1f1dSLionel Sambuc * symbol table. 154*0a6a1f1dSLionel Sambuc */ 155*0a6a1f1dSLionel Sambuc typedef struct { 156*0a6a1f1dSLionel Sambuc off_t as_off; /* byte offset to member's header */ 157*0a6a1f1dSLionel Sambuc unsigned long as_hash; /* elf_hash() value for name */ 158*0a6a1f1dSLionel Sambuc char *as_name; /* null terminated symbol name */ 159*0a6a1f1dSLionel Sambuc } Elf_Arsym; 160*0a6a1f1dSLionel Sambuc 161*0a6a1f1dSLionel Sambuc /* 162*0a6a1f1dSLionel Sambuc * Error numbers. 163*0a6a1f1dSLionel Sambuc */ 164*0a6a1f1dSLionel Sambuc 165*0a6a1f1dSLionel Sambuc enum Elf_Error { 166*0a6a1f1dSLionel Sambuc ELF_E_NONE, /* No error */ 167*0a6a1f1dSLionel Sambuc ELF_E_ARCHIVE, /* Malformed ar(1) archive */ 168*0a6a1f1dSLionel Sambuc ELF_E_ARGUMENT, /* Invalid argument */ 169*0a6a1f1dSLionel Sambuc ELF_E_CLASS, /* Mismatched ELF class */ 170*0a6a1f1dSLionel Sambuc ELF_E_DATA, /* Invalid data descriptor */ 171*0a6a1f1dSLionel Sambuc ELF_E_HEADER, /* Missing or malformed ELF header */ 172*0a6a1f1dSLionel Sambuc ELF_E_IO, /* I/O error */ 173*0a6a1f1dSLionel Sambuc ELF_E_LAYOUT, /* Layout constraint violation */ 174*0a6a1f1dSLionel Sambuc ELF_E_MODE, /* Wrong mode for ELF descriptor */ 175*0a6a1f1dSLionel Sambuc ELF_E_RANGE, /* Value out of range */ 176*0a6a1f1dSLionel Sambuc ELF_E_RESOURCE, /* Resource exhaustion */ 177*0a6a1f1dSLionel Sambuc ELF_E_SECTION, /* Invalid section descriptor */ 178*0a6a1f1dSLionel Sambuc ELF_E_SEQUENCE, /* API calls out of sequence */ 179*0a6a1f1dSLionel Sambuc ELF_E_UNIMPL, /* Feature is unimplemented */ 180*0a6a1f1dSLionel Sambuc ELF_E_VERSION, /* Unknown API version */ 181*0a6a1f1dSLionel Sambuc ELF_E_NUM /* Max error number */ 182*0a6a1f1dSLionel Sambuc }; 183*0a6a1f1dSLionel Sambuc 184*0a6a1f1dSLionel Sambuc /* 185*0a6a1f1dSLionel Sambuc * Flags defined by the API. 186*0a6a1f1dSLionel Sambuc */ 187*0a6a1f1dSLionel Sambuc 188*0a6a1f1dSLionel Sambuc #define ELF_F_LAYOUT 0x001U /* application will layout the file */ 189*0a6a1f1dSLionel Sambuc #define ELF_F_DIRTY 0x002U /* a section or ELF file is dirty */ 190*0a6a1f1dSLionel Sambuc 191*0a6a1f1dSLionel Sambuc /* ELF(3) API extensions. */ 192*0a6a1f1dSLionel Sambuc #define ELF_F_ARCHIVE 0x100U /* archive creation */ 193*0a6a1f1dSLionel Sambuc #define ELF_F_ARCHIVE_SYSV 0x200U /* SYSV style archive */ 194*0a6a1f1dSLionel Sambuc 195*0a6a1f1dSLionel Sambuc __BEGIN_DECLS 196*0a6a1f1dSLionel Sambuc Elf *elf_begin(int _fd, Elf_Cmd _cmd, Elf *_elf); 197*0a6a1f1dSLionel Sambuc int elf_cntl(Elf *_elf, Elf_Cmd _cmd); 198*0a6a1f1dSLionel Sambuc int elf_end(Elf *_elf); 199*0a6a1f1dSLionel Sambuc const char *elf_errmsg(int _error); 200*0a6a1f1dSLionel Sambuc int elf_errno(void); 201*0a6a1f1dSLionel Sambuc void elf_fill(int _fill); 202*0a6a1f1dSLionel Sambuc unsigned int elf_flagarhdr(Elf_Arhdr *_arh, Elf_Cmd _cmd, 203*0a6a1f1dSLionel Sambuc unsigned int _flags); 204*0a6a1f1dSLionel Sambuc unsigned int elf_flagdata(Elf_Data *_data, Elf_Cmd _cmd, 205*0a6a1f1dSLionel Sambuc unsigned int _flags); 206*0a6a1f1dSLionel Sambuc unsigned int elf_flagehdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags); 207*0a6a1f1dSLionel Sambuc unsigned int elf_flagelf(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags); 208*0a6a1f1dSLionel Sambuc unsigned int elf_flagphdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags); 209*0a6a1f1dSLionel Sambuc unsigned int elf_flagscn(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags); 210*0a6a1f1dSLionel Sambuc unsigned int elf_flagshdr(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags); 211*0a6a1f1dSLionel Sambuc Elf_Arhdr *elf_getarhdr(Elf *_elf); 212*0a6a1f1dSLionel Sambuc Elf_Arsym *elf_getarsym(Elf *_elf, size_t *_ptr); 213*0a6a1f1dSLionel Sambuc off_t elf_getbase(Elf *_elf); 214*0a6a1f1dSLionel Sambuc Elf_Data *elf_getdata(Elf_Scn *, Elf_Data *); 215*0a6a1f1dSLionel Sambuc char *elf_getident(Elf *_elf, size_t *_ptr); 216*0a6a1f1dSLionel Sambuc int elf_getphdrnum(Elf *_elf, size_t *_dst); 217*0a6a1f1dSLionel Sambuc int elf_getphnum(Elf *_elf, size_t *_dst); /* Deprecated */ 218*0a6a1f1dSLionel Sambuc Elf_Scn *elf_getscn(Elf *_elf, size_t _index); 219*0a6a1f1dSLionel Sambuc int elf_getshdrnum(Elf *_elf, size_t *_dst); 220*0a6a1f1dSLionel Sambuc int elf_getshnum(Elf *_elf, size_t *_dst); /* Deprecated */ 221*0a6a1f1dSLionel Sambuc int elf_getshdrstrndx(Elf *_elf, size_t *_dst); 222*0a6a1f1dSLionel Sambuc int elf_getshstrndx(Elf *_elf, size_t *_dst); /* Deprecated */ 223*0a6a1f1dSLionel Sambuc unsigned long elf_hash(const void *_name); 224*0a6a1f1dSLionel Sambuc Elf_Kind elf_kind(Elf *_elf); 225*0a6a1f1dSLionel Sambuc Elf *elf_memory(char *_image, size_t _size); 226*0a6a1f1dSLionel Sambuc size_t elf_ndxscn(Elf_Scn *_scn); 227*0a6a1f1dSLionel Sambuc Elf_Data *elf_newdata(Elf_Scn *_scn); 228*0a6a1f1dSLionel Sambuc Elf_Scn *elf_newscn(Elf *_elf); 229*0a6a1f1dSLionel Sambuc Elf_Scn *elf_nextscn(Elf *_elf, Elf_Scn *_scn); 230*0a6a1f1dSLionel Sambuc Elf_Cmd elf_next(Elf *_elf); 231*0a6a1f1dSLionel Sambuc Elf *elf_open(int _fd); 232*0a6a1f1dSLionel Sambuc Elf *elf_openmemory(char *_image, size_t _size); 233*0a6a1f1dSLionel Sambuc off_t elf_rand(Elf *_elf, off_t _off); 234*0a6a1f1dSLionel Sambuc Elf_Data *elf_rawdata(Elf_Scn *_scn, Elf_Data *_data); 235*0a6a1f1dSLionel Sambuc char *elf_rawfile(Elf *_elf, size_t *_size); 236*0a6a1f1dSLionel Sambuc int elf_setshstrndx(Elf *_elf, size_t _shnum); 237*0a6a1f1dSLionel Sambuc char *elf_strptr(Elf *_elf, size_t _section, size_t _offset); 238*0a6a1f1dSLionel Sambuc off_t elf_update(Elf *_elf, Elf_Cmd _cmd); 239*0a6a1f1dSLionel Sambuc unsigned int elf_version(unsigned int _version); 240*0a6a1f1dSLionel Sambuc 241*0a6a1f1dSLionel Sambuc long elf32_checksum(Elf *_elf); 242*0a6a1f1dSLionel Sambuc size_t elf32_fsize(Elf_Type _type, size_t _count, 243*0a6a1f1dSLionel Sambuc unsigned int _version); 244*0a6a1f1dSLionel Sambuc Elf32_Ehdr *elf32_getehdr(Elf *_elf); 245*0a6a1f1dSLionel Sambuc Elf32_Phdr *elf32_getphdr(Elf *_elf); 246*0a6a1f1dSLionel Sambuc Elf32_Shdr *elf32_getshdr(Elf_Scn *_scn); 247*0a6a1f1dSLionel Sambuc Elf32_Ehdr *elf32_newehdr(Elf *_elf); 248*0a6a1f1dSLionel Sambuc Elf32_Phdr *elf32_newphdr(Elf *_elf, size_t _count); 249*0a6a1f1dSLionel Sambuc Elf_Data *elf32_xlatetof(Elf_Data *_dst, const Elf_Data *_src, 250*0a6a1f1dSLionel Sambuc unsigned int _enc); 251*0a6a1f1dSLionel Sambuc Elf_Data *elf32_xlatetom(Elf_Data *_dst, const Elf_Data *_src, 252*0a6a1f1dSLionel Sambuc unsigned int _enc); 253*0a6a1f1dSLionel Sambuc 254*0a6a1f1dSLionel Sambuc long elf64_checksum(Elf *_elf); 255*0a6a1f1dSLionel Sambuc size_t elf64_fsize(Elf_Type _type, size_t _count, 256*0a6a1f1dSLionel Sambuc unsigned int _version); 257*0a6a1f1dSLionel Sambuc Elf64_Ehdr *elf64_getehdr(Elf *_elf); 258*0a6a1f1dSLionel Sambuc Elf64_Phdr *elf64_getphdr(Elf *_elf); 259*0a6a1f1dSLionel Sambuc Elf64_Shdr *elf64_getshdr(Elf_Scn *_scn); 260*0a6a1f1dSLionel Sambuc Elf64_Ehdr *elf64_newehdr(Elf *_elf); 261*0a6a1f1dSLionel Sambuc Elf64_Phdr *elf64_newphdr(Elf *_elf, size_t _count); 262*0a6a1f1dSLionel Sambuc Elf_Data *elf64_xlatetof(Elf_Data *_dst, const Elf_Data *_src, 263*0a6a1f1dSLionel Sambuc unsigned int _enc); 264*0a6a1f1dSLionel Sambuc Elf_Data *elf64_xlatetom(Elf_Data *_dst, const Elf_Data *_src, 265*0a6a1f1dSLionel Sambuc unsigned int _enc); 266*0a6a1f1dSLionel Sambuc __END_DECLS 267*0a6a1f1dSLionel Sambuc 268*0a6a1f1dSLionel Sambuc #endif /* _LIBELF_H_ */ 269