elfcore.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/elf.h>
  3. #include <linux/coredump.h>
  4. #include <linux/fs.h>
  5. #include <linux/mm.h>
  6. #include <asm/elf.h>
  7. Elf64_Half elf_core_extra_phdrs(void)
  8. {
  9. return GATE_EHDR->e_phnum;
  10. }
  11. int elf_core_write_extra_phdrs(struct coredump_params *cprm, loff_t offset)
  12. {
  13. const struct elf_phdr *const gate_phdrs =
  14. (const struct elf_phdr *) (GATE_ADDR + GATE_EHDR->e_phoff);
  15. int i;
  16. Elf64_Off ofs = 0;
  17. for (i = 0; i < GATE_EHDR->e_phnum; ++i) {
  18. struct elf_phdr phdr = gate_phdrs[i];
  19. if (phdr.p_type == PT_LOAD) {
  20. phdr.p_memsz = PAGE_ALIGN(phdr.p_memsz);
  21. phdr.p_filesz = phdr.p_memsz;
  22. if (ofs == 0) {
  23. ofs = phdr.p_offset = offset;
  24. offset += phdr.p_filesz;
  25. } else {
  26. phdr.p_offset = ofs;
  27. }
  28. } else {
  29. phdr.p_offset += ofs;
  30. }
  31. phdr.p_paddr = 0; /* match other core phdrs */
  32. if (!dump_emit(cprm, &phdr, sizeof(phdr)))
  33. return 0;
  34. }
  35. return 1;
  36. }
  37. int elf_core_write_extra_data(struct coredump_params *cprm)
  38. {
  39. const struct elf_phdr *const gate_phdrs =
  40. (const struct elf_phdr *) (GATE_ADDR + GATE_EHDR->e_phoff);
  41. int i;
  42. for (i = 0; i < GATE_EHDR->e_phnum; ++i) {
  43. if (gate_phdrs[i].p_type == PT_LOAD) {
  44. void *addr = (void *)gate_phdrs[i].p_vaddr;
  45. size_t memsz = PAGE_ALIGN(gate_phdrs[i].p_memsz);
  46. if (!dump_emit(cprm, addr, memsz))
  47. return 0;
  48. break;
  49. }
  50. }
  51. return 1;
  52. }
  53. size_t elf_core_extra_data_size(void)
  54. {
  55. const struct elf_phdr *const gate_phdrs =
  56. (const struct elf_phdr *) (GATE_ADDR + GATE_EHDR->e_phoff);
  57. int i;
  58. size_t size = 0;
  59. for (i = 0; i < GATE_EHDR->e_phnum; ++i) {
  60. if (gate_phdrs[i].p_type == PT_LOAD) {
  61. size += PAGE_ALIGN(gate_phdrs[i].p_memsz);
  62. break;
  63. }
  64. }
  65. return size;
  66. }