this repo has no description
1
fork

Configure Feed

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

Add A Union Between Elf32/Elf64 Structs. Create Universal Elf Header Struct.

Also did some minor reorganization.

Thomas A bc76af48 72d78ef5

+54 -31
+2
src/hosttools/CMakeLists.txt
··· 7 7 ) 8 8 9 9 target_include_directories(darling-coredump PRIVATE 10 + include 11 + 10 12 # TEMPORARY 11 13 # this set of includes should probably be moved out because we probably want to be able to use Mach-O headers 12 14 # in non-Darling compilation environments more often. by non-Darling, i mean when compiling directly for Linux
+52 -31
src/hosttools/src/coredump/main.c
··· 15 15 #include <linux/time_types.h> 16 16 17 17 #if __x86_64__ 18 - #include "x86_64.h" 18 + #include <coredump/x86_64.h> 19 19 #else 20 20 #error Not implemented 21 21 #endif ··· 71 71 struct nt_prstatus* prstatus; 72 72 }; 73 73 74 + struct elf_universal_header { 75 + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ 76 + /* Elf32_Half/Elf64_Half -> uint16_t */ 77 + uint16_t e_type; /* Object file type */ 78 + /* Elf32_Half/Elf64_Half -> uint16_t */ 79 + uint16_t e_machine; /* Architecture */ 80 + /* Elf32_Word/Elf64_Word -> uint32_t */ 81 + uint32_t e_version; /* Object file version */ 82 + }; 83 + 74 84 struct coredump_params { 75 85 int input_corefile; 76 86 int output_corefile; 77 87 size_t input_corefile_size; 78 88 const void* input_corefile_mapping; 79 - const Elf64_Ehdr* input_header; 80 - const Elf64_Phdr* input_program_headers; 81 - const Elf64_Phdr* input_program_headers_end; 82 - const Elf64_Nhdr* input_notes; 89 + struct elf_universal_header *universal_header; 90 + union { 91 + const Elf32_Ehdr* elf32; 92 + const Elf64_Ehdr* elf64; 93 + } input_header; 94 + union { 95 + const Elf32_Phdr* elf32; 96 + const Elf64_Phdr* elf64; 97 + } input_program_headers; 98 + union { 99 + const Elf32_Phdr* elf32; 100 + const Elf64_Phdr* elf64; 101 + } input_program_headers_end; 102 + union { 103 + const Elf32_Nhdr* elf32; 104 + const Elf64_Nhdr* elf64; 105 + } input_notes; 83 106 size_t input_notes_size; 84 107 struct vm_area* vm_areas; 85 108 size_t vm_area_count; ··· 194 217 return 1; 195 218 } 196 219 197 - cprm.input_header = cprm.input_corefile_mapping; 220 + cprm.universal_header = cprm.input_corefile_mapping; 198 221 199 222 if ( 200 - cprm.input_header->e_ident[EI_MAG0] != ELFMAG0 || 201 - cprm.input_header->e_ident[EI_MAG1] != ELFMAG1 || 202 - cprm.input_header->e_ident[EI_MAG2] != ELFMAG2 || 203 - cprm.input_header->e_ident[EI_MAG3] != ELFMAG3 || 204 - cprm.input_header->e_ident[EI_CLASS] != ELFCLASS64 || 205 - cprm.input_header->e_ident[EI_VERSION] != EV_CURRENT || 206 - cprm.input_header->e_version != EV_CURRENT || 207 - cprm.input_header->e_type != ET_CORE 223 + cprm.universal_header->e_ident[EI_MAG0] != ELFMAG0 || 224 + cprm.universal_header->e_ident[EI_MAG1] != ELFMAG1 || 225 + cprm.universal_header->e_ident[EI_MAG2] != ELFMAG2 || 226 + cprm.universal_header->e_ident[EI_MAG3] != ELFMAG3 || 227 + cprm.universal_header->e_ident[EI_CLASS] != ELFCLASS64 || 228 + cprm.universal_header->e_ident[EI_VERSION] != EV_CURRENT || 229 + cprm.universal_header->e_version != EV_CURRENT || 230 + cprm.universal_header->e_type != ET_CORE 208 231 ) { 209 232 fprintf(stderr, "Input file is not a valid corefile\n"); 210 233 return 1; 211 234 } 212 235 213 - #if __x86_64__ 214 - if (cprm.input_header->e_machine != EM_X86_64) { 215 - fprintf(stderr, "Input file is not a valid x86_64 corefile\n"); 236 + if (cprm.universal_header->e_machine == EM_X86_64) { 237 + 238 + } else { 239 + fprintf(stderr, "Unexpected e_machine (%d) detected, aborting.\n", cprm.universal_header->e_machine); 216 240 return 1; 217 241 } 218 - #else 219 - #error Not implemented! 220 - #endif 221 242 222 - cprm.input_program_headers = (const void*)((const char*)cprm.input_corefile_mapping + cprm.input_header->e_phoff); 223 - cprm.input_program_headers_end = (const Elf64_Phdr*)((const char*)cprm.input_program_headers + (cprm.input_header->e_phentsize * cprm.input_header->e_phnum)); 243 + cprm.input_program_headers.elf64 = (const void*)((const char*)cprm.input_corefile_mapping + cprm.input_header.elf64->e_phoff); 244 + cprm.input_program_headers_end.elf64 = (const Elf64_Phdr*)((const char*)cprm.input_program_headers.elf64 + (cprm.input_header.elf64->e_phentsize * cprm.input_header.elf64->e_phnum)); 224 245 225 246 // first, count how many VM areas we have 226 - for (const Elf64_Phdr* program_header = cprm.input_program_headers; program_header < cprm.input_program_headers_end; program_header = (const Elf64_Phdr*)((const char*)program_header + cprm.input_header->e_phentsize)) { 247 + for (const Elf64_Phdr* program_header = cprm.input_program_headers.elf64; program_header < cprm.input_program_headers_end.elf64; program_header = (const Elf64_Phdr*)((const char*)program_header + cprm.input_header.elf64->e_phentsize)) { 227 248 if (program_header->p_type == PT_LOAD) { 228 249 ++cprm.vm_area_count; 229 250 } else if (program_header->p_type == PT_NOTE) { 230 251 // while we're at it, also load the NOTE segment 231 252 232 253 // XXX: ignoring it is probably not the best choice 233 - if (cprm.input_notes) { 254 + if (cprm.input_notes.elf64) { 234 255 printf("warning: ignoring extra PT_NOTE segment\n"); 235 256 continue; 236 257 } 237 258 238 - cprm.input_notes = (const void*)((const char*)cprm.input_corefile_mapping + program_header->p_offset); 259 + cprm.input_notes.elf64 = (const void*)((const char*)cprm.input_corefile_mapping + program_header->p_offset); 239 260 cprm.input_notes_size = program_header->p_filesz; 240 261 } 241 262 } 242 263 243 - if (!cprm.input_notes) { 264 + if (!cprm.input_notes.elf64) { 244 265 fprintf(stderr, "Input corefile does not contain PT_NOTE segment\n"); 245 266 return 1; 246 267 } 247 268 248 - for (const Elf64_Nhdr* note_header = cprm.input_notes; note_header < (const Elf64_Nhdr*)((const char*)cprm.input_notes + cprm.input_notes_size); note_header = find_next_note(note_header)) { 269 + for (const Elf64_Nhdr* note_header = cprm.input_notes.elf64; note_header < (const Elf64_Nhdr*)((const char*)cprm.input_notes.elf64 + cprm.input_notes_size); note_header = find_next_note(note_header)) { 249 270 if (note_header->n_type == NT_FILE) { 250 271 // allocate a copy for alignment purposes 251 272 cprm.nt_file = malloc(note_header->n_descsz); ··· 285 306 } 286 307 287 308 size_t thread_info_index = 0; 288 - for (const Elf64_Nhdr* note_header = cprm.input_notes; note_header < (const Elf64_Nhdr*)((const char*)cprm.input_notes + cprm.input_notes_size); note_header = find_next_note(note_header)) { 309 + for (const Elf64_Nhdr* note_header = cprm.input_notes.elf64; note_header < (const Elf64_Nhdr*)((const char*)cprm.input_notes.elf64 + cprm.input_notes_size); note_header = find_next_note(note_header)) { 289 310 if (note_header->n_type == NT_PRSTATUS) { 290 311 // allocate a copy for alignment purposes 291 312 struct nt_prstatus* prstatus = malloc(note_header->n_descsz); ··· 328 349 } 329 350 } 330 351 331 - for (const Elf64_Phdr* program_header = cprm.input_program_headers; program_header < cprm.input_program_headers_end; program_header = (const Elf64_Phdr*)((const char*)program_header + cprm.input_header->e_phentsize)) { 352 + for (const Elf64_Phdr* program_header = cprm.input_program_headers.elf64; program_header < cprm.input_program_headers_end.elf64; program_header = (const Elf64_Phdr*)((const char*)program_header + cprm.input_header.elf64->e_phentsize)) { 332 353 if (program_header->p_type != PT_LOAD) { 333 354 continue; 334 355 } ··· 359 380 360 381 // now load up the VM area array 361 382 size_t vm_area_index = 0; 362 - for (const Elf64_Phdr* program_header = cprm.input_program_headers; program_header < cprm.input_program_headers_end; program_header = (const Elf64_Phdr*)((const char*)program_header + cprm.input_header->e_phentsize)) { 383 + for (const Elf64_Phdr* program_header = cprm.input_program_headers.elf64; program_header < cprm.input_program_headers_end.elf64; program_header = (const Elf64_Phdr*)((const char*)program_header + cprm.input_header.elf64->e_phentsize)) { 363 384 if (program_header->p_type == PT_LOAD) { 364 385 struct vm_area* vm_area = &cprm.vm_areas[vm_area_index++]; 365 386 ··· 440 461 const char* filename = cprm.nt_file_filenames[i]; 441 462 bool found = false; 442 463 443 - for (const Elf64_Phdr* program_header = cprm.input_program_headers; program_header < cprm.input_program_headers_end; program_header = (const Elf64_Phdr*)((const char*)program_header + cprm.input_header->e_phentsize)) { 464 + for (const Elf64_Phdr* program_header = cprm.input_program_headers.elf64; program_header < cprm.input_program_headers_end.elf64; program_header = (const Elf64_Phdr*)((const char*)program_header + cprm.input_header.elf64->e_phentsize)) { 444 465 if (program_header->p_type != PT_LOAD) { 445 466 continue; 446 467 }
src/hosttools/src/coredump/x86_64.h src/hosttools/include/coredump/x86_64.h