Blob
1 /*2 * Copyright (c) 2004 Russ Cox. See LICENSE.3 */5 /* /home/rsc/papers/elfXXelf.pdf */7 typedef struct Elf Elf;8 typedef struct ElfHdr ElfHdr;9 typedef struct ElfSect ElfSect;10 typedef struct ElfProg ElfProg;11 typedef struct ElfNote ElfNote;12 typedef struct ElfSym ElfSym;14 enum15 {16 ElfClassNone = 0,17 ElfClass32,18 ElfClass64,20 ElfDataNone = 0,21 ElfDataLsb,22 ElfDataMsb,24 ElfTypeNone = 0,25 ElfTypeRelocatable,26 ElfTypeExecutable,27 ElfTypeSharedObject,28 ElfTypeCore,29 /* 0xFF00 - 0xFFFF reserved for processor-specific types */31 ElfMachNone = 0,32 ElfMach32100, /* AT&T WE 32100 */33 ElfMachSparc, /* SPARC */34 ElfMach386, /* Intel 80386 */35 ElfMach68000, /* Motorola 68000 */36 ElfMach88000, /* Motorola 88000 */37 ElfMach486, /* Intel 80486, no longer used */38 ElfMach860, /* Intel 80860 */39 ElfMachMips, /* MIPS RS3000 */40 ElfMachS370, /* IBM System/370 */41 ElfMachMipsLe, /* MIPS RS3000 LE */42 ElfMachParisc = 15, /* HP PA RISC */43 ElfMachVpp500 = 17, /* Fujitsu VPP500 */44 ElfMachSparc32Plus, /* SPARC V8+ */45 ElfMach960, /* Intel 80960 */46 ElfMachPower, /* PowerPC */47 ElfMachPower64, /* PowerPC 64 */48 ElfMachS390, /* IBM System/390 */49 ElfMachV800 = 36, /* NEC V800 */50 ElfMachFr20, /* Fujitsu FR20 */51 ElfMachRh32, /* TRW RH-32 */52 ElfMachRce, /* Motorola RCE */53 ElfMachArm, /* ARM */54 ElfMachAlpha, /* Digital Alpha */55 ElfMachSH, /* Hitachi SH */56 ElfMachSparc9, /* SPARC V9 */57 /* and the list goes on... */59 ElfAbiNone = 0,60 ElfAbiSystemV = 0, /* [sic] */61 ElfAbiHPUX,62 ElfAbiNetBSD,63 ElfAbiLinux,64 ElfAbiSolaris = 6,65 ElfAbiAix,66 ElfAbiIrix,67 ElfAbiFreeBSD,68 ElfAbiTru64,69 ElfAbiModesto,70 ElfAbiOpenBSD,71 ElfAbiARM = 97,72 ElfAbiEmbedded = 255,74 /* some of sections 0xFF00 - 0xFFFF reserved for various things */75 ElfSectNone = 0,76 ElfSectProgbits,77 ElfSectSymtab,78 ElfSectStrtab,79 ElfSectRela,80 ElfSectHash,81 ElfSectDynamic,82 ElfSectNote,83 ElfSectNobits,84 ElfSectRel,85 ElfSectShlib,86 ElfSectDynsym,88 ElfSectFlagWrite = 0x1,89 ElfSectFlagAlloc = 0x2,90 ElfSectFlagExec = 0x4,91 /* 0xF0000000 are reserved for processor specific */93 ElfSymBindLocal = 0,94 ElfSymBindGlobal,95 ElfSymBindWeak,96 /* 13-15 reserved */98 ElfSymTypeNone = 0,99 ElfSymTypeObject,100 ElfSymTypeFunc,101 ElfSymTypeSection,102 ElfSymTypeFile,103 /* 13-15 reserved */105 ElfSymShnNone = 0,106 ElfSymShnAbs = 0xFFF1,107 ElfSymShnCommon = 0xFFF2,108 /* 0xFF00-0xFF1F reserved for processors */109 /* 0xFF20-0xFF3F reserved for operating systems */111 ElfProgNone = 0,112 ElfProgLoad,113 ElfProgDynamic,114 ElfProgInterp,115 ElfProgNote,116 ElfProgShlib,117 ElfProgPhdr,119 ElfProgFlagExec = 0x1,120 ElfProgFlagWrite = 0x2,121 ElfProgFlagRead = 0x4,123 ElfNotePrStatus = 1,124 ElfNotePrFpreg = 2,125 ElfNotePrPsinfo = 3,126 ElfNotePrTaskstruct = 4,127 ElfNotePrAuxv = 6,128 ElfNotePrXfpreg = 0x46e62b7f, /* for gdb/386 */129 };131 struct ElfHdr132 {133 uchar magic[4];134 uchar class;135 uchar encoding;136 uchar version;137 uchar abi;138 uchar abiversion;139 u32int type;140 u32int machine;141 u32int entry;142 u32int phoff;143 u32int shoff;144 u32int flags;145 u32int ehsize;146 u32int phentsize;147 u32int phnum;148 u32int shentsize;149 u32int shnum;150 u32int shstrndx;151 u16int (*e2)(uchar*);152 u32int (*e4)(uchar*);153 u64int (*e8)(uchar*);154 };156 struct ElfSect157 {158 char *name;159 u32int type;160 u32int flags;161 u32int addr;162 u32int offset;163 u32int size;164 u32int link;165 u32int info;166 u32int align;167 u32int entsize;168 uchar *base;169 };171 struct ElfProg172 {173 u32int type;174 u32int offset;175 u32int vaddr;176 u32int paddr;177 u32int filesz;178 u32int memsz;179 u32int flags;180 u32int align;181 };183 struct ElfNote184 {185 u32int namesz;186 u32int descsz;187 u32int type;188 char *name;189 uchar *desc;190 u32int offset; /* in-memory only */191 };193 struct ElfSym194 {195 char* name;196 u32int value;197 u32int size;198 uchar bind;199 uchar type;200 uchar other;201 u16int shndx;202 };204 struct Elf205 {206 int fd;207 ElfHdr hdr;208 ElfSect *sect;209 uint nsect;210 ElfProg *prog;211 uint nprog;212 char *shstrtab;214 int nsymtab;215 ElfSect *symtab;216 ElfSect *symstr;217 int ndynsym;218 ElfSect *dynsym;219 ElfSect *dynstr;220 ElfSect *bss;221 ulong dynamic; /* offset to elf dynamic crap */223 int (*coreregs)(Elf*, ElfNote*, uchar**);224 };226 Elf* elfopen(char*);227 Elf* elfinit(int);228 ElfSect *elfsection(Elf*, char*);229 void elfclose(Elf*);230 int elfsym(Elf*, int, ElfSym*);231 int elfsymlookup(Elf*, char*, ulong*);232 int elfmap(Elf*, ElfSect*);234 int coreregslinux386(Elf*, ElfNote*, uchar**);235 int coreregsfreebsd386(Elf*, ElfNote*, uchar**);236 int corecmdlinux386(Elf*, ElfNote*, char**);237 int corecmdfreebsd386(Elf*, ElfNote*, char**);238 void elfdl386mapdl(void);