History

x86-64: Combine buffers of sections before we call tcc_run().

- Now we can run tcc -run tcc.c successfully, though there are some bugs. - Remove jmp_table and got_table and use text_section for got and plt entries. - Combine buffers in tcc_relocate(). - Use R_X86_64_64 instead of R_X86_64_32 for R_DATA_32 (now the name R_DATA_32 is inappropriate...).

Shinichiro Hamaji committed 17 years ago

Commit fcf2e591 parent 830b7533 files · +56 −47Browse files

Unified · Split

3 files
  1. tcc.c +52 −19
  2. tccelf.c +3 −27
  3. x86_64-gen.c +1 −1
tcc.c+52−19
@@ -542,15 +542,6 @@ struct TCCState { · 541 unmodified lines
542 542
543 /* output file for preprocessing */543 /* output file for preprocessing */
544 FILE *outfile;544 FILE *outfile;
545
546#ifdef TCC_TARGET_X86_64
547 /* buffer to store jump tables used when the output is memory */
548 char *jmp_table;
549 int jmp_table_num;
550 /* buffer to store got tables used when the output is memory */
551 void **got_table;
552 int got_table_num;
553#endif
554};545};
555 546
556/* The current value can be: */547/* The current value can be: */
@@ -1315,7 +1306,12 @@ Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags) · 758 unmodified lines
1315 1306
1316static void free_section(Section *s)1307static void free_section(Section *s)
1317{1308{
1318 tcc_free(s->data);1309#ifdef TCC_TARGET_X86_64
1310 /* after tcc_relocate(), some sections share the data buffer.
1311 let's check if the data is allocated not to free the shared buffers */
1312 if (s->data_allocated)
1313#endif
1314 tcc_free(s->data);
1319}1315}
1320 1316
1321/* realloc section and set its content to zero */1317/* realloc section and set its content to zero */
@@ -10276,6 +10272,51 @@ int tcc_relocate(TCCState *s1) · 8954 unmodified lines
10276#ifndef TCC_TARGET_PE10272#ifndef TCC_TARGET_PE
10277 build_got_entries(s1);10273 build_got_entries(s1);
10278#endif10274#endif
10275
10276#ifdef TCC_TARGET_X86_64
10277 {
10278 /* If the distance of two sections are longer than 32bit, our
10279 program will crash. Let's combine all sections which are
10280 necessary to run the program into a single buffer in the
10281 text section */
10282 unsigned char *p;
10283 int size;
10284 /* calculate the size of buffers we need */
10285 size = 0;
10286 for(i = 1; i < s1->nb_sections; i++) {
10287 s = s1->sections[i];
10288 if (s->sh_flags & SHF_ALLOC) {
10289 size += s->data_offset;
10290 }
10291 }
10292 /* double the size of the buffer for got and plt entries
10293 XXX: calculate exact size for them? */
10294 section_realloc(text_section, size * 2);
10295 p = text_section->data + text_section->data_offset;
10296 /* we will put got and plt after this offset */
10297 text_section->data_offset = size;
10298
10299 for(i = 1; i < s1->nb_sections; i++) {
10300 s = s1->sections[i];
10301 if (s->sh_flags & SHF_ALLOC) {
10302 if (s != text_section && s->data_offset) {
10303 if (s->sh_type == SHT_NOBITS) {
10304 /* for bss section */
10305 memset(p, 0, s->data_offset);
10306 } else {
10307 memcpy(p, s->data, s->data_offset);
10308 tcc_free(s->data);
10309 }
10310 s->data = p;
10311 /* we won't free s->data for this section */
10312 s->data_allocated = 0;
10313 p += s->data_offset;
10314 }
10315 s->sh_addr = (unsigned long)s->data;
10316 }
10317 }
10318 }
10319#else
10279 /* compute relocation address : section are relocated in place. We10320 /* compute relocation address : section are relocated in place. We
10280 also alloc the bss space */10321 also alloc the bss space */
10281 for(i = 1; i < s1->nb_sections; i++) {10322 for(i = 1; i < s1->nb_sections; i++) {
@@ -10286,6 +10327,7 @@ int tcc_relocate(TCCState *s1) · 4 unmodified lines
10286 s->sh_addr = (unsigned long)s->data;10327 s->sh_addr = (unsigned long)s->data;
10287 }10328 }
10288 }10329 }
10330#endif
10289 10331
10290 relocate_syms(s1, 1);10332 relocate_syms(s1, 1);
10291 10333
@@ -10505,11 +10547,6 @@ TCCState *tcc_new(void) · 213 unmodified lines
10505 /* XXX: currently the PE linker is not ready to support that */10547 /* XXX: currently the PE linker is not ready to support that */
10506 s->leading_underscore = 1;10548 s->leading_underscore = 1;
10507#endif10549#endif
10508
10509#ifdef TCC_TARGET_X86_64
10510 s->jmp_table = NULL;
10511 s->got_table = NULL;
10512#endif
10513 return s;10550 return s;
10514}10551}
10515 10552
@@ -10546,10 +10583,6 @@ void tcc_delete(TCCState *s1) · 30 unmodified lines
10546 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);10583 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
10547 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);10584 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
10548 10585
10549#ifdef TCC_TARGET_X86_64
10550 tcc_free(s1->jmp_table);
10551 tcc_free(s1->got_table);
10552#endif
10553 tcc_free(s1);10586 tcc_free(s1);
10554}10587}
10555 10588
tccelf.c+3−27
@@ -479,22 +479,9 @@ static void relocate_syms(TCCState *s1, int do_resolve) · 478 unmodified lines
479 479
480#ifdef TCC_TARGET_X86_64480#ifdef TCC_TARGET_X86_64
481#define JMP_TABLE_ENTRY_SIZE 14481#define JMP_TABLE_ENTRY_SIZE 14
482#define JMP_TABLE_ENTRY_MAX_NUM 4096
483static unsigned long add_jmp_table(TCCState *s1, unsigned long val)482static unsigned long add_jmp_table(TCCState *s1, unsigned long val)
484{483{
485 char *p;484 char *p = (char *)section_ptr_add(text_section, JMP_TABLE_ENTRY_SIZE);
486 if (!s1->jmp_table) {
487 int size = JMP_TABLE_ENTRY_SIZE * JMP_TABLE_ENTRY_MAX_NUM;
488 s1->jmp_table_num = 0;
489 s1->jmp_table = (char *)tcc_malloc(size);
490 set_pages_executable(s1->jmp_table, size);
491 }
492 if (s1->jmp_table_num == JMP_TABLE_ENTRY_MAX_NUM) {
493 error("relocating >%d symbols are not supported",
494 JMP_TABLE_ENTRY_MAX_NUM);
495 }
496 p = s1->jmp_table + s1->jmp_table_num * JMP_TABLE_ENTRY_SIZE;
497 s1->jmp_table_num++;
498 /* jmp *0x0(%rip) */485 /* jmp *0x0(%rip) */
499 p[0] = 0xff;486 p[0] = 0xff;
500 p[1] = 0x25;487 p[1] = 0x25;
@@ -503,21 +490,10 @@ static unsigned long add_jmp_table(TCCState *s1, unsigned long val) · 2 unmodified lines
503 return (unsigned long)p;490 return (unsigned long)p;
504}491}
505 492
506#define GOT_TABLE_ENTRY_MAX_NUM 4096
507static unsigned long add_got_table(TCCState *s1, unsigned long val)493static unsigned long add_got_table(TCCState *s1, unsigned long val)
508{494{
509 unsigned long *p;495 unsigned long *p =
510 if (!s1->got_table) {496 (unsigned long *)section_ptr_add(text_section, sizeof(void *));
511 int size = sizeof(void *) * GOT_TABLE_ENTRY_MAX_NUM;
512 s1->got_table_num = 0;
513 s1->got_table = (char *)tcc_malloc(size);
514 }
515 if (s1->got_table_num == GOT_TABLE_ENTRY_MAX_NUM) {
516 error("relocating >%d symbols are not supported",
517 GOT_TABLE_ENTRY_MAX_NUM);
518 }
519 p = s1->got_table + s1->got_table_num;
520 s1->got_table_num++;
521 *p = val;497 *p = val;
522 return (unsigned long)p;498 return (unsigned long)p;
523}499}
x86_64-gen.c+1−1
@@ -91,7 +91,7 @@ int reg_classes[NB_REGS] = { · 90 unmodified lines
91#define EM_TCC_TARGET EM_X86_6491#define EM_TCC_TARGET EM_X86_64
92 92
93/* relocation type for 32 bit data relocation */93/* relocation type for 32 bit data relocation */
94#define R_DATA_32 R_X86_64_3294#define R_DATA_32 R_X86_64_64
95#define R_JMP_SLOT R_X86_64_JUMP_SLOT95#define R_JMP_SLOT R_X86_64_JUMP_SLOT
96#define R_COPY R_X86_64_COPY96#define R_COPY R_X86_64_COPY
97 97

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t