How to do a elf linker in a C kernel ?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
SamyPesse
Posts: 17
Joined: Mon Apr 13, 2009 2:56 pm
Location: France

How to do a elf linker in a C kernel ?

Post by SamyPesse »

Hello everybody, for my os, i want to do a elf linker to load dynamic library, i have already a elf loader but i don't how the dynamic elf works,
This is my elf loader code to load static elf :
sorry for my english, i'm french

Code: Select all

u32 load_elf(char *file, struct process *proc)
{
	char *p;
	u32 v_begin, v_end;
	Elf32_Ehdr *hdr;
	Elf32_Phdr *p_entry;
	int i, pe;

	hdr = (Elf32_Ehdr *) file;
	p_entry = (Elf32_Phdr *) (file + hdr->e_phoff);	/* Program header table offset */

	if (!is_elf(file)) {
		printk("INFO: load_elf(): file not in ELF format !\n");
		return 0;
	}

	for (pe = 0; pe < hdr->e_phnum; pe++, p_entry++) {	/* Read each entry */

		if (p_entry->p_type == PT_LOAD) {
			v_begin = p_entry->p_vaddr;
			v_end = p_entry->p_vaddr + p_entry->p_memsz;

			if (v_begin < USER_OFFSET) {
				printk ("INFO: load_elf(): can't load executable below %p\n", USER_OFFSET);
				return 0;
			}

			if (v_end > USER_STACK) {
				printk ("INFO: load_elf(): can't load executable above %p\n", USER_STACK);
				return 0;
			}


			/* Description de la zone exec + rodata */
			if (p_entry->p_flags == PF_X + PF_R) {	
				proc->b_exec = (char*) v_begin;
				proc->e_exec = (char*) v_end;
			}

			/* Description de la zone bss */
			if (p_entry->p_flags == PF_W + PF_R) {	
				proc->b_bss = (char*) v_begin;
				proc->e_bss = (char*) v_end;
			}

			memcpy((char *) v_begin, (char *) (file + p_entry->p_offset), p_entry->p_filesz);

			if (p_entry->p_memsz > p_entry->p_filesz)
				for (i = p_entry->p_filesz, p = (char *) p_entry->p_vaddr; i < (int)(p_entry->p_memsz); i++)
					p[i] = 0;
		}
	}

	/* Return program entry point */
	return hdr->e_entry;
}
DeleteMe
Posts: 8
Joined: Tue Oct 21, 2008 12:22 pm
Location: denmark
Contact:

Re: How to do a elf linker in a C kernel ?

Post by DeleteMe »

you shuodt implemtent reclocations. as i can see you have'nt impemented it yet. then you can load object files, with undfinned symbols. =D>

sorry aboute my bad englisk, im from danmark
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: How to do a elf linker in a C kernel ?

Post by thepowersgang »

I suggest reading the ELF specs.
You would want to implement the sections on the "Program Header", "Program Loading" and "Dynamic Linking" (in my copy they are in section 2)
It is actually quite simple to do, once you understand the specifcation.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Post Reply