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
542542
543543 /* output file for preprocessing */
544544 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
554545 };
555546
556547 /* 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
13151306
13161307 static void free_section(Section *s)
13171308 {
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);
13191315 }
13201316
13211317 /* realloc section and set its content to zero */
@@ -10276,6 +10272,51 @@ int tcc_relocate(TCCState *s1) · 8954 unmodified lines
1027610272 #ifndef TCC_TARGET_PE
1027710273 build_got_entries(s1);
1027810274 #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
1027910320 /* compute relocation address : section are relocated in place. We
1028010321 also alloc the bss space */
1028110322 for(i = 1; i < s1->nb_sections; i++) {
@@ -10286,6 +10327,7 @@ int tcc_relocate(TCCState *s1) · 4 unmodified lines
1028610327 s->sh_addr = (unsigned long)s->data;
1028710328 }
1028810329 }
10330+#endif
1028910331
1029010332 relocate_syms(s1, 1);
1029110333
@@ -10505,11 +10547,6 @@ TCCState *tcc_new(void) · 213 unmodified lines
1050510547 /* XXX: currently the PE linker is not ready to support that */
1050610548 s->leading_underscore = 1;
1050710549 #endif
10508−
10509−#ifdef TCC_TARGET_X86_64
10510− s->jmp_table = NULL;
10511− s->got_table = NULL;
10512−#endif
1051310550 return s;
1051410551 }
1051510552
@@ -10546,10 +10583,6 @@ void tcc_delete(TCCState *s1) · 30 unmodified lines
1054610583 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
1054710584 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
1054810585
10549−#ifdef TCC_TARGET_X86_64
10550− tcc_free(s1->jmp_table);
10551− tcc_free(s1->got_table);
10552−#endif
1055310586 tcc_free(s1);
1055410587 }
1055510588
tccelf.c+3−27
@@ -479,22 +479,9 @@ static void relocate_syms(TCCState *s1, int do_resolve) · 478 unmodified lines
479479
480480 #ifdef TCC_TARGET_X86_64
481481 #define JMP_TABLE_ENTRY_SIZE 14
482−#define JMP_TABLE_ENTRY_MAX_NUM 4096
483482 static unsigned long add_jmp_table(TCCState *s1, unsigned long val)
484483 {
485− char *p;
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++;
484+ char *p = (char *)section_ptr_add(text_section, JMP_TABLE_ENTRY_SIZE);
498485 /* jmp *0x0(%rip) */
499486 p[0] = 0xff;
500487 p[1] = 0x25;
@@ -503,21 +490,10 @@ static unsigned long add_jmp_table(TCCState *s1, unsigned long val) · 2 unmodified lines
503490 return (unsigned long)p;
504491 }
505492
506−#define GOT_TABLE_ENTRY_MAX_NUM 4096
507493 static unsigned long add_got_table(TCCState *s1, unsigned long val)
508494 {
509− unsigned long *p;
510− if (!s1->got_table) {
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++;
495+ unsigned long *p =
496+ (unsigned long *)section_ptr_add(text_section, sizeof(void *));
521497 *p = val;
522498 return (unsigned long)p;
523499 }
x86_64-gen.c+1−1
@@ -91,7 +91,7 @@ int reg_classes[NB_REGS] = { · 90 unmodified lines
9191 #define EM_TCC_TARGET EM_X86_64
9292
9393 /* relocation type for 32 bit data relocation */
94−#define R_DATA_32 R_X86_64_32
94+#define R_DATA_32 R_X86_64_64
9595 #define R_JMP_SLOT R_X86_64_JUMP_SLOT
9696 #define R_COPY R_X86_64_COPY
9797

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