genvdso.h 3.4 KB

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