libmiASMaELF 0.0.1 review

Download
by rbytes.net on

libmiASMaELF is a library for generating relocatable object files that conform to the ELF format. libmiASMaELF library has no comp

License: GPL (GNU General Public License)
File size: 16K
Developer: Hareesh Nagarajan
0 stars award from rbytes.net

libmiASMaELF is a library for generating relocatable object files that conform to the ELF format.

libmiASMaELF library has no complex class hierarchy, hence it is extremly easy to use, unlike most other libraries that accomplish the same task. Documentation and examples are provided to demonstrate the use of the library.

< b >How does one use the library?< /b >

/* hello.c */
#include < iostream >
#include < vector >
#include "libmiasmaelf.h"

int main(void )
{
char text[] = {
'xB8', 'x04', 'x00', 'x00', 'x00', // mov eax, 4
'xBB', 'x01', 'x00', 'x00', 'x00', // mov ebx, 1
'xB9', 'x00', 'x00', 'x00', 'x00', // mov ecx, myvariable
'xBA', 'x0E', 'x00', 'x00', 'x00', // mov edx, 14
'xCD', 'x80', // int 0x80
'xB8', 'x01', 'x00', 'x00', 'x00', // mov eax, 1
'xCD', 'x80' // int 0x80
};

char data[] = {
'x48', 'x65', 'x6C', 'x6C', 'x6F',
'x2C', 'x20', 'x57', 'x6F', 'x72',
'x6C', 'x64', 'x21', 'x0A'
}; //Hello,World!

vector< char > vtext(&text[0], &text[29]);
vector< char > vdata(&data[0], &data[14]);
miasmaELF obj;

obj.InitializeELFHeader();
obj.InitializeSymbolTable();

obj.AddNewSection(".shstrtab",SHT_STRTAB, 0,0,0,0,0,0);
obj.AddNewSection(".text", SHT_PROGBITS,6,0,0,0,16,0);
obj.AddNewSection(".data", SHT_PROGBITS,3,0,0,0,16,0);
obj.AddNewSection(".symtab", SHT_SYMTAB, 0,0,
obj.GetSectionIndexOfType(SHT_STRTAB, ".strtab"),
0,
4,sizeof(Elf32_Sym));

obj.AddNewSection(".rel.text",SHT_REL,0,0,
obj.GetSectionIndexOfType(SHT_SYMTAB),
obj.GetSectionIndexOfType(SHT_PROGBITS, ".text"),
4,sizeof(Elf32_Rel));

obj.AddContents(vtext, obj.GetSectionIndexOfType(SHT_PROGBITS,".text"));
obj.AddContents(vdata, obj.GetSectionIndexOfType(SHT_PROGBITS,".data"));

obj.AddSymbol("_start",0,0, STB_WEAK, STT_FUNC,
obj.GetSectionIndexOfType(SHT_PROGBITS, ".text"));
obj.AddSymbol("myvariable",0,0, STB_GLOBAL, STT_OBJECT,
obj.GetSectionIndexOfType(SHT_PROGBITS, ".data"));

obj.AddRelocationEntry(11, obj.ReturnSymbolIndex("myvariable"),
R_386_RELATIVE,
obj.GetSectionIndexOfType(SHT_REL, ".rel.text"));

obj.PrepareFile();
obj.WriteFile("hello.o");
}

The library makes extensive use of vectors - a data structure that is a part of the Standard Template Library. We first create the machine language equivalents of every instruction and populate the vectors accordingly.

We then initialize the ELFHeader, the SymbolTable Initialization follows next. This is done after defining an object of type miasmaELF.

We then go on to initialize individual sections. The function

obj.GetSectionIndexOfType(SHT_PROGBITS, ".text")

is used when one wants to obtain the SectionIndex of a given section. We find this function helps greatly in linking the various structures that are described in elf.h. Here, it is used in building the section header of a particular section. It is imperative that the user of the library must have a general idea of the various structures that are involved.

We then invoke

obj.AddContents(vtext, obj.GetSectionIndexOfType(SHT_PROGBITS,".text"));

which add the contents to the text section. The 2nd argument to AddContents is the section that we are referring to. In this case it is the .text section, and from our example the Index=3.

We employ a similar technique to add Symbols and Relocation Entries. To finally write the file one must first must prepare it by invoking the function PrepareFile(...), and only then invoke WriteFile(FileName)

To compile hello.c one must link it with libmiasmaelf.o

libmiASMaELF 0.0.1 search tags