vdso2c.h 5.1 KB

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