genvdso.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) 2015 Imagination Technologies
  4. * Author: Alex Smith <alex.smith@imgtec.com>
  5. */
  6. static inline bool FUNC(patch_vdso)(const char *path, void *vdso)
  7. {
  8. const ELF(Ehdr) *ehdr = vdso;
  9. void *shdrs;
  10. ELF(Shdr) *shdr;
  11. char *shstrtab, *name;
  12. uint16_t sh_count, sh_entsize, i;
  13. shdrs = vdso + FUNC(swap_uint)(ehdr->e_shoff);
  14. sh_count = swap_uint16(ehdr->e_shnum);
  15. sh_entsize = swap_uint16(ehdr->e_shentsize);
  16. shdr = shdrs + (sh_entsize * swap_uint16(ehdr->e_shstrndx));
  17. shstrtab = vdso + FUNC(swap_uint)(shdr->sh_offset);
  18. for (i = 0; i < sh_count; i++) {
  19. shdr = shdrs + (i * sh_entsize);
  20. name = shstrtab + swap_uint32(shdr->sh_name);
  21. /*
  22. * Ensure there are no relocation sections - ld.so does not
  23. * relocate the VDSO so if there are relocations things will
  24. * break.
  25. */
  26. switch (swap_uint32(shdr->sh_type)) {
  27. case SHT_REL:
  28. case SHT_RELA:
  29. fprintf(stderr,
  30. "%s: '%s' contains relocation sections\n",
  31. program_name, path);
  32. return false;
  33. }
  34. /* Check for existing sections. */
  35. if (strcmp(name, ".MIPS.abiflags") == 0) {
  36. fprintf(stderr,
  37. "%s: '%s' already contains a '.MIPS.abiflags' section\n",
  38. program_name, path);
  39. return false;
  40. }
  41. if (strcmp(name, ".mips_abiflags") == 0) {
  42. strcpy(name, ".MIPS.abiflags");
  43. shdr->sh_type = swap_uint32(SHT_MIPS_ABIFLAGS);
  44. shdr->sh_entsize = shdr->sh_size;
  45. }
  46. }
  47. return true;
  48. }
  49. static inline bool FUNC(get_symbols)(const char *path, void *vdso)
  50. {
  51. const ELF(Ehdr) *ehdr = vdso;
  52. void *shdrs, *symtab;
  53. ELF(Shdr) *shdr;
  54. const ELF(Sym) *sym;
  55. char *strtab, *name;
  56. uint16_t sh_count, sh_entsize, st_count, st_entsize, i, j;
  57. uint64_t offset;
  58. uint32_t flags;
  59. shdrs = vdso + FUNC(swap_uint)(ehdr->e_shoff);
  60. sh_count = swap_uint16(ehdr->e_shnum);
  61. sh_entsize = swap_uint16(ehdr->e_shentsize);
  62. for (i = 0; i < sh_count; i++) {
  63. shdr = shdrs + (i * sh_entsize);
  64. if (swap_uint32(shdr->sh_type) == SHT_SYMTAB)
  65. break;
  66. }
  67. if (i == sh_count) {
  68. fprintf(stderr, "%s: '%s' has no symbol table\n", program_name,
  69. path);
  70. return false;
  71. }
  72. /* Get flags */
  73. flags = swap_uint32(ehdr->e_flags);
  74. if (elf_class == ELFCLASS64)
  75. elf_abi = ABI_N64;
  76. else if (flags & EF_MIPS_ABI2)
  77. elf_abi = ABI_N32;
  78. else
  79. elf_abi = ABI_O32;
  80. /* Get symbol table. */
  81. symtab = vdso + FUNC(swap_uint)(shdr->sh_offset);
  82. st_entsize = FUNC(swap_uint)(shdr->sh_entsize);
  83. st_count = FUNC(swap_uint)(shdr->sh_size) / st_entsize;
  84. /* Get string table. */
  85. shdr = shdrs + (swap_uint32(shdr->sh_link) * sh_entsize);
  86. strtab = vdso + FUNC(swap_uint)(shdr->sh_offset);
  87. /* Write offsets for symbols needed by the kernel. */
  88. for (i = 0; vdso_symbols[i].name; i++) {
  89. if (!(vdso_symbols[i].abis & elf_abi))
  90. continue;
  91. for (j = 0; j < st_count; j++) {
  92. sym = symtab + (j * st_entsize);
  93. name = strtab + swap_uint32(sym->st_name);
  94. if (!strcmp(name, vdso_symbols[i].name)) {
  95. offset = FUNC(swap_uint)(sym->st_value);
  96. fprintf(out_file,
  97. "\t.%s = 0x%" PRIx64 ",\n",
  98. vdso_symbols[i].offset_name, offset);
  99. break;
  100. }
  101. }
  102. if (j == st_count) {
  103. fprintf(stderr,
  104. "%s: '%s' is missing required symbol '%s'\n",
  105. program_name, path, vdso_symbols[i].name);
  106. return false;
  107. }
  108. }
  109. return true;
  110. }