vdso2c.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * This file is included twice from vdso2c.c. It generates code for 32-bit
  4. * and 64-bit vDSOs. We need both for 64-bit builds, since 32-bit vDSOs
  5. * are built for 32-bit userspace.
  6. */
  7. static void BITSFUNC(go)(void *raw_addr, size_t raw_len,
  8. void *stripped_addr, size_t stripped_len,
  9. FILE *outfile, const char *name)
  10. {
  11. int found_load = 0;
  12. unsigned long load_size = -1; /* Work around bogus warning */
  13. unsigned long mapping_size;
  14. ELF(Ehdr) *hdr = (ELF(Ehdr) *)raw_addr;
  15. int i;
  16. unsigned long j;
  17. ELF(Shdr) *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr,
  18. *alt_sec = NULL;
  19. ELF(Dyn) *dyn = 0, *dyn_end = 0;
  20. const char *secstrings;
  21. INT_BITS syms[NSYMS] = {};
  22. ELF(Phdr) *pt = (ELF(Phdr) *)(raw_addr + GET_LE(&hdr->e_phoff));
  23. if (GET_LE(&hdr->e_type) != ET_DYN)
  24. fail("input is not a shared object\n");
  25. /* Walk the segment table. */
  26. for (i = 0; i < GET_LE(&hdr->e_phnum); i++) {
  27. if (GET_LE(&pt[i].p_type) == PT_LOAD) {
  28. if (found_load)
  29. fail("multiple PT_LOAD segs\n");
  30. if (GET_LE(&pt[i].p_offset) != 0 ||
  31. GET_LE(&pt[i].p_vaddr) != 0)
  32. fail("PT_LOAD in wrong place\n");
  33. if (GET_LE(&pt[i].p_memsz) != GET_LE(&pt[i].p_filesz))
  34. fail("cannot handle memsz != filesz\n");
  35. load_size = GET_LE(&pt[i].p_memsz);
  36. found_load = 1;
  37. } else if (GET_LE(&pt[i].p_type) == PT_DYNAMIC) {
  38. dyn = raw_addr + GET_LE(&pt[i].p_offset);
  39. dyn_end = raw_addr + GET_LE(&pt[i].p_offset) +
  40. GET_LE(&pt[i].p_memsz);
  41. }
  42. }
  43. if (!found_load)
  44. fail("no PT_LOAD seg\n");
  45. if (stripped_len < load_size)
  46. fail("stripped input is too short\n");
  47. if (!dyn)
  48. fail("input has no PT_DYNAMIC section -- your toolchain is buggy\n");
  49. /* Walk the dynamic table */
  50. for (i = 0; dyn + i < dyn_end &&
  51. GET_LE(&dyn[i].d_tag) != DT_NULL; i++) {
  52. typeof(dyn[i].d_tag) tag = GET_LE(&dyn[i].d_tag);
  53. if (tag == DT_REL || tag == DT_RELSZ || tag == DT_RELA ||
  54. tag == DT_RELENT || tag == DT_TEXTREL)
  55. fail("vdso image contains dynamic relocations\n");
  56. }
  57. /* Walk the section table */
  58. secstrings_hdr = raw_addr + GET_LE(&hdr->e_shoff) +
  59. GET_LE(&hdr->e_shentsize)*GET_LE(&hdr->e_shstrndx);
  60. secstrings = raw_addr + GET_LE(&secstrings_hdr->sh_offset);
  61. for (i = 0; i < GET_LE(&hdr->e_shnum); i++) {
  62. ELF(Shdr) *sh = raw_addr + GET_LE(&hdr->e_shoff) +
  63. GET_LE(&hdr->e_shentsize) * i;
  64. if (GET_LE(&sh->sh_type) == SHT_SYMTAB)
  65. symtab_hdr = sh;
  66. if (!strcmp(secstrings + GET_LE(&sh->sh_name),
  67. ".altinstructions"))
  68. alt_sec = sh;
  69. }
  70. if (!symtab_hdr)
  71. fail("no symbol table\n");
  72. strtab_hdr = raw_addr + GET_LE(&hdr->e_shoff) +
  73. GET_LE(&hdr->e_shentsize) * GET_LE(&symtab_hdr->sh_link);
  74. /* Walk the symbol table */
  75. for (i = 0;
  76. i < GET_LE(&symtab_hdr->sh_size) / GET_LE(&symtab_hdr->sh_entsize);
  77. i++) {
  78. int k;
  79. ELF(Sym) *sym = raw_addr + GET_LE(&symtab_hdr->sh_offset) +
  80. GET_LE(&symtab_hdr->sh_entsize) * i;
  81. const char *name = raw_addr + GET_LE(&strtab_hdr->sh_offset) +
  82. GET_LE(&sym->st_name);
  83. for (k = 0; k < NSYMS; k++) {
  84. if (!strcmp(name, required_syms[k].name)) {
  85. if (syms[k]) {
  86. fail("duplicate symbol %s\n",
  87. required_syms[k].name);
  88. }
  89. /*
  90. * Careful: we use negative addresses, but
  91. * st_value is unsigned, so we rely
  92. * on syms[k] being a signed type of the
  93. * correct width.
  94. */
  95. syms[k] = GET_LE(&sym->st_value);
  96. }
  97. }
  98. }
  99. /* Validate mapping addresses. */
  100. for (i = 0; i < sizeof(special_pages) / sizeof(special_pages[0]); i++) {
  101. INT_BITS symval = syms[special_pages[i]];
  102. if (!symval)
  103. continue; /* The mapping isn't used; ignore it. */
  104. if (symval % 4096)
  105. fail("%s must be a multiple of 4096\n",
  106. required_syms[i].name);
  107. if (symval + 4096 < syms[sym_vvar_start])
  108. fail("%s underruns vvar_start\n",
  109. required_syms[i].name);
  110. if (symval + 4096 > 0)
  111. fail("%s is on the wrong side of the vdso text\n",
  112. required_syms[i].name);
  113. }
  114. if (syms[sym_vvar_start] % 4096)
  115. fail("vvar_begin must be a multiple of 4096\n");
  116. if (!name) {
  117. fwrite(stripped_addr, stripped_len, 1, outfile);
  118. return;
  119. }
  120. mapping_size = (stripped_len + 4095) / 4096 * 4096;
  121. fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
  122. fprintf(outfile, "#include <linux/linkage.h>\n");
  123. fprintf(outfile, "#include <asm/page_types.h>\n");
  124. fprintf(outfile, "#include <asm/vdso.h>\n");
  125. fprintf(outfile, "\n");
  126. fprintf(outfile,
  127. "static unsigned char raw_data[%lu] __ro_after_init __aligned(PAGE_SIZE) = {",
  128. mapping_size);
  129. for (j = 0; j < stripped_len; j++) {
  130. if (j % 10 == 0)
  131. fprintf(outfile, "\n\t");
  132. fprintf(outfile, "0x%02X, ",
  133. (int)((unsigned char *)stripped_addr)[j]);
  134. }
  135. fprintf(outfile, "\n};\n\n");
  136. fprintf(outfile, "const struct vdso_image %s = {\n", name);
  137. fprintf(outfile, "\t.data = raw_data,\n");
  138. fprintf(outfile, "\t.size = %lu,\n", mapping_size);
  139. if (alt_sec) {
  140. fprintf(outfile, "\t.alt = %lu,\n",
  141. (unsigned long)GET_LE(&alt_sec->sh_offset));
  142. fprintf(outfile, "\t.alt_len = %lu,\n",
  143. (unsigned long)GET_LE(&alt_sec->sh_size));
  144. }
  145. for (i = 0; i < NSYMS; i++) {
  146. if (required_syms[i].export && syms[i])
  147. fprintf(outfile, "\t.sym_%s = %" PRIi64 ",\n",
  148. required_syms[i].name, (int64_t)syms[i]);
  149. }
  150. fprintf(outfile, "};\n");
  151. }