genvdso.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2015 Imagination Technologies
  3. * Author: Alex Smith <alex.smith@imgtec.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. static inline bool FUNC(patch_vdso)(const char *path, void *vdso)
  11. {
  12. const ELF(Ehdr) *ehdr = vdso;
  13. void *shdrs;
  14. ELF(Shdr) *shdr;
  15. char *shstrtab, *name;
  16. uint16_t sh_count, sh_entsize, i;
  17. unsigned int local_gotno, symtabno, gotsym;
  18. ELF(Dyn) *dyn = NULL;
  19. shdrs = vdso + FUNC(swap_uint)(ehdr->e_shoff);
  20. sh_count = swap_uint16(ehdr->e_shnum);
  21. sh_entsize = swap_uint16(ehdr->e_shentsize);
  22. shdr = shdrs + (sh_entsize * swap_uint16(ehdr->e_shstrndx));
  23. shstrtab = vdso + FUNC(swap_uint)(shdr->sh_offset);
  24. for (i = 0; i < sh_count; i++) {
  25. shdr = shdrs + (i * sh_entsize);
  26. name = shstrtab + swap_uint32(shdr->sh_name);
  27. /*
  28. * Ensure there are no relocation sections - ld.so does not
  29. * relocate the VDSO so if there are relocations things will
  30. * break.
  31. */
  32. switch (swap_uint32(shdr->sh_type)) {
  33. case SHT_REL:
  34. case SHT_RELA:
  35. fprintf(stderr,
  36. "%s: '%s' contains relocation sections\n",
  37. program_name, path);
  38. return false;
  39. case SHT_DYNAMIC:
  40. dyn = vdso + FUNC(swap_uint)(shdr->sh_offset);
  41. break;
  42. }
  43. /* Check for existing sections. */
  44. if (strcmp(name, ".MIPS.abiflags") == 0) {
  45. fprintf(stderr,
  46. "%s: '%s' already contains a '.MIPS.abiflags' section\n",
  47. program_name, path);
  48. return false;
  49. }
  50. if (strcmp(name, ".mips_abiflags") == 0) {
  51. strcpy(name, ".MIPS.abiflags");
  52. shdr->sh_type = swap_uint32(SHT_MIPS_ABIFLAGS);
  53. shdr->sh_entsize = shdr->sh_size;
  54. }
  55. }
  56. /*
  57. * Ensure the GOT has no entries other than the standard 2, for the same
  58. * reason we check that there's no relocation sections above.
  59. * The standard two entries are:
  60. * - Lazy resolver
  61. * - Module pointer
  62. */
  63. if (dyn) {
  64. local_gotno = symtabno = gotsym = 0;
  65. while (FUNC(swap_uint)(dyn->d_tag) != DT_NULL) {
  66. switch (FUNC(swap_uint)(dyn->d_tag)) {
  67. /*
  68. * This member holds the number of local GOT entries.
  69. */
  70. case DT_MIPS_LOCAL_GOTNO:
  71. local_gotno = FUNC(swap_uint)(dyn->d_un.d_val);
  72. break;
  73. /*
  74. * This member holds the number of entries in the
  75. * .dynsym section.
  76. */
  77. case DT_MIPS_SYMTABNO:
  78. symtabno = FUNC(swap_uint)(dyn->d_un.d_val);
  79. break;
  80. /*
  81. * This member holds the index of the first dynamic
  82. * symbol table entry that corresponds to an entry in
  83. * the GOT.
  84. */
  85. case DT_MIPS_GOTSYM:
  86. gotsym = FUNC(swap_uint)(dyn->d_un.d_val);
  87. break;
  88. }
  89. dyn++;
  90. }
  91. if (local_gotno > 2 || symtabno - gotsym) {
  92. fprintf(stderr,
  93. "%s: '%s' contains unexpected GOT entries\n",
  94. program_name, path);
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. static inline bool FUNC(get_symbols)(const char *path, void *vdso)
  101. {
  102. const ELF(Ehdr) *ehdr = vdso;
  103. void *shdrs, *symtab;
  104. ELF(Shdr) *shdr;
  105. const ELF(Sym) *sym;
  106. char *strtab, *name;
  107. uint16_t sh_count, sh_entsize, st_count, st_entsize, i, j;
  108. uint64_t offset;
  109. uint32_t flags;
  110. shdrs = vdso + FUNC(swap_uint)(ehdr->e_shoff);
  111. sh_count = swap_uint16(ehdr->e_shnum);
  112. sh_entsize = swap_uint16(ehdr->e_shentsize);
  113. for (i = 0; i < sh_count; i++) {
  114. shdr = shdrs + (i * sh_entsize);
  115. if (swap_uint32(shdr->sh_type) == SHT_SYMTAB)
  116. break;
  117. }
  118. if (i == sh_count) {
  119. fprintf(stderr, "%s: '%s' has no symbol table\n", program_name,
  120. path);
  121. return false;
  122. }
  123. /* Get flags */
  124. flags = swap_uint32(ehdr->e_flags);
  125. if (elf_class == ELFCLASS64)
  126. elf_abi = ABI_N64;
  127. else if (flags & EF_MIPS_ABI2)
  128. elf_abi = ABI_N32;
  129. else
  130. elf_abi = ABI_O32;
  131. /* Get symbol table. */
  132. symtab = vdso + FUNC(swap_uint)(shdr->sh_offset);
  133. st_entsize = FUNC(swap_uint)(shdr->sh_entsize);
  134. st_count = FUNC(swap_uint)(shdr->sh_size) / st_entsize;
  135. /* Get string table. */
  136. shdr = shdrs + (swap_uint32(shdr->sh_link) * sh_entsize);
  137. strtab = vdso + FUNC(swap_uint)(shdr->sh_offset);
  138. /* Write offsets for symbols needed by the kernel. */
  139. for (i = 0; vdso_symbols[i].name; i++) {
  140. if (!(vdso_symbols[i].abis & elf_abi))
  141. continue;
  142. for (j = 0; j < st_count; j++) {
  143. sym = symtab + (j * st_entsize);
  144. name = strtab + swap_uint32(sym->st_name);
  145. if (!strcmp(name, vdso_symbols[i].name)) {
  146. offset = FUNC(swap_uint)(sym->st_value);
  147. fprintf(out_file,
  148. "\t.%s = 0x%" PRIx64 ",\n",
  149. vdso_symbols[i].offset_name, offset);
  150. break;
  151. }
  152. }
  153. if (j == st_count) {
  154. fprintf(stderr,
  155. "%s: '%s' is missing required symbol '%s'\n",
  156. program_name, path, vdso_symbols[i].name);
  157. return false;
  158. }
  159. }
  160. return true;
  161. }