Main Page   Modules   Data Structures   File List   Data Fields   Examples  

melf.h

00001 /*
00002  * ELF manipulation library
00003  *
00004  * skape
00005  * [email protected]
00006  */
00021 #ifndef _MELF_H
00022 #define _MELF_H
00023 
00024 #ifdef MELF_COMP
00025         #include "stdelf.h"
00026 #else
00027         #include <stdelf.h>
00028 #endif
00029 
00030 #define IS_OVERFLOW(melf, base, addr) ((melf)->imageSize < ((unsigned long)(addr) - (unsigned long)(base)))
00031 
00035 #define MELF_ACCESS_READ            (1 << 0)
00036 
00039 #define MELF_ACCESS_WRITE           (1 << 1)
00040 
00043 #define MELF_ACCESS_ALL             (MELF_ACCESS_READ | MELF_ACCESS_WRITE)
00044 
00048 #define MELF_HAS_ACCESS(ctx, flag)  ((ctx)->access & (flag))
00049 
00053 enum ElfHeaderType
00054 {
00055         ELF_HEADER_TYPE_SECTION = 0,
00056         ELF_HEADER_TYPE_PROGRAM = 1,
00057 };
00058 
00064 typedef struct _elf_spec_header {
00065 
00069         unsigned long           index;
00073         unsigned long           identifier;
00077         enum ElfHeaderType      type;
00078 
00082         union
00083         {
00084 
00088                 Elf32_Shdr section;
00092                 Elf32_Phdr program;
00093 
00094         } spec;
00095 
00099         void                    *content;
00103         unsigned long           contentLength;
00104 
00108         void                    *contentListHead;
00112         void                    *contentListTail;
00116         unsigned long           contentListSize;
00117 
00121         struct _elf_spec_header *prev;
00125         struct _elf_spec_header *next;
00126 
00127 } ELF_SPEC_HEADER;
00128 
00134 typedef struct _elf_spec_header_list {
00135 
00139         ELF_SPEC_HEADER *head;
00143         ELF_SPEC_HEADER *tail;
00144 
00148         unsigned long   seq;
00152         unsigned long   length;
00153 
00154 } ELF_SPEC_HEADER_LIST;
00155 
00156 typedef struct _elf_section_entry {
00157 
00158         void *next;
00159         void *prev;
00160 
00161 } ELF_SECTION_ENTRY;
00162 
00168 typedef struct _melf {
00169 
00173         char                 image[1024];
00177         unsigned long        imageSize;
00178 
00182         int                  access;
00183 
00187         Elf32_Ehdr           header;
00188 
00192         ELF_SPEC_HEADER_LIST sections;
00193 
00197         ELF_SPEC_HEADER_LIST programs;
00198 
00199 } MELF;
00200 
00206 MELF *melf_new();
00223 MELF *melf_open(const char *image, int access);
00229 void melf_destroy(MELF *melf);
00230 
00240 unsigned long melf_synchronize(MELF *melf);
00248 unsigned long melf_save(MELF *melf, const char *path);
00249 
00255 unsigned long melf_version();
00256 
00261 // Elf header functions
00262 
00276 void melf_elfSetRaw(MELF *melf, Elf32_Ehdr *header);
00283 Elf32_Ehdr *melf_elfGetRaw(MELF *melf);
00284 
00304 void melf_elfSetType(MELF *melf, Elf32_Half type);
00311 Elf32_Half melf_elfGetType(MELF *melf);
00312 
00319 void melf_elfSetMachine(MELF *melf, Elf32_Half machine);
00326 Elf32_Half melf_elfGetMachine(MELF *melf);
00327 
00334 void melf_elfSetVersion(MELF *melf, Elf32_Word version);
00341 Elf32_Word melf_elfGetVersion(MELF *melf);
00342 
00349 void melf_elfSetEntry(MELF *melf, Elf32_Addr entry);
00356 Elf32_Addr melf_elfGetEntry(MELF *melf);
00357 
00364 void melf_elfSetProgramHeaderOffset(MELF *melf, Elf32_Off offset);
00371 Elf32_Off melf_elfGetProgramHeaderOffset(MELF *melf);
00372 
00379 void melf_elfSetSectionHeaderOffset(MELF *melf, Elf32_Off offset);
00386 Elf32_Off melf_elfGetSectionHeaderOffset(MELF *melf);
00387 
00394 void melf_elfSetProgramHeaderCount(MELF *melf, Elf32_Half count);
00401 Elf32_Half melf_elfGetProgramHeaderCount(MELF *melf);
00402 
00409 void melf_elfSetSectionHeaderEntrySize(MELF *melf, Elf32_Half size);
00416 Elf32_Half melf_elfGetSectionEntrySize(MELF *melf);
00423 void melf_elfSetProgramHeaderEntrySize(MELF *melf, Elf32_Half size);
00430 Elf32_Half melf_elfGetProgramEntrySize(MELF *melf);
00431 
00438 void melf_elfSetSectionHeaderCount(MELF *melf, Elf32_Half count);
00445 Elf32_Half melf_elfGetSectionHeaderCount(MELF *melf);
00446 
00453 void melf_elfSetStringTableIndex(MELF *melf, Elf32_Half index);
00460 Elf32_Half melf_elfGetStringTableIndex(MELF *melf);
00461 
00466 // Section functions
00467 
00481 ELF_SPEC_HEADER *melf_sectionAdd(MELF *melf);
00488 ELF_SPEC_HEADER *melf_sectionGetEnum(MELF *melf);
00496 ELF_SPEC_HEADER *melf_sectionEnumNext(MELF *melf, ELF_SPEC_HEADER *en);
00504 ELF_SPEC_HEADER *melf_sectionEnumPrev(MELF *melf, ELF_SPEC_HEADER *en);
00505 
00513 ELF_SPEC_HEADER *melf_sectionFindType(MELF *melf, unsigned long type);
00521 ELF_SPEC_HEADER *melf_sectionFindIndex(MELF *melf, unsigned long index);
00529 ELF_SPEC_HEADER *melf_sectionFindName(MELF *melf, const char *name);
00530 
00539 void melf_sectionSetContent(MELF *melf, ELF_SPEC_HEADER *section, void *content, unsigned long contentLength);
00547 void *melf_sectionGetContent(MELF *melf, ELF_SPEC_HEADER *section);
00548 
00559 unsigned long melf_sectionInsertContent(MELF *melf, ELF_SPEC_HEADER *section, unsigned long offset, void *content, unsigned long contentLength);
00560 
00569 void melf_sectionSetName(MELF *melf, ELF_SPEC_HEADER *section, const char *name);
00577 const char *melf_sectionGetName(MELF *melf, ELF_SPEC_HEADER *section);
00578 
00613 void melf_sectionSetType(MELF *melf, ELF_SPEC_HEADER *section, unsigned long type);
00621 unsigned long melf_sectionGetType(MELF *melf, ELF_SPEC_HEADER *section);
00622 
00630 void melf_sectionSetOffset(MELF *melf, ELF_SPEC_HEADER *section, Elf32_Off offset);
00638 Elf32_Off melf_sectionGetOffset(MELF *melf, ELF_SPEC_HEADER *section);
00639 
00649 void melf_sectionSetSize(MELF *melf, ELF_SPEC_HEADER *section, Elf32_Half size);
00657 Elf32_Half melf_sectionGetSize(MELF *melf, ELF_SPEC_HEADER *section);
00658 
00666 void melf_sectionSetAddress(MELF *melf, ELF_SPEC_HEADER *section, Elf32_Addr addr);
00674 Elf32_Addr melf_sectionGetAddress(MELF *melf, ELF_SPEC_HEADER *section);
00675 
00683 void melf_sectionSetLink(MELF *melf, ELF_SPEC_HEADER *section, Elf32_Word link);
00690 Elf32_Word melf_sectionGetLink(MELF *melf, ELF_SPEC_HEADER *section);
00691 
00699 void melf_sectionSetInfo(MELF *melf, ELF_SPEC_HEADER *section, Elf32_Word info);
00706 Elf32_Word melf_sectionGetInfo(MELF *melf, ELF_SPEC_HEADER *section);
00707 
00734 void melf_sectionSetFlags(MELF *melf, ELF_SPEC_HEADER *section, Elf32_Word flags);
00741 Elf32_Word melf_sectionGetFlags(MELF *melf, ELF_SPEC_HEADER *section);
00742 
00750 void melf_sectionSetEntrySize(MELF *melf, ELF_SPEC_HEADER *section, Elf32_Word entsize);
00758 Elf32_Word melf_sectionGetEntrySize(MELF *melf, ELF_SPEC_HEADER *section);
00759 
00767 unsigned long melf_sectionGetIndex(MELF *melf, ELF_SPEC_HEADER *section);
00768 
00777 void melf_sectionSetStringTableHeader(MELF *melf, ELF_SPEC_HEADER *section, ELF_SPEC_HEADER *stringTable);
00785 ELF_SPEC_HEADER *melf_sectionGetStringTableHeader(MELF *melf, ELF_SPEC_HEADER *section);
00786 
00794 unsigned long melf_sectionRemove(MELF *melf, unsigned long id);
00795 
00813 ELF_SPEC_HEADER *melf_programAdd(MELF *melf);
00820 ELF_SPEC_HEADER *melf_programGetEnum(MELF *melf);
00828 ELF_SPEC_HEADER *melf_programEnumNext(MELF *melf, ELF_SPEC_HEADER *en);
00836 ELF_SPEC_HEADER *melf_programEnumPrev(MELF *melf, ELF_SPEC_HEADER *en);
00837 
00860 void melf_programSetType(MELF *melf, ELF_SPEC_HEADER *program, Elf32_Word type);
00868 Elf32_Word melf_programGetType(MELF *melf, ELF_SPEC_HEADER *program);
00869 
00877 void melf_programSetVirtualAddress(MELF *melf, ELF_SPEC_HEADER *program, Elf32_Addr addr);
00885 Elf32_Addr melf_programGetVirtualAddress(MELF *melf, ELF_SPEC_HEADER *program);
00886 
00894 void melf_programSetPhysicalAddress(MELF *melf, ELF_SPEC_HEADER *program, Elf32_Addr addr);
00902 Elf32_Addr melf_programGetPhysicalAddress(MELF *melf, ELF_SPEC_HEADER *program);
00903 
00911 void melf_programSetVirtualSize(MELF *melf, ELF_SPEC_HEADER *program, Elf32_Word size);
00919 Elf32_Word melf_programGetVirtualSize(MELF *melf, ELF_SPEC_HEADER *program);
00920 
00928 void melf_programSetPhysicalSize(MELF *melf, ELF_SPEC_HEADER *program, Elf32_Word size);
00936 Elf32_Word melf_programGetPhysicalSize(MELF *melf, ELF_SPEC_HEADER *program);
00937 
00954 void melf_programSetFlags(MELF *melf, ELF_SPEC_HEADER *program, Elf32_Word flags);
00962 Elf32_Word melf_programGetFlags(MELF *melf, ELF_SPEC_HEADER *program);
00963 
00971 unsigned long melf_programRemove(MELF *melf, unsigned long id);
00972 
00992 ELF_SPEC_HEADER *melf_stringTableCreate(MELF *melf, const char *name);
00993 
01000 void melf_stringTableInitializeEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01007 void melf_stringTableSynchronizeEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01008 
01018 unsigned long melf_stringTableSetString(MELF *melf, ELF_SPEC_HEADER *stringTable, const char *name);
01028 unsigned long melf_stringTableGetStringIndex(MELF *melf, ELF_SPEC_HEADER *stringTable, const char *name);
01037 const char *melf_stringTableGetString(MELF *melf, ELF_SPEC_HEADER *stringTable, unsigned long index);
01038 
01056 ELF_SPEC_HEADER *melf_dynamicCreate(MELF *melf);
01057 
01064 void melf_dynamicInitializeEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01071 void melf_dynamicSynchronizeEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01072 
01073 
01136 Elf32_Dyn *melf_dynamicAddTag(MELF *melf, ELF_SPEC_HEADER *dynamic, Elf32_Sword tag, Elf32_Word val);
01145 unsigned long melf_dynamicRemoveTag(MELF *melf, ELF_SPEC_HEADER *dynamic, Elf32_Sword tag);
01154 unsigned long melf_dynamicRemoveIndex(MELF *melf, ELF_SPEC_HEADER *dynamic, unsigned long index);
01163 Elf32_Dyn *melf_dynamicGetTag(MELF *melf, ELF_SPEC_HEADER *dynamic, Elf32_Sword tag);
01172 Elf32_Dyn *melf_dynamicGetIndex(MELF *melf, ELF_SPEC_HEADER *dynamic, unsigned long index);
01182 unsigned long melf_dynamicSetTag(MELF *melf, ELF_SPEC_HEADER *dynamic, Elf32_Sword tag, Elf32_Word val);
01192 unsigned long melf_dynamicSetIndex(MELF *melf, ELF_SPEC_HEADER *dynamic, unsigned long index, Elf32_Word val);
01193 
01213 ELF_SPEC_HEADER *melf_noteCreate(MELF *melf, const char *name, unsigned long createProgramHeader);
01214 
01221 void melf_noteInitializeEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01228 void melf_noteSynchronizeEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01229 
01276 Elf32_Nhdr *melf_noteAdd(MELF *melf, ELF_SPEC_HEADER *note, Elf32_Word type, const char *name, unsigned char *desc, unsigned long descLength);
01285 Elf32_Nhdr *melf_noteEnum(MELF *melf, ELF_SPEC_HEADER *note, unsigned long index);
01294 unsigned long melf_noteRemove(MELF *melf, ELF_SPEC_HEADER *note, Elf32_Nhdr *item);
01295 
01304 unsigned long melf_noteGetType(MELF *melf, ELF_SPEC_HEADER *note, Elf32_Nhdr *item);
01312 const char *melf_noteGetName(MELF *melf, ELF_SPEC_HEADER *note, Elf32_Nhdr *item);
01320 unsigned char *melf_noteGetDesc(MELF *melf, ELF_SPEC_HEADER *note, Elf32_Nhdr *item);
01321 
01340 ELF_SPEC_HEADER *melf_symbolTableCreate(MELF *melf, const char *name);
01341 
01348 void melf_symbolTableInitializeEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01355 void melf_symbolTableSynchronizeEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01356 
01364 Elf32_Sym *melf_symbolTableAddSymbol(MELF *melf, ELF_SPEC_HEADER *symTable, const char *name);
01373 Elf32_Sym *melf_symbolTableEnum(MELF *melf, ELF_SPEC_HEADER *symTable, unsigned long index);
01382 unsigned long melf_symbolTableRemoveSymbol(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *symbol);
01383 
01393 unsigned long melf_symbolTableRecalculateOffsets(MELF *melf, ELF_SPEC_HEADER *relatedSection, unsigned long offset, unsigned long contentLength);
01394 
01414 void melf_symbolSetName(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *sym, const char *name);
01422 const char *melf_symbolGetName(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *sym);
01423 
01432 void melf_symbolSetValue(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *sym, Elf32_Addr value);
01441 Elf32_Addr melf_symbolGetValue(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *sym);
01442 
01451 void melf_symbolSetSize(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *sym, Elf32_Word size);
01460 Elf32_Word melf_symbolGetSize(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *sym);
01461 
01479 void melf_symbolSetBinding(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *sym, unsigned char binding);
01488 unsigned char melf_symbolGetBinding(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *sym);
01489 
01513 void melf_symbolSetType(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *sym, unsigned char type);
01522 unsigned char melf_symbolGetType(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *sym);
01523 
01532 void melf_symbolSetSectionIndex(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *sym, Elf32_Half shndx);
01541 Elf32_Half melf_symbolGetSectionIndex(MELF *melf, ELF_SPEC_HEADER *symTable, Elf32_Sym *sym);
01542 
01559 typedef enum _Elf32_ResType {
01563         ELF_RES_TYPE_UNKNOWN = 0,
01567         ELF_RES_TYPE_STRING  = 1,
01571         ELF_RES_TYPE_BINARY  = 2,
01572 } Elf32_ResType;
01573 
01579 typedef struct _Elf32_Res {
01580 
01581         // Stored on disk:
01582         struct _Elf32_Res_Header {
01583 
01587                 unsigned long length;
01591                 Elf32_ResType type;
01595                 unsigned long identifier;
01596         } header;
01597 
01601         void          *data;    
01602 
01603         // Not stored on disk, internal use only:
01607         unsigned long index;
01611         unsigned long offset;
01612 
01613 } Elf32_Res;
01614 
01621 ELF_SPEC_HEADER *melf_resOpen(MELF *melf);
01628 ELF_SPEC_HEADER *melf_resCreate(MELF *melf);
01629 
01636 void melf_resInitializeEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01643 void melf_resSynchronizeEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01644 
01654 unsigned char melf_resEnum(MELF *melf, ELF_SPEC_HEADER *resTable, unsigned long index, Elf32_Res *res);
01664 unsigned char melf_resGetType(MELF *melf, ELF_SPEC_HEADER *resTable, Elf32_ResType type, Elf32_Res *res);
01674 unsigned char melf_resGetId(MELF *melf, ELF_SPEC_HEADER *resTable, unsigned long id, Elf32_Res *res);
01686 unsigned char melf_resAdd(MELF *melf, ELF_SPEC_HEADER *resTable, Elf32_ResType type, unsigned long length, unsigned long id, void *data);
01699 unsigned char melf_resUpdate(MELF *melf, ELF_SPEC_HEADER *resTable, Elf32_ResType type, unsigned long length, unsigned long id, void *data);
01708 unsigned char melf_resRemove(MELF *melf, ELF_SPEC_HEADER *resTable, Elf32_Res *res);
01715 void melf_resClose(MELF *melf, ELF_SPEC_HEADER *resTable);
01716 
01737 ELF_SPEC_HEADER *melf_relocTableCreate(MELF *melf, const char *name, unsigned char hasAddends);
01738 
01745 void melf_relocInitializeRelEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01752 void melf_relocSynchronizeRelEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01759 void melf_relocInitializeRelaEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01766 void melf_relocSynchronizeRelaEntryList(MELF *melf, ELF_SPEC_HEADER *sect);
01767 
01775 void melf_relocTableSetSymbolTableHeader(MELF *melf, ELF_SPEC_HEADER *relocTable, ELF_SPEC_HEADER *symTable);
01776 
01784 Elf32_Rel *melf_relocTableAddRel(MELF *melf, ELF_SPEC_HEADER *relocTable);
01793 Elf32_Rel *melf_relocTableEnumRel(MELF *melf, ELF_SPEC_HEADER *relocTable, unsigned long index);
01803 unsigned long melf_relocTableRemoveRel(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rel *rel);
01804 
01812 Elf32_Rela *melf_relocTableAddRela(MELF *melf, ELF_SPEC_HEADER *relocTable);
01821 Elf32_Rela *melf_relocTableEnumRela(MELF *melf, ELF_SPEC_HEADER *relocTable, unsigned long index);
01831 unsigned long melf_relocTableRemoveRela(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rela *rela);
01841 unsigned long melf_relocRecalculateOffsets(MELF *melf, ELF_SPEC_HEADER *relatedSection, unsigned long offset, unsigned long contentLength);
01842 
01843 
01854 void melf_relocRelInitialize(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rel *rel, Elf32_Addr offset, unsigned char symbolIndex, unsigned char type);
01855 
01864 void melf_relocRelSetOffset(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rel *rel, Elf32_Addr offset);
01873 Elf32_Addr melf_relocRelGetOffset(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rel *rel);
01874 
01883 void melf_relocRelSetSymbol(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rel *rel, unsigned char symbolIndex);
01892 unsigned char melf_relocRelGetSymbol(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rel *rel);
01893 
01927 void melf_relocRelSetType(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rel *rel, unsigned char type);
01936 unsigned char melf_relocRelGetType(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rel *rel);
01937 
01949 void melf_relocRelaInitialize(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rela *rela, Elf32_Addr offset, unsigned char symbolIndex, unsigned char type, Elf32_Sword addend);
01950 
01959 void melf_relocRelaSetOffset(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rela *rela, Elf32_Addr offset);
01968 Elf32_Addr melf_relocRelaGetOffset(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rela *rela);
01969 
01978 void melf_relocRelaSetSymbol(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rela *rela, unsigned char symbolIndex);
01987 unsigned char melf_relocRelaGetSymbol(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rela *rela);
01988 
02022 void melf_relocRelaSetType(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rela *rela, unsigned char type);
02031 unsigned char melf_relocRelaGetType(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rela *rela);
02032 
02041 void melf_relocRelaSetAddend(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rela *rela, Elf32_Sword addend);
02050 Elf32_Sword melf_relocRelaGetAddend(MELF *melf, ELF_SPEC_HEADER *relocTable, Elf32_Rela *rela);
02051 
02056 #ifdef MELF_COMP
02057 #include "melf_internal.h"
02058 #endif
02059 
02060 #endif

Generated on Tue May 4 00:14:34 2004 for libmelf by doxygen1.2.15