Alien-LibJIT

 view release on metacpan or  search on metacpan

libjit/jit/jit-elf-write.c  view on Meta::CPAN

static int add_to_section
	(jit_section_t section, const void *buf, unsigned int len)
{
	char *data = (char *)jit_realloc(section->data, section->data_len + len);
	if(!data)
	{
		return 0;
	}
	section->data = data;
	jit_memcpy(data + section->data_len, buf, len);
	section->data_len += len;
	return 1;
}

/*
 * Add an entry to the dynamic linking information section.
 */
static int add_dyn_info
	(jit_writeelf_t writeelf, int type, Elf_Addr value, int modify_existing)
{
	jit_section_t section;
	Elf_Dyn dyn;

	/* Get or create the ".dynamic" section */
	section = get_section(writeelf, ".dynamic", SHT_DYNAMIC,
						  SHF_WRITE | SHF_ALLOC,
						  sizeof(Elf_Dyn), sizeof(Elf_Dyn));
	if(!section)
	{
		return 0;
	}

	/* See if we already have this entry, and modify it as appropriate */
	if(modify_existing)
	{
		Elf_Dyn *existing = (Elf_Dyn *)(section->data);
		unsigned int num = section->data_len / sizeof(Elf_Dyn);
		while(num > 0)
		{
			if(existing->d_tag == type)
			{
				existing->d_un.d_ptr = value;
				return 1;
			}
			++existing;
			--num;
		}
	}

	/* Format the dynamic entry */
	jit_memzero(&dyn, sizeof(dyn));
	dyn.d_tag = type;
	dyn.d_un.d_ptr = value;

	/* Add the entry to the section's contents */
	return add_to_section(section, &dyn, sizeof(dyn));
}

/*@
 * @deftypefun jit_writeelf_t jit_writeelf_create (const char *@var{library_name})
 * Create an object to assist with the process of writing an ELF binary.
 * The @var{library_name} will be embedded into the binary.  Returns NULL
 * if out of memory.
 * @end deftypefun
@*/
jit_writeelf_t jit_writeelf_create(const char *library_name)
{
	jit_writeelf_t writeelf;
	Elf_Word name_index;
	union
	{
		jit_ushort value;
		unsigned char bytes[2];

	} un;
	jit_elf_info_t elf_info;

	/* Create the writer control structure */
	writeelf = jit_cnew(struct jit_writeelf);
	if(!writeelf)
	{
		return 0;
	}
	writeelf->regular_string_section = -1;
	writeelf->dynamic_string_section = -1;

	/* Create the regular string section for section names,
	   which must be the first section that we create */
	if(!get_section(writeelf, ".shstrtab", SHT_STRTAB, 0, 0, 0))
	{
		jit_writeelf_destroy(writeelf);
		return 0;
	}

	/* Create the dynamic string section, for dynamic linking symbols */
	if(!get_section(writeelf, ".dynstr", SHT_STRTAB, SHF_ALLOC, 0, 0))
	{
		jit_writeelf_destroy(writeelf);
		return 0;
	}
	writeelf->dynamic_string_section = writeelf->num_sections - 1;
	if(!add_dyn_string(writeelf, ""))
	{
		jit_writeelf_destroy(writeelf);
		return 0;
	}

	/* Add the library name to the dynamic linking information section */
	name_index = add_dyn_string(writeelf, library_name);
	if(!name_index)
	{
		jit_writeelf_destroy(writeelf);
		return 0;
	}
	if(!add_dyn_info(writeelf, DT_SONAME, (Elf_Addr)name_index, 0))
	{
		jit_writeelf_destroy(writeelf);
		return 0;
	}

	/* Fill in the Ehdr fields */



( run in 1.127 second using v1.01-cache-2.11-cpan-5b529ec07f3 )