Changeset View
Changeset View
Standalone View
Standalone View
sys/kern/imgact_elf.c
| Show First 20 Lines • Show All 1,578 Lines • ▼ Show 20 Lines | |||||
| */ | */ | ||||
| typedef void (*segment_callback)(vm_map_entry_t, void *); | typedef void (*segment_callback)(vm_map_entry_t, void *); | ||||
| /* Closure for cb_put_phdr(). */ | /* Closure for cb_put_phdr(). */ | ||||
| struct phdr_closure { | struct phdr_closure { | ||||
| Elf_Phdr *phdr; /* Program header to fill in */ | Elf_Phdr *phdr; /* Program header to fill in */ | ||||
| Elf_Off offset; /* Offset of segment in core file */ | Elf_Off offset; /* Offset of segment in core file */ | ||||
| int numsegs; /* Maximum number of segments */ | |||||
| int nextseg; /* Next segment to fill in */ | |||||
| }; | }; | ||||
| struct note_info { | struct note_info { | ||||
| int type; /* Note type. */ | int type; /* Note type. */ | ||||
| struct regset *regset; /* Register set. */ | struct regset *regset; /* Register set. */ | ||||
| outfunc_t outfunc; /* Output function. */ | outfunc_t outfunc; /* Output function. */ | ||||
| void *outarg; /* Argument for the output function. */ | void *outarg; /* Argument for the output function. */ | ||||
| size_t outsize; /* Output size. */ | size_t outsize; /* Output size. */ | ||||
| ▲ Show 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | #endif | ||||
| if (cdw->init_fn != NULL) { | if (cdw->init_fn != NULL) { | ||||
| error = (*cdw->init_fn)(cdw, ¶ms); | error = (*cdw->init_fn)(cdw, ¶ms); | ||||
| if (error != 0) | if (error != 0) | ||||
| goto done; | goto done; | ||||
| } | } | ||||
| /* | /* | ||||
| * Allocate memory for building the header, fill it up, | * Allocate memory for building the header, fill it up, and write it out | ||||
| * and write it out following the notes. | * following the notes. | ||||
| * | |||||
| * Note that a process sharing our vmspace might be concurrently | |||||
| * mutating the map, in which case we could populate fewer than | |||||
| * seginfo.count headers. Zero the buffer to ensure that unpopulated | |||||
| * headers are still initialized. | |||||
| */ | */ | ||||
| hdr = malloc(hdrsize, M_TEMP, M_WAITOK); | hdr = malloc(hdrsize, M_TEMP, M_WAITOK | M_ZERO); | ||||
markj: This M_ZERO is to make sure we don't write out uninitialized memory if the number of phdrs… | |||||
Not Done Inline ActionsI would put the text into the comment in the code. kib: I would put the text into the comment in the code. | |||||
| error = __elfN(corehdr)(¶ms, seginfo.count, hdr, hdrsize, ¬elst, | error = __elfN(corehdr)(¶ms, seginfo.count, hdr, hdrsize, ¬elst, | ||||
| notesz, flags); | notesz, flags); | ||||
| /* Write the contents of all of the writable segments. */ | /* Write the contents of all of the writable segments. */ | ||||
| if (error == 0) { | if (error == 0) { | ||||
| Elf_Phdr *php; | Elf_Phdr *php; | ||||
| off_t offset; | off_t offset; | ||||
| int i; | int i; | ||||
| Show All 36 Lines | |||||
| * program header entry. | * program header entry. | ||||
| */ | */ | ||||
| static void | static void | ||||
| cb_put_phdr(vm_map_entry_t entry, void *closure) | cb_put_phdr(vm_map_entry_t entry, void *closure) | ||||
| { | { | ||||
| struct phdr_closure *phc = (struct phdr_closure *)closure; | struct phdr_closure *phc = (struct phdr_closure *)closure; | ||||
| Elf_Phdr *phdr = phc->phdr; | Elf_Phdr *phdr = phc->phdr; | ||||
| if (phc->nextseg >= phc->numsegs) { | |||||
| /* Only write as many headers as we have space for. */ | |||||
| return; | |||||
| } | |||||
| phc->offset = round_page(phc->offset); | phc->offset = round_page(phc->offset); | ||||
| phdr->p_type = PT_LOAD; | phdr->p_type = PT_LOAD; | ||||
| phdr->p_offset = phc->offset; | phdr->p_offset = phc->offset; | ||||
| phdr->p_vaddr = entry->start; | phdr->p_vaddr = entry->start; | ||||
| phdr->p_paddr = 0; | phdr->p_paddr = 0; | ||||
| phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; | phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; | ||||
| phdr->p_align = PAGE_SIZE; | phdr->p_align = PAGE_SIZE; | ||||
| phdr->p_flags = __elfN(untrans_prot)(entry->protection); | phdr->p_flags = __elfN(untrans_prot)(entry->protection); | ||||
| phc->offset += phdr->p_filesz; | phc->offset += phdr->p_filesz; | ||||
| phc->phdr++; | phc->phdr++; | ||||
| phc->nextseg++; | |||||
| } | } | ||||
| /* | /* | ||||
| * A callback for each_dumpable_segment() to gather information about | * A callback for each_dumpable_segment() to gather information about | ||||
| * the number of segments and their total size. | * the number of segments and their total size. | ||||
| */ | */ | ||||
| static void | static void | ||||
| cb_size_segment(vm_map_entry_t entry, void *closure) | cb_size_segment(vm_map_entry_t entry, void *closure) | ||||
| ▲ Show 20 Lines • Show All 246 Lines • ▼ Show 20 Lines | __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs, | ||||
| phdr->p_memsz = 0; | phdr->p_memsz = 0; | ||||
| phdr->p_flags = PF_R; | phdr->p_flags = PF_R; | ||||
| phdr->p_align = ELF_NOTE_ROUNDSIZE; | phdr->p_align = ELF_NOTE_ROUNDSIZE; | ||||
| phdr++; | phdr++; | ||||
| /* All the writable segments from the program. */ | /* All the writable segments from the program. */ | ||||
| phc.phdr = phdr; | phc.phdr = phdr; | ||||
| phc.offset = round_page(hdrsize + notesz); | phc.offset = round_page(hdrsize + notesz); | ||||
| phc.numsegs = numsegs; | |||||
| phc.nextseg = 0; | |||||
| each_dumpable_segment(td, cb_put_phdr, &phc, flags); | each_dumpable_segment(td, cb_put_phdr, &phc, flags); | ||||
| } | } | ||||
| static size_t | static size_t | ||||
| __elfN(register_regset_note)(struct thread *td, struct note_info_list *list, | __elfN(register_regset_note)(struct thread *td, struct note_info_list *list, | ||||
| struct regset *regset, struct thread *target_td) | struct regset *regset, struct thread *target_td) | ||||
| { | { | ||||
| const struct sysentvec *sv; | const struct sysentvec *sv; | ||||
| ▲ Show 20 Lines • Show All 985 Lines • Show Last 20 Lines | |||||
This M_ZERO is to make sure we don't write out uninitialized memory if the number of phdrs shrinks instead of growing.
In general, if something is mutating the vm_map as we're writing out a core dump, then I don't expect the core dump to be consistent; it's enough to ensure that such races don't trigger any memory safety problems in the kernel.