From f4f600fc52d38e4a287913050880647144d1873c Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Sun, 16 Dec 2012 21:06:38 +0100 Subject: imxtools/sbtools: add elf function (sort by address) Change-Id: Ib68746e11b43eadbbe0443626d4dc65d998348fa --- utils/imxtools/sbtools/elf.c | 57 +++++++++++++++++++++++++++++++++++++++++++- utils/imxtools/sbtools/elf.h | 1 + 2 files changed, 57 insertions(+), 1 deletion(-) (limited to 'utils/imxtools/sbtools') diff --git a/utils/imxtools/sbtools/elf.c b/utils/imxtools/sbtools/elf.c index 35bc1605d3..c41fc27fdd 100644 --- a/utils/imxtools/sbtools/elf.c +++ b/utils/imxtools/sbtools/elf.c @@ -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(§ions[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 = ¶ms->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, §ions[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) { diff --git a/utils/imxtools/sbtools/elf.h b/utils/imxtools/sbtools/elf.h index ae4e3b4225..2e14e66fd3 100644 --- a/utils/imxtools/sbtools/elf.h +++ b/utils/imxtools/sbtools/elf.h @@ -83,6 +83,7 @@ void elf_add_fill_section(struct elf_params_t *params, uint32_t elf_translate_virtual_address(struct elf_params_t *params, uint32_t addr); void elf_translate_addresses(struct elf_params_t *params); void elf_simplify(struct elf_params_t *params); +void elf_sort_by_address(struct elf_params_t *params); void elf_write_file(struct elf_params_t *params, elf_write_fn_t write, elf_printf_fn_t printf, void *user); bool elf_read_file(struct elf_params_t *params, elf_read_fn_t read, elf_printf_fn_t printf, void *user); -- cgit v1.2.3