| @@ -71,7 +71,7 @@ static const struct { · 70 unmodified lines |
| 71 | 71 | { VT_BOOL, 1, DW_ATE_boolean, "bool:t26=r26;0;255;" }, |
| 72 | 72 | #if LONG_SIZE == 4 |
| 73 | 73 | { VT_VOID, 1, DW_ATE_unsigned_char, "void:t27=27" }, |
| 74 | | −#else |
| 74 | +#else |
| 75 | 75 | /* bitfields use these */ |
| 76 | 76 | { VT_LONG | VT_INT, 8, DW_ATE_signed, "long int:t27=r27;-9223372036854775808;9223372036854775807;" }, |
| 77 | 77 | { VT_LONG | VT_INT | VT_UNSIGNED, 8, DW_ATE_unsigned, "long unsigned int:t28=r28;0;01777777777777777777777;" }, |
| @@ -407,6 +407,8 @@ struct _tccdbg { · 329 unmodified lines |
| 407 | 407 | #define dwarf_info s1->dState->dwarf_info |
| 408 | 408 | #define tcov_data s1->dState->tcov_data |
| 409 | 409 | |
| 410 | +#define FDE_ENCODING (DW_EH_PE_udata4 | DW_EH_PE_signed | DW_EH_PE_pcrel) |
| 411 | + |
| 410 | 412 | /* ------------------------------------------------------------------------- */ |
| 411 | 413 | static void put_stabs(TCCState *s1, const char *str, int type, int other, |
| 412 | 414 | int desc, unsigned long value); |
| @@ -611,7 +613,7 @@ static void dwarf_file(TCCState *s1) · 198 unmodified lines |
| 611 | 613 | } |
| 612 | 614 | if (i == dwarf_line.dir_size) { |
| 613 | 615 | dwarf_line.dir_size++; |
| 614 | | − dwarf_line.dir_table = |
| 616 | + dwarf_line.dir_table = |
| 615 | 617 | (char **) tcc_realloc(dwarf_line.dir_table, |
| 616 | 618 | dwarf_line.dir_size * |
| 617 | 619 | sizeof (char *)); |
| @@ -710,6 +712,299 @@ static void dwarf_sleb128_op (TCCState *s1, long long value) · 92 unmodified lines |
| 710 | 712 | } while (more); |
| 711 | 713 | } |
| 712 | 714 | |
| 715 | +ST_FUNC void tcc_eh_frame_start(TCCState *s1) |
| 716 | +{ |
| 717 | +#if !(defined _WIN32 || defined __APPLE__ || defined TCC_TARGET_ARM || \ |
| 718 | + defined TARGETOS_BSD) |
| 719 | + eh_frame_section = new_section(s1, ".eh_frame", SHT_PROGBITS, SHF_ALLOC); |
| 720 | + |
| 721 | + s1->eh_start = eh_frame_section->data_offset; |
| 722 | + dwarf_data4(eh_frame_section, 0); // length |
| 723 | + dwarf_data4(eh_frame_section, 0); // CIE ID |
| 724 | + dwarf_data1(eh_frame_section, 1); // Version |
| 725 | + dwarf_data1(eh_frame_section, 'z'); // Augmentation String |
| 726 | + dwarf_data1(eh_frame_section, 'R'); |
| 727 | + dwarf_data1(eh_frame_section, 0); |
| 728 | +#if defined TCC_TARGET_I386 |
| 729 | + dwarf_uleb128(eh_frame_section, 1); // code_alignment_factor |
| 730 | + dwarf_sleb128(eh_frame_section, -4); // data_alignment_factor |
| 731 | + dwarf_uleb128(eh_frame_section, 8); // return address column |
| 732 | + dwarf_uleb128(eh_frame_section, 1); // Augmentation len |
| 733 | + dwarf_data1(eh_frame_section, FDE_ENCODING); |
| 734 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa); |
| 735 | + dwarf_uleb128(eh_frame_section, 4); // r4 (esp) |
| 736 | + dwarf_uleb128(eh_frame_section, 4); // ofs 4 |
| 737 | + dwarf_data1(eh_frame_section, DW_CFA_offset + 8); // r8 (eip) |
| 738 | + dwarf_uleb128(eh_frame_section, 1); // cfa-4 |
| 739 | +#elif defined TCC_TARGET_X86_64 |
| 740 | + dwarf_uleb128(eh_frame_section, 1); // code_alignment_factor |
| 741 | + dwarf_sleb128(eh_frame_section, -8); // data_alignment_factor |
| 742 | + dwarf_uleb128(eh_frame_section, 16); // return address column |
| 743 | + dwarf_uleb128(eh_frame_section, 1); // Augmentation len |
| 744 | + dwarf_data1(eh_frame_section, FDE_ENCODING); |
| 745 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa); |
| 746 | + dwarf_uleb128(eh_frame_section, 7); // r7 (rsp) |
| 747 | + dwarf_uleb128(eh_frame_section, 8); // ofs 8 |
| 748 | + dwarf_data1(eh_frame_section, DW_CFA_offset + 16); // r16 (rip) |
| 749 | + dwarf_uleb128(eh_frame_section, 1); // cfa-8 |
| 750 | +#elif defined TCC_TARGET_ARM |
| 751 | + /* TODO: arm must be compiled with: -funwind-tables */ |
| 752 | + /* arm also uses .ARM.extab and .ARM.exidx sections */ |
| 753 | + dwarf_uleb128(eh_frame_section, 2); // code_alignment_factor |
| 754 | + dwarf_sleb128(eh_frame_section, -4); // data_alignment_factor |
| 755 | + dwarf_uleb128(eh_frame_section, 14); // return address column |
| 756 | + dwarf_uleb128(eh_frame_section, 1); // Augmentation len |
| 757 | + dwarf_data1(eh_frame_section, FDE_ENCODING); |
| 758 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa); |
| 759 | + dwarf_uleb128(eh_frame_section, 13); // r13 (sp) |
| 760 | + dwarf_uleb128(eh_frame_section, 0); // ofs 0 |
| 761 | +#elif defined TCC_TARGET_ARM64 |
| 762 | + dwarf_uleb128(eh_frame_section, 4); // code_alignment_factor |
| 763 | + dwarf_sleb128(eh_frame_section, -8); // data_alignment_factor |
| 764 | + dwarf_uleb128(eh_frame_section, 30); // return address column |
| 765 | + dwarf_uleb128(eh_frame_section, 1); // Augmentation len |
| 766 | + dwarf_data1(eh_frame_section, FDE_ENCODING); |
| 767 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa); |
| 768 | + dwarf_uleb128(eh_frame_section, 31); // x31 (sp) |
| 769 | + dwarf_uleb128(eh_frame_section, 0); // ofs 0 |
| 770 | +#elif defined TCC_TARGET_RISCV64 |
| 771 | + eh_frame_section->data[s1->eh_start + 8] = 3; // version = 3 |
| 772 | + dwarf_uleb128(eh_frame_section, 1); // code_alignment_factor |
| 773 | + dwarf_sleb128(eh_frame_section, -4); // data_alignment_factor |
| 774 | + dwarf_uleb128(eh_frame_section, 1); // return address column |
| 775 | + dwarf_uleb128(eh_frame_section, 1); // Augmentation len |
| 776 | + dwarf_data1(eh_frame_section, FDE_ENCODING); |
| 777 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa); |
| 778 | + dwarf_uleb128(eh_frame_section, 2); // r2 (sp) |
| 779 | + dwarf_uleb128(eh_frame_section, 0); // ofs 0 |
| 780 | +#endif |
| 781 | + while ((eh_frame_section->data_offset - s1->eh_start) & 3) |
| 782 | + dwarf_data1(eh_frame_section, DW_CFA_nop); |
| 783 | + write32le(eh_frame_section->data + s1->eh_start, // length |
| 784 | + eh_frame_section->data_offset - s1->eh_start - 4); |
| 785 | +#endif |
| 786 | +} |
| 787 | + |
| 788 | +static void tcc_debug_frame_end(TCCState *s1, int size) |
| 789 | +{ |
| 790 | + int eh_section_sym; |
| 791 | + unsigned long fde_start; |
| 792 | + |
| 793 | + if (!eh_frame_section) |
| 794 | + return; |
| 795 | + eh_section_sym = dwarf_get_section_sym(text_section); |
| 796 | + fde_start = eh_frame_section->data_offset; |
| 797 | + dwarf_data4(eh_frame_section, 0); // length |
| 798 | + dwarf_data4(eh_frame_section, |
| 799 | + fde_start - s1->eh_start + 4); // CIE Pointer |
| 800 | +#if TCC_TARGET_I386 |
| 801 | + dwarf_reloc(eh_frame_section, eh_section_sym, R_386_PC32); |
| 802 | +#elif defined TCC_TARGET_X86_64 |
| 803 | + dwarf_reloc(eh_frame_section, eh_section_sym, R_X86_64_PC32); |
| 804 | +#elif defined TCC_TARGET_ARM |
| 805 | + dwarf_reloc(eh_frame_section, eh_section_sym, R_ARM_REL32); |
| 806 | +#elif defined TCC_TARGET_ARM64 |
| 807 | + dwarf_reloc(eh_frame_section, eh_section_sym, R_AARCH64_PREL32); |
| 808 | +#elif defined TCC_TARGET_RISCV64 |
| 809 | + dwarf_reloc(eh_frame_section, eh_section_sym, R_RISCV_32_PCREL); |
| 810 | +#endif |
| 811 | + dwarf_data4(eh_frame_section, func_ind); // PC Begin |
| 812 | + dwarf_data4(eh_frame_section, size); // PC Range |
| 813 | + dwarf_data1(eh_frame_section, 0); // Augmentation Length |
| 814 | +#if TCC_TARGET_I386 |
| 815 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 1); |
| 816 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset); |
| 817 | + dwarf_uleb128(eh_frame_section, 8); |
| 818 | + dwarf_data1(eh_frame_section, DW_CFA_offset + 5); // r5 (ebp) |
| 819 | + dwarf_uleb128(eh_frame_section, 2); // cfa-8 |
| 820 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 2); |
| 821 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa_register); |
| 822 | + dwarf_uleb128(eh_frame_section, 5); // r5 (ebp) |
| 823 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc4); |
| 824 | + dwarf_data4(eh_frame_section, size - 5); |
| 825 | + dwarf_data1(eh_frame_section, DW_CFA_restore + 5); // r5 (ebp) |
| 826 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa); |
| 827 | + dwarf_uleb128(eh_frame_section, 4); // r4 (esp) |
| 828 | + dwarf_uleb128(eh_frame_section, 4); // ofs 4 |
| 829 | +#elif defined TCC_TARGET_X86_64 |
| 830 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 1); |
| 831 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset); |
| 832 | + dwarf_uleb128(eh_frame_section, 16); |
| 833 | + dwarf_data1(eh_frame_section, DW_CFA_offset + 6); // r6 (rbp) |
| 834 | + dwarf_uleb128(eh_frame_section, 2); // cfa-16 |
| 835 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 3); |
| 836 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa_register); |
| 837 | + dwarf_uleb128(eh_frame_section, 6); // r6 (rbp) |
| 838 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc4); |
| 839 | + dwarf_data4(eh_frame_section, size - 5); |
| 840 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa); |
| 841 | + dwarf_uleb128(eh_frame_section, 7); // r7 (rsp) |
| 842 | + dwarf_uleb128(eh_frame_section, 8); // ofs 8 |
| 843 | +#elif defined TCC_TARGET_ARM |
| 844 | + /* TODO */ |
| 845 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 2); |
| 846 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset); |
| 847 | + dwarf_uleb128(eh_frame_section, 8); |
| 848 | + dwarf_data1(eh_frame_section, DW_CFA_offset + 14); // r14 (lr) |
| 849 | + dwarf_uleb128(eh_frame_section, 1); |
| 850 | + dwarf_data1(eh_frame_section, DW_CFA_offset + 11); // r11 (fp) |
| 851 | + dwarf_uleb128(eh_frame_section, 2); |
| 852 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc4); |
| 853 | + dwarf_data4(eh_frame_section, size / 2 - 5); |
| 854 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa_register); |
| 855 | + dwarf_uleb128(eh_frame_section, 11); // r11 (fp) |
| 856 | +#elif defined TCC_TARGET_ARM64 |
| 857 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 1); |
| 858 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset); |
| 859 | + dwarf_uleb128(eh_frame_section, 224); |
| 860 | + dwarf_data1(eh_frame_section, DW_CFA_offset + 29); // x29 (fp) |
| 861 | + dwarf_uleb128(eh_frame_section, 28); // cfa-224 |
| 862 | + dwarf_data1(eh_frame_section, DW_CFA_offset + 30); // x30 (lr) |
| 863 | + dwarf_uleb128(eh_frame_section, 27); // cfa-216 |
| 864 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 3); |
| 865 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset); |
| 866 | + dwarf_uleb128(eh_frame_section, 224 + ((-loc + 15) & ~15)); |
| 867 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc4); |
| 868 | + dwarf_data4(eh_frame_section, size / 4 - 5); |
| 869 | + dwarf_data1(eh_frame_section, DW_CFA_restore + 30); // x30 (lr) |
| 870 | + dwarf_data1(eh_frame_section, DW_CFA_restore + 29); // x29 (fp) |
| 871 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset); |
| 872 | + dwarf_uleb128(eh_frame_section, 0); |
| 873 | +#elif defined TCC_TARGET_RISCV64 |
| 874 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 4); |
| 875 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset); |
| 876 | + dwarf_uleb128(eh_frame_section, 16); // ofs 16 |
| 877 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 8); |
| 878 | + dwarf_data1(eh_frame_section, DW_CFA_offset + 1); // r1 (ra, lr) |
| 879 | + dwarf_uleb128(eh_frame_section, 2); // cfa-8 |
| 880 | + dwarf_data1(eh_frame_section, DW_CFA_offset + 8); // r8 (s0, fp) |
| 881 | + dwarf_uleb128(eh_frame_section, 4); // cfa-16 |
| 882 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 8); |
| 883 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa); |
| 884 | + dwarf_uleb128(eh_frame_section, 8); // r8 (s0, fp) |
| 885 | + dwarf_uleb128(eh_frame_section, 0); // ofs 0 |
| 886 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc4); |
| 887 | + while (size >= 4 && |
| 888 | + read32le(cur_text_section->data + func_ind + size - 4) != 0x00008067) |
| 889 | + size -= 4; |
| 890 | + dwarf_data4(eh_frame_section, size - 36); |
| 891 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa); |
| 892 | + dwarf_uleb128(eh_frame_section, 2); // r2 (r2, sp) |
| 893 | + dwarf_uleb128(eh_frame_section, 16); // ofs 16 |
| 894 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 4); |
| 895 | + dwarf_data1(eh_frame_section, DW_CFA_restore + 1); // r1 (lr) |
| 896 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 4); |
| 897 | + dwarf_data1(eh_frame_section, DW_CFA_restore + 8); // r8 (s0, fp) |
| 898 | + dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 4); |
| 899 | + dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset); |
| 900 | + dwarf_uleb128(eh_frame_section, 0); // ofs 0 |
| 901 | +#endif |
| 902 | + while ((eh_frame_section->data_offset - fde_start) & 3) |
| 903 | + dwarf_data1(eh_frame_section, DW_CFA_nop); |
| 904 | + write32le(eh_frame_section->data + fde_start, // length |
| 905 | + eh_frame_section->data_offset - fde_start - 4); |
| 906 | +} |
| 907 | + |
| 908 | +ST_FUNC void tcc_eh_frame_end(TCCState *s1) |
| 909 | +{ |
| 910 | + if (!eh_frame_section) |
| 911 | + return; |
| 912 | + dwarf_data4(eh_frame_section, 0); |
| 913 | +} |
| 914 | + |
| 915 | +struct eh_search_table { |
| 916 | + uint32_t pc_offset; |
| 917 | + uint32_t fde_offset; |
| 918 | +}; |
| 919 | + |
| 920 | +static int sort_eh_table(const void *a, const void *b) |
| 921 | +{ |
| 922 | + uint32_t pc1 = ((const struct eh_search_table *)a)->pc_offset; |
| 923 | + uint32_t pc2 = ((const struct eh_search_table *)b)->pc_offset; |
| 924 | + |
| 925 | + return pc1 < pc2 ? -1 : pc1 > pc2 ? 1 : 0; |
| 926 | +} |
| 927 | + |
| 928 | +ST_FUNC void tcc_eh_frame_hdr(TCCState *s1, int final) |
| 929 | +{ |
| 930 | + int count = 0, offset; |
| 931 | + unsigned long count_offset, tab_offset; |
| 932 | + unsigned char *ln, *end; |
| 933 | + unsigned int last_cie_offset = 0xffffffff; |
| 934 | + |
| 935 | + if (!eh_frame_section || !eh_frame_section->data_offset) |
| 936 | + return; |
| 937 | + if (final && !eh_frame_hdr_section) |
| 938 | + return; |
| 939 | + if (final == 0) |
| 940 | + eh_frame_hdr_section = |
| 941 | + new_section(s1, ".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC); |
| 942 | + eh_frame_hdr_section->data_offset = 0; |
| 943 | + dwarf_data1(eh_frame_hdr_section, 1); // Version |
| 944 | + // Pointer Encoding Format |
| 945 | + dwarf_data1(eh_frame_hdr_section, DW_EH_PE_sdata4 | DW_EH_PE_pcrel); |
| 946 | + // Count Encoding Format |
| 947 | + dwarf_data1(eh_frame_hdr_section, DW_EH_PE_udata4 | DW_EH_PE_absptr); |
| 948 | + // Table Encoding Format |
| 949 | + dwarf_data1(eh_frame_hdr_section, DW_EH_PE_sdata4 | DW_EH_PE_datarel); |
| 950 | + offset = eh_frame_section->sh_addr - |
| 951 | + eh_frame_hdr_section->sh_addr - |
| 952 | + eh_frame_hdr_section->data_offset; |
| 953 | + dwarf_data4(eh_frame_hdr_section, offset); // eh_frame_ptr |
| 954 | + // Count |
| 955 | + count_offset = eh_frame_hdr_section->data_offset; |
| 956 | + dwarf_data4(eh_frame_hdr_section, 0); |
| 957 | + tab_offset = eh_frame_hdr_section->data_offset; |
| 958 | + ln = eh_frame_section->data; |
| 959 | + end = eh_frame_section->data + eh_frame_section->data_offset; |
| 960 | + while (ln < end) { |
| 961 | + unsigned char *fde = ln, *rd = ln; |
| 962 | + unsigned int cie_offset, version, length = dwarf_read_4(rd, end); |
| 963 | + unsigned int pc_offset, fde_offset; |
| 964 | + |
| 965 | + if (length == 0) |
| 966 | + goto next; |
| 967 | + cie_offset = dwarf_read_4(rd, end); |
| 968 | + if (cie_offset == 0) |
| 969 | + goto next; |
| 970 | + if (cie_offset != last_cie_offset) { |
| 971 | + unsigned char *cie = rd - cie_offset + 4; |
| 972 | + |
| 973 | + if (cie < eh_frame_section->data) |
| 974 | + goto next; |
| 975 | + version = dwarf_read_1(cie, end); |
| 976 | + if ((version == 1 || version == 3) && |
| 977 | + dwarf_read_1(cie, end) == 'z' && // Augmentation String |
| 978 | + dwarf_read_1(cie, end) == 'R' && |
| 979 | + dwarf_read_1(cie, end) == 0) { |
| 980 | + dwarf_read_uleb128(&cie, end); // code_alignment_factor |
| 981 | + dwarf_read_sleb128(&cie, end); // data_alignment_factor |
| 982 | + dwarf_read_1(cie, end); // return address column |
| 983 | + if (dwarf_read_uleb128(&cie, end) == 1 && |
| 984 | + dwarf_read_1(cie, end) == FDE_ENCODING) { |
| 985 | + last_cie_offset = cie_offset; |
| 986 | + } |
| 987 | + else |
| 988 | + goto next; |
| 989 | + } |
| 990 | + else |
| 991 | + goto next; |
| 992 | + } |
| 993 | + count++; |
| 994 | + fde_offset = eh_frame_section->sh_addr + |
| 995 | + (fde - eh_frame_section->data) - |
| 996 | + eh_frame_hdr_section->sh_addr; |
| 997 | + pc_offset = dwarf_read_4(rd, end) + fde_offset + 8; |
| 998 | + dwarf_data4(eh_frame_hdr_section, pc_offset); |
| 999 | + dwarf_data4(eh_frame_hdr_section, fde_offset); |
| 1000 | +next: |
| 1001 | + ln += length + 4; |
| 1002 | + } |
| 1003 | + add32le(eh_frame_hdr_section->data + count_offset, count); |
| 1004 | + qsort(eh_frame_hdr_section->data + tab_offset, count, |
| 1005 | + sizeof(struct eh_search_table), sort_eh_table); |
| 1006 | +} |
| 1007 | + |
| 713 | 1008 | /* start of translation unit info */ |
| 714 | 1009 | ST_FUNC void tcc_debug_start(TCCState *s1) |
| 715 | 1010 | { |
| @@ -1875,6 +2170,7 @@ ST_FUNC void tcc_debug_funcend(TCCState *s1, int size) · 1159 unmodified lines |
| 1875 | 2170 | /* lldb does not like function end and next function start at same pc */ |
| 1876 | 2171 | int min_instr_len; |
| 1877 | 2172 | |
| 2173 | + tcc_debug_frame_end(s1, size); |
| 1878 | 2174 | if (!s1->do_debug) |
| 1879 | 2175 | return; |
| 1880 | 2176 | min_instr_len = dwarf_line.last_pc == ind ? 0 : DWARF_MIN_INSTR_LEN; |