- Add a macro TCC_OUTPUT_DLL_WITH_PLT. -- Now, the DLL with PLT support works only on x86-64, but we may be able to support it on all architectures eventually. - Define TCC_OUTPUT_DLL_WITH_PLT when target architecture is x86-64. - Current status (x86-64): -- Main program should be able to call functions in shared objects. -- Main program should be able to use global variables in shared objects. -- Shared objects should be able to call functions in main program. -- Shared objects can NOT use global variables in main program. - To fix the last issue, we may need to add support of -fPIC option in our code generator.
Unified · Split
| @@ -22,12 +22,17 @@ · 21 unmodified lines | |||
| 22 | #define ElfW_Rel ElfW(Rela) | 22 | #define ElfW_Rel ElfW(Rela) |
| 23 | #define SHT_RELX SHT_RELA | 23 | #define SHT_RELX SHT_RELA |
| 24 | #define REL_SECTION_FMT ".rela%s" | 24 | #define REL_SECTION_FMT ".rela%s" |
| 25 | /* x86-64 requires PLT for DLLs */ | ||
| 26 | #define TCC_OUTPUT_DLL_WITH_PLT | ||
| 25 | #else | 27 | #else |
| 26 | #define ElfW_Rel ElfW(Rel) | 28 | #define ElfW_Rel ElfW(Rel) |
| 27 | #define SHT_RELX SHT_REL | 29 | #define SHT_RELX SHT_REL |
| 28 | #define REL_SECTION_FMT ".rel%s" | 30 | #define REL_SECTION_FMT ".rel%s" |
| 29 | #endif | 31 | #endif |
| 30 | 32 | ||
| 33 | /* XXX: DLL with PLT would only work with x86-64 for now */ | ||
| 34 | //#define TCC_OUTPUT_DLL_WITH_PLT | ||
| 35 | |||
| 31 | static int put_elf_str(Section *s, const char *sym) | 36 | static int put_elf_str(Section *s, const char *sym) |
| 32 | { | 37 | { |
| 33 | int offset, len; | 38 | int offset, len; |
| @@ -902,11 +907,15 @@ static void put_got_entry(TCCState *s1, · 868 unmodified lines | |||
| 902 | uint8_t *p; | 907 | uint8_t *p; |
| 903 | int modrm; | 908 | int modrm; |
| 904 | 909 | ||
| 910 | #if defined(TCC_OUTPUT_DLL_WITH_PLT) | ||
| 911 | modrm = 0x25; | ||
| 912 | #else | ||
| 905 | /* if we build a DLL, we add a %ebx offset */ | 913 | /* if we build a DLL, we add a %ebx offset */ |
| 906 | if (s1->output_type == TCC_OUTPUT_DLL) | 914 | if (s1->output_type == TCC_OUTPUT_DLL) |
| 907 | modrm = 0xa3; | 915 | modrm = 0xa3; |
| 908 | else | 916 | else |
| 909 | modrm = 0x25; | 917 | modrm = 0x25; |
| 918 | #endif | ||
| 910 | 919 | ||
| 911 | /* add a PLT entry */ | 920 | /* add a PLT entry */ |
| 912 | plt = s1->plt; | 921 | plt = s1->plt; |
| @@ -932,7 +941,9 @@ static void put_got_entry(TCCState *s1, · 19 unmodified lines | |||
| 932 | 941 | ||
| 933 | /* the symbol is modified so that it will be relocated to | 942 | /* the symbol is modified so that it will be relocated to |
| 934 | the PLT */ | 943 | the PLT */ |
| 944 | #if !defined(TCC_OUTPUT_DLL_WITH_PLT) | ||
| 935 | if (s1->output_type == TCC_OUTPUT_EXE) | 945 | if (s1->output_type == TCC_OUTPUT_EXE) |
| 946 | #endif | ||
| 936 | offset = plt->data_offset - 16; | 947 | offset = plt->data_offset - 16; |
| 937 | } | 948 | } |
| 938 | #elif defined(TCC_TARGET_ARM) | 949 | #elif defined(TCC_TARGET_ARM) |
| @@ -1468,13 +1479,29 @@ int elf_output_file(TCCState *s1, const char *filename) · 529 unmodified lines | |||
| 1468 | sym < sym_end; | 1479 | sym < sym_end; |
| 1469 | sym++) { | 1480 | sym++) { |
| 1470 | if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { | 1481 | if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { |
| 1471 | name = symtab_section->link->data + sym->st_name; | 1482 | #if defined(TCC_OUTPUT_DLL_WITH_PLT) |
| 1472 | index = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, | 1483 | if (ELFW(ST_TYPE)(sym->st_info) == STT_FUNC && |
| 1473 | sym->st_info, 0, | 1484 | sym->st_shndx == SHN_UNDEF) { |
| 1474 | sym->st_shndx, name); | 1485 | put_got_entry(s1, R_JMP_SLOT, sym->st_size, |
| 1475 | s1->symtab_to_dynsym[sym - | 1486 | sym->st_info, |
| 1476 | (ElfW(Sym) *)symtab_section->data] = | 1487 | sym - (ElfW(Sym) *)symtab_section->data); |
| 1477 | index; | 1488 | } |
| 1489 | else if (ELFW(ST_TYPE)(sym->st_info) == STT_OBJECT) { | ||
| 1490 | put_got_entry(s1, R_X86_64_GLOB_DAT, sym->st_size, | ||
| 1491 | sym->st_info, | ||
| 1492 | sym - (ElfW(Sym) *)symtab_section->data); | ||
| 1493 | } | ||
| 1494 | else | ||
| 1495 | #endif | ||
| 1496 | { | ||
| 1497 | name = symtab_section->link->data + sym->st_name; | ||
| 1498 | index = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, | ||
| 1499 | sym->st_info, 0, | ||
| 1500 | sym->st_shndx, name); | ||
| 1501 | s1->symtab_to_dynsym[sym - | ||
| 1502 | (ElfW(Sym) *)symtab_section->data] = | ||
| 1503 | index; | ||
| 1504 | } | ||
| 1478 | } | 1505 | } |
| 1479 | } | 1506 | } |
| 1480 | } | 1507 | } |
| @@ -1727,7 +1754,11 @@ int elf_output_file(TCCState *s1, const char *filename) · 246 unmodified lines | |||
| 1727 | put32(s1->got->data, dynamic->sh_addr); | 1754 | put32(s1->got->data, dynamic->sh_addr); |
| 1728 | 1755 | ||
| 1729 | /* relocate the PLT */ | 1756 | /* relocate the PLT */ |
| 1730 | if (file_type == TCC_OUTPUT_EXE) { | 1757 | if (file_type == TCC_OUTPUT_EXE |
| 1758 | #if defined(TCC_OUTPUT_DLL_WITH_PLT) | ||
| 1759 | || file_type == TCC_OUTPUT_DLL | ||
| 1760 | #endif | ||
| 1761 | ) { | ||
| 1731 | uint8_t *p, *p_end; | 1762 | uint8_t *p, *p_end; |
| 1732 | 1763 | ||
| 1733 | p = s1->plt->data; | 1764 | p = s1->plt->data; |