1
0
Fork 0
forked from len0rd/rockbox

imxtools/sbtools: add elf function (sort by address)

Change-Id: Ib68746e11b43eadbbe0443626d4dc65d998348fa
This commit is contained in:
Amaury Pouly 2012-12-16 21:06:38 +01:00
parent be9787f2e6
commit f4f600fc52
2 changed files with 57 additions and 1 deletions

View file

@ -229,7 +229,7 @@ static int elf_simplify_compare(const void *a, const void *b)
void elf_simplify(struct elf_params_t *params)
{
/** find all sections of the same times which are contiguous and merge them */
/** find all sections of the same types which are contiguous and merge them */
/* count sections */
int nr_sections = 0;
@ -312,6 +312,61 @@ void elf_simplify(struct elf_params_t *params)
free(sections);
}
/* sort by increasing address */
static int elf_addr_compare(const void *a, const void *b)
{
const struct elf_section_t *sa = a;
const struct elf_section_t *sb = b;
return sa->addr - sb->addr;
}
void elf_sort_by_address(struct elf_params_t *params)
{
/** sort sections by address */
/* count sections */
int nr_sections = 0;
struct elf_section_t *cur_sec = params->first_section;
while(cur_sec)
{
nr_sections++;
cur_sec = cur_sec->next;
}
/* put all sections in an array and free list */
struct elf_section_t *sections = malloc(sizeof(struct elf_section_t) * nr_sections);
cur_sec = params->first_section;
for(int i = 0; i < nr_sections; i++)
{
memcpy(&sections[i], cur_sec, sizeof(struct elf_section_t));
struct elf_section_t *old = cur_sec;
cur_sec = cur_sec->next;
free(old);
}
/* sort them by type and increasing addresses */
qsort(sections, nr_sections, sizeof(struct elf_section_t), &elf_addr_compare);
/* put back on a list and free array */
struct elf_section_t **prev_ptr = &params->first_section;
struct elf_section_t *prev_sec = NULL;
for(int i = 0; i < nr_sections; i++)
{
/* skip empty sections produced by simplification */
if(sections[i].size == 0)
continue;
struct elf_section_t *sec = malloc(sizeof(struct elf_section_t));
memcpy(sec, &sections[i], sizeof(struct elf_section_t));
*prev_ptr = sec;
prev_ptr = &sec->next;
prev_sec = sec;
}
*prev_ptr = NULL;
params->last_section = prev_sec;
free(sections);
}
void elf_write_file(struct elf_params_t *params, elf_write_fn_t write,
elf_printf_fn_t printf, void *user)
{