tccrun: sort sections

Sort executable before other sections.

Also, apply RUN_SECTION_ALIGNMENT=63 for TCC_TARGET_I386
as well.
This commit is contained in:
grischka 2017-02-05 14:00:42 +01:00
parent ea2c36c5a9
commit 85fca9e924

View File

@ -155,7 +155,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
return (*prog_main)(argc, argv); return (*prog_main)(argc, argv);
} }
#ifdef TCC_TARGET_X86_64 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
#define RUN_SECTION_ALIGNMENT 63 #define RUN_SECTION_ALIGNMENT 63
#else #else
#define RUN_SECTION_ALIGNMENT 15 #define RUN_SECTION_ALIGNMENT 15
@ -166,9 +166,8 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
static int tcc_relocate_ex(TCCState *s1, void *ptr) static int tcc_relocate_ex(TCCState *s1, void *ptr)
{ {
Section *s; Section *s;
unsigned long offset, length; unsigned offset, length, fill, i, k;
addr_t mem; addr_t mem;
int i;
if (NULL == ptr) { if (NULL == ptr) {
s1->nb_errors = 0; s1->nb_errors = 0;
@ -185,17 +184,33 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr)
} }
offset = 0, mem = (addr_t)ptr; offset = 0, mem = (addr_t)ptr;
mem += -(int)mem & RUN_SECTION_ALIGNMENT; fill = -mem & RUN_SECTION_ALIGNMENT;
#ifdef _WIN64 #ifdef _WIN64
offset += sizeof (void*); offset += sizeof (void*);
#endif #endif
for(i = 1; i < s1->nb_sections; i++) { for (k = 0; k < 2; ++k) {
s = s1->sections[i]; for(i = 1; i < s1->nb_sections; i++) {
if (0 == (s->sh_flags & SHF_ALLOC)) s = s1->sections[i];
continue; if (0 == (s->sh_flags & SHF_ALLOC))
offset = (offset + RUN_SECTION_ALIGNMENT) & ~RUN_SECTION_ALIGNMENT; continue;
s->sh_addr = mem ? mem + offset : 0; if (k != !(s->sh_flags & SHF_EXECINSTR))
offset += s->data_offset; continue;
offset += fill;
s->sh_addr = mem ? mem + offset : 0;
#if 0
if (mem)
printf("%-16s +%02lx %p %04x\n",
s->name, fill, (void*)s->sh_addr, (unsigned)s->data_offset);
#endif
offset += s->data_offset;
fill = -(mem + offset) & 15;
}
#if RUN_SECTION_ALIGNMENT > 15
/* To avoid that x86 processors would reload cached instructions each time
when data is written in the near, we need to make sure that code and data
do not share the same 64 byte unit */
fill = -(mem + offset) & RUN_SECTION_ALIGNMENT;
#endif
} }
/* relocate symbols */ /* relocate symbols */
@ -223,7 +238,6 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr)
if (0 == (s->sh_flags & SHF_ALLOC)) if (0 == (s->sh_flags & SHF_ALLOC))
continue; continue;
length = s->data_offset; length = s->data_offset;
// printf("%-12s %08lx %04x\n", s->name, s->sh_addr, length);
ptr = (void*)s->sh_addr; ptr = (void*)s->sh_addr;
if (NULL == s->data || s->sh_type == SHT_NOBITS) if (NULL == s->data || s->sh_type == SHT_NOBITS)
memset(ptr, 0, length); memset(ptr, 0, length);