Alien-LibJIT
view release on metacpan or search on metacpan
libjit/jit/jit-elf-write.c view on Meta::CPAN
section = &(writeelf->sections[writeelf->regular_string_section]);
data = (char *)jit_realloc(section->data, section->data_len + name_len);
if(!data)
{
return 0;
}
section->data = data;
jit_strcpy(data + section->data_len, name);
index = (Elf_Word)(section->data_len);
section->data_len += name_len;
return index;
}
/*
* Get a string from the dynamic string section.
*/
static const char *get_dyn_string(jit_writeelf_t writeelf, Elf_Word index)
{
if(writeelf->dynamic_string_section < 0)
{
/* The dynamic string section has not been created yet */
return 0;
}
else
{
/* Retrieve the pointer to the string's starting point */
return writeelf->sections[writeelf->dynamic_string_section].data +
(jit_nuint)index;
}
}
/*
* Add a string to the dynamic string section.
*
* TODO: use a hash table to cache previous names.
*/
static Elf_Word add_dyn_string(jit_writeelf_t writeelf, const char *name)
{
jit_section_t section;
char *data;
Elf_Word index;
unsigned int name_len = jit_strlen(name) + 1;
section = &(writeelf->sections[writeelf->dynamic_string_section]);
data = (char *)jit_realloc(section->data, section->data_len + name_len);
if(!data)
{
return 0;
}
section->data = data;
jit_strcpy(data + section->data_len, name);
index = (Elf_Word)(section->data_len);
section->data_len += name_len;
return index;
}
/*
* Get or add a section.
*/
static jit_section_t get_section
(jit_writeelf_t writeelf, const char *name, jit_int type,
Elf_Word flags, Elf_Word entry_size, Elf_Word alignment)
{
int index;
jit_section_t section;
/* Search the section table for an existing section by this name */
for(index = 0; index < writeelf->num_sections; ++index)
{
section = &(writeelf->sections[index]);
if(!jit_strcmp(get_string(writeelf, section->shdr.sh_name), name))
{
return section;
}
}
/* Create a new section and clear it */
section = (jit_section_t)jit_realloc
(writeelf->sections,
(writeelf->num_sections + 1) * sizeof(struct jit_section));
if(!section)
{
return 0;
}
writeelf->sections = section;
section += writeelf->num_sections;
jit_memzero(section, sizeof(struct jit_section));
/* Set the section's name. If this is the first section created,
then it is the string table itself, and we have to add the
name to the section itself to start the ball rolling */
if(writeelf->regular_string_section < 0)
{
section->data = (char *)jit_malloc(jit_strlen(name) + 2);
if(!(section->data))
{
return 0;
}
section->data_len = jit_strlen(name) + 2;
section->data[0] = '\0'; /* Empty string is always first */
jit_strcpy(section->data + 1, name);
section->shdr.sh_name = 1;
writeelf->regular_string_section = writeelf->num_sections;
}
else
{
section->shdr.sh_name = add_string(writeelf, name);
if(!(section->shdr.sh_name))
{
return 0;
}
}
/* Set the other section properties */
section->shdr.sh_type = (Elf_Word)type;
section->shdr.sh_flags = flags;
section->shdr.sh_entsize = entry_size;
section->shdr.sh_addralign = alignment;
/* Increase the section count and return */
++(writeelf->num_sections);
return section;
}
/*
* Append data to a section.
*/
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;
}
}
( run in 1.287 second using v1.01-cache-2.11-cpan-796a6f069b2 )