this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #1461 from darlinghq/coredump_rework

Rework `darling-coredump` To Avoid Hardcoding x86/x64 Logic

authored by

CuriousTommy and committed by
GitHub
1aa1d3cd 34351655

+88 -48
+88 -48
src/hosttools/src/coredump/main.c
··· 15 15 16 16 #include <linux/time_types.h> 17 17 18 - #if __x86_64__ 19 18 #include <coredump/x86_64.h> 20 - #else 21 - #error Not implemented 22 - #endif 23 19 24 20 #include <darling-config.h> 25 21 ··· 238 234 return (const union nt_file_entry*)((const char*)header + nt_file_header_size(cprm) + (nt_file_entry_size(cprm) * index)); 239 235 }; 240 236 237 + static uint16_t get_elf_machine_type(const struct coredump_params* cprm) { 238 + return cprm->universal_header->e_machine; 239 + } 240 + 241 241 // first tries to open the file directly, then tries to open the file in the lower layer of the overlay 242 242 // (because if we're outside the container, the overlay won't be mounted, but the core dump paths would refer to it) 243 243 static int open_file(struct coredump_params* cprm, const char* filename, size_t filename_length) { ··· 342 342 return 1; 343 343 } 344 344 345 - switch (cprm.universal_header->e_machine) { 345 + switch (get_elf_machine_type(&cprm)) { 346 346 case EM_X86_64: 347 347 case EM_386: 348 348 cprm.input_header = cprm.input_corefile_mapping; ··· 681 681 }; 682 682 683 683 static 684 - void fill_thread_state32(x86_thread_state32_t* state, const struct thread_info* info) 684 + void fill_x86_thread_state32(x86_thread_state32_t* state, const struct thread_info* info) 685 685 { 686 686 state->eax = info->prstatus->elf32.general_registers.i386.eax; 687 687 state->ebx = info->prstatus->elf32.general_registers.i386.ebx; ··· 702 702 } 703 703 704 704 static 705 - void fill_float_state32(x86_float_state32_t* state, const struct thread_info* info) 705 + void fill_x86_float_state32(x86_float_state32_t* state, const struct thread_info* info) 706 706 { 707 707 // TODO 708 708 memset(state, 0, sizeof(*state)); 709 709 } 710 710 711 711 static 712 - void fill_thread_state64(x86_thread_state64_t* state, const struct thread_info* info) 712 + void fill_x86_thread_state64(x86_thread_state64_t* state, const struct thread_info* info) 713 713 { 714 714 state->rax = info->prstatus->elf64.general_registers.x86_64.ax; 715 715 state->rbx = info->prstatus->elf64.general_registers.x86_64.bx; ··· 735 735 } 736 736 737 737 static 738 - void fill_float_state64(x86_float_state64_t* state, const struct thread_info* info) 738 + void fill_x86_float_state64(x86_float_state64_t* state, const struct thread_info* info) 739 739 { 740 740 // TODO 741 741 memset(state, 0, sizeof(*state)); ··· 744 744 static 745 745 bool macho_dump_headers32(struct coredump_params* cprm) 746 746 { 747 + uint16_t machine_type = get_elf_machine_type(cprm); 748 + 747 749 // Count memory segments and threads 748 750 unsigned int segs = cprm->vm_area_count; 749 751 unsigned int threads = cprm->thread_info_count; ··· 760 762 } 761 763 762 764 mh.magic = MH_MAGIC; 763 - #ifdef __x86_64__ 764 - mh.cputype = CPU_TYPE_X86; 765 - mh.cpusubtype = CPU_SUBTYPE_X86_ALL; 766 - #else 767 - #warning Missing code for this arch 768 - #endif 769 765 mh.filetype = MH_CORE; 770 766 mh.ncmds = segs + threads; 771 767 772 - const int statesize = sizeof(x86_thread_state32_t) + (DUMP_FLOAT_STATE ? sizeof(x86_float_state32_t) : 0) + sizeof(struct thread_flavor) * (DUMP_FLOAT_STATE ? 2 : 1); 768 + int statesize; 769 + switch (machine_type) 770 + { 771 + case EM_386: 772 + mh.cputype = CPU_TYPE_X86; 773 + mh.cpusubtype = CPU_SUBTYPE_X86_ALL; 774 + 775 + statesize = sizeof(struct thread_flavor) + sizeof(x86_thread_state32_t); 776 + statesize += (DUMP_FLOAT_STATE ? sizeof(struct thread_flavor) + sizeof(x86_float_state32_t) : 0); 777 + break; 778 + 779 + default: 780 + // Missing code for this arch 781 + abort(); 782 + } 783 + 773 784 mh.sizeofcmds = segs * sizeof(struct segment_command) + threads * (sizeof(struct thread_command) + statesize); 774 785 mh.flags = 0; 775 786 ··· 837 848 for (size_t i = 0; i < cprm->thread_info_count; ++i) { 838 849 const struct thread_info* thread_info = &cprm->thread_infos[i]; 839 850 struct thread_command* tc = (struct thread_command*) buffer; 840 - struct thread_flavor* tf = (struct thread_flavor*)(tc+1); 851 + struct thread_flavor* tf; 841 852 842 853 tc->cmd = LC_THREAD; 843 854 tc->cmdsize = memsize; 844 855 845 - // General registers 846 - tf->flavor = x86_THREAD_STATE32; 847 - tf->count = x86_THREAD_STATE32_COUNT; 856 + switch (machine_type) 857 + { 858 + case EM_386: 859 + // General registers 860 + tf = (struct thread_flavor*)(tc+1); 861 + tf->flavor = x86_THREAD_STATE32; 862 + tf->count = x86_THREAD_STATE32_COUNT; 863 + fill_x86_thread_state32((x86_thread_state32_t*)tf->state, thread_info); 848 864 849 - fill_thread_state32((x86_thread_state32_t*)tf->state, thread_info); 865 + // Float registers 866 + if (DUMP_FLOAT_STATE) { 867 + tf = (struct thread_flavor*) (tf->state + sizeof(x86_thread_state32_t)); 868 + tf->flavor = x86_FLOAT_STATE32; 869 + tf->count = x86_FLOAT_STATE32_COUNT; 870 + fill_x86_float_state32((x86_float_state32_t*)tf->state, thread_info); 871 + } 850 872 851 - #if DUMP_FLOAT_STATE 852 - // Float registers 853 - tf = (struct thread_flavor*) (tf->state + sizeof(x86_thread_state32_t)); 854 - tf->flavor = x86_FLOAT_STATE32; 855 - tf->count = x86_FLOAT_STATE32_COUNT; 873 + break; 856 874 857 - fill_float_state32((x86_float_state32_t*)tf->state, thread_info); 858 - #endif 875 + default: 876 + // Missing code for this arch 877 + abort(); 878 + } 859 879 860 880 if (!dump_emit(cprm, buffer, memsize)) 861 881 { ··· 873 893 static 874 894 bool macho_dump_headers64(struct coredump_params* cprm) 875 895 { 896 + uint16_t machine_type = get_elf_machine_type(cprm); 897 + 876 898 // Count memory segments and threads 877 899 unsigned int segs = cprm->vm_area_count; 878 900 unsigned int threads = cprm->thread_info_count; ··· 889 911 } 890 912 891 913 mh.magic = MH_MAGIC_64; 892 - #ifdef __x86_64__ 893 - mh.cputype = CPU_TYPE_X86_64; 894 - mh.cpusubtype = CPU_SUBTYPE_X86_64_ALL; 895 - #else 896 - #warning Missing code for this arch 897 - #endif 898 914 mh.filetype = MH_CORE; 899 915 mh.ncmds = segs + threads; 900 916 901 - const int statesize = sizeof(x86_thread_state64_t) + (DUMP_FLOAT_STATE ? sizeof(x86_float_state64_t) : 0) + sizeof(struct thread_flavor) * (DUMP_FLOAT_STATE ? 2 : 1); 917 + int statesize; 918 + switch (machine_type) 919 + { 920 + case EM_X86_64: 921 + mh.cputype = CPU_TYPE_X86_64; 922 + mh.cpusubtype = CPU_SUBTYPE_X86_64_ALL; 923 + 924 + statesize = sizeof(struct thread_flavor) + sizeof(x86_thread_state64_t); 925 + statesize += (DUMP_FLOAT_STATE ? sizeof(struct thread_flavor) + sizeof(x86_float_state64_t) : 0); 926 + break; 927 + 928 + default: 929 + // Missing code for this arch 930 + abort(); 931 + } 932 + 902 933 mh.sizeofcmds = segs * sizeof(struct segment_command_64) + threads * (sizeof(struct thread_command) + statesize); 903 934 mh.flags = 0; 904 935 mh.reserved = 0; ··· 965 996 for (size_t i = 0; i < cprm->thread_info_count; ++i) { 966 997 const struct thread_info* thread_info = &cprm->thread_infos[i]; 967 998 struct thread_command* tc = (struct thread_command*) buffer; 968 - struct thread_flavor* tf = (struct thread_flavor*)(tc+1); 999 + struct thread_flavor* tf; 969 1000 970 1001 tc->cmd = LC_THREAD; 971 1002 tc->cmdsize = memsize; 972 1003 973 - // General registers 974 - tf->flavor = x86_THREAD_STATE64; 975 - tf->count = x86_THREAD_STATE64_COUNT; 1004 + switch (machine_type) 1005 + { 1006 + case EM_X86_64: 1007 + // General registers 1008 + tf = (struct thread_flavor*)(tc+1); 1009 + tf->flavor = x86_THREAD_STATE64; 1010 + tf->count = x86_THREAD_STATE64_COUNT; 1011 + fill_x86_thread_state64((x86_thread_state64_t*)tf->state, thread_info); 976 1012 977 - fill_thread_state64((x86_thread_state64_t*)tf->state, thread_info); 978 - 979 - #if DUMP_FLOAT_STATE 980 - // Float registers 981 - tf = (struct thread_flavor*) (tf->state + sizeof(x86_thread_state64_t)); 982 - tf->flavor = x86_FLOAT_STATE64; 983 - tf->count = x86_FLOAT_STATE64_COUNT; 1013 + // Float registers 1014 + if (DUMP_FLOAT_STATE) { 1015 + tf = (struct thread_flavor*) (tf->state + sizeof(x86_thread_state64_t)); 1016 + tf->flavor = x86_FLOAT_STATE64; 1017 + tf->count = x86_FLOAT_STATE64_COUNT; 1018 + fill_x86_float_state64((x86_float_state64_t*)tf->state, thread_info); 1019 + } 984 1020 985 - fill_float_state64((x86_float_state64_t*)tf->state, thread_info); 986 - #endif 1021 + break; 1022 + 1023 + default: 1024 + // Missing code for this arch 1025 + abort(); 1026 + } 987 1027 988 1028 if (!dump_emit(cprm, buffer, memsize)) 989 1029 {