- 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 | 22 | #define ElfW_Rel ElfW(Rela) |
| 23 | 23 | #define SHT_RELX SHT_RELA |
| 24 | 24 | #define REL_SECTION_FMT ".rela%s" |
| 25 | +/* x86-64 requires PLT for DLLs */ | |
| 26 | +#define TCC_OUTPUT_DLL_WITH_PLT | |
| 25 | 27 | #else |
| 26 | 28 | #define ElfW_Rel ElfW(Rel) |
| 27 | 29 | #define SHT_RELX SHT_REL |
| 28 | 30 | #define REL_SECTION_FMT ".rel%s" |
| 29 | 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 | 36 | static int put_elf_str(Section *s, const char *sym) |
| 32 | 37 | { |
| 33 | 38 | int offset, len; |
| @@ -902,11 +907,15 @@ static void put_got_entry(TCCState *s1, · 868 unmodified lines | ||
| 902 | 907 | uint8_t *p; |
| 903 | 908 | int modrm; |
| 904 | 909 | |
| 910 | +#if defined(TCC_OUTPUT_DLL_WITH_PLT) | |
| 911 | + modrm = 0x25; | |
| 912 | +#else | |
| 905 | 913 | /* if we build a DLL, we add a %ebx offset */ |
| 906 | 914 | if (s1->output_type == TCC_OUTPUT_DLL) |
| 907 | 915 | modrm = 0xa3; |
| 908 | 916 | else |
| 909 | 917 | modrm = 0x25; |
| 918 | +#endif | |
| 910 | 919 | |
| 911 | 920 | /* add a PLT entry */ |
| 912 | 921 | plt = s1->plt; |
| @@ -932,7 +941,9 @@ static void put_got_entry(TCCState *s1, · 19 unmodified lines | ||
| 932 | 941 | |
| 933 | 942 | /* the symbol is modified so that it will be relocated to |
| 934 | 943 | the PLT */ |
| 944 | +#if !defined(TCC_OUTPUT_DLL_WITH_PLT) | |
| 935 | 945 | if (s1->output_type == TCC_OUTPUT_EXE) |
| 946 | +#endif | |
| 936 | 947 | offset = plt->data_offset - 16; |
| 937 | 948 | } |
| 938 | 949 | #elif defined(TCC_TARGET_ARM) |
| @@ -1468,13 +1479,29 @@ int elf_output_file(TCCState *s1, const char *filename) · 529 unmodified lines | ||
| 1468 | 1479 | sym < sym_end; |
| 1469 | 1480 | sym++) { |
| 1470 | 1481 | if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { |
| 1471 | − name = symtab_section->link->data + sym->st_name; | |
| 1472 | − index = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, | |
| 1473 | − sym->st_info, 0, | |
| 1474 | − sym->st_shndx, name); | |
| 1475 | − s1->symtab_to_dynsym[sym - | |
| 1476 | − (ElfW(Sym) *)symtab_section->data] = | |
| 1477 | − index; | |
| 1482 | +#if defined(TCC_OUTPUT_DLL_WITH_PLT) | |
| 1483 | + if (ELFW(ST_TYPE)(sym->st_info) == STT_FUNC && | |
| 1484 | + sym->st_shndx == SHN_UNDEF) { | |
| 1485 | + put_got_entry(s1, R_JMP_SLOT, sym->st_size, | |
| 1486 | + sym->st_info, | |
| 1487 | + sym - (ElfW(Sym) *)symtab_section->data); | |
| 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 | 1754 | put32(s1->got->data, dynamic->sh_addr); |
| 1728 | 1755 | |
| 1729 | 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 | 1762 | uint8_t *p, *p_end; |
| 1732 | 1763 | |
| 1733 | 1764 | p = s1->plt->data; |