vdso2c.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /*
  7. * We're writing a section table for a few reasons:
  8. *
  9. * The Go runtime had a couple of bugs: it would read the section
  10. * table to try to figure out how many dynamic symbols there were (it
  11. * shouldn't have looked at the section table at all) and, if there
  12. * were no SHT_SYNDYM section table entry, it would use an
  13. * uninitialized value for the number of symbols. An empty DYNSYM
  14. * table would work, but I see no reason not to write a valid one (and
  15. * keep full performance for old Go programs). This hack is only
  16. * needed on x86_64.
  17. *
  18. * The bug was introduced on 2012-08-31 by:
  19. * https://code.google.com/p/go/source/detail?r=56ea40aac72b
  20. * and was fixed on 2014-06-13 by:
  21. * https://code.google.com/p/go/source/detail?r=fc1cd5e12595
  22. *
  23. * Binutils has issues debugging the vDSO: it reads the section table to
  24. * find SHT_NOTE; it won't look at PT_NOTE for the in-memory vDSO, which
  25. * would break build-id if we removed the section table. Binutils
  26. * also requires that shstrndx != 0. See:
  27. * https://sourceware.org/bugzilla/show_bug.cgi?id=17064
  28. *
  29. * elfutils might not look for PT_NOTE if there is a section table at
  30. * all. I don't know whether this matters for any practical purpose.
  31. *
  32. * For simplicity, rather than hacking up a partial section table, we
  33. * just write a mostly complete one. We omit non-dynamic symbols,
  34. * though, since they're rather large.
  35. *
  36. * Once binutils gets fixed, we might be able to drop this for all but
  37. * the 64-bit vdso, since build-id only works in kernel RPMs, and
  38. * systems that update to new enough kernel RPMs will likely update
  39. * binutils in sync. build-id has never worked for home-built kernel
  40. * RPMs without manual symlinking, and I suspect that no one ever does
  41. * that.
  42. */
  43. struct BITSFUNC(fake_sections)
  44. {
  45. ELF(Shdr) *table;
  46. unsigned long table_offset;
  47. int count, max_count;
  48. int in_shstrndx;
  49. unsigned long shstr_offset;
  50. const char *shstrtab;
  51. size_t shstrtab_len;
  52. int out_shstrndx;
  53. };
  54. static unsigned int BITSFUNC(find_shname)(struct BITSFUNC(fake_sections) *out,
  55. const char *name)
  56. {
  57. const char *outname = out->shstrtab;
  58. while (outname - out->shstrtab < out->shstrtab_len) {
  59. if (!strcmp(name, outname))
  60. return (outname - out->shstrtab) + out->shstr_offset;
  61. outname += strlen(outname) + 1;
  62. }
  63. if (*name)
  64. printf("Warning: could not find output name \"%s\"\n", name);
  65. return out->shstr_offset + out->shstrtab_len - 1; /* Use a null. */
  66. }
  67. static void BITSFUNC(init_sections)(struct BITSFUNC(fake_sections) *out)
  68. {
  69. if (!out->in_shstrndx)
  70. fail("didn't find the fake shstrndx\n");
  71. memset(out->table, 0, out->max_count * sizeof(ELF(Shdr)));
  72. if (out->max_count < 1)
  73. fail("we need at least two fake output sections\n");
  74. PUT_LE(&out->table[0].sh_type, SHT_NULL);
  75. PUT_LE(&out->table[0].sh_name, BITSFUNC(find_shname)(out, ""));
  76. out->count = 1;
  77. }
  78. static void BITSFUNC(copy_section)(struct BITSFUNC(fake_sections) *out,
  79. int in_idx, const ELF(Shdr) *in,
  80. const char *name)
  81. {
  82. uint64_t flags = GET_LE(&in->sh_flags);
  83. bool copy = flags & SHF_ALLOC &&
  84. (GET_LE(&in->sh_size) ||
  85. (GET_LE(&in->sh_type) != SHT_RELA &&
  86. GET_LE(&in->sh_type) != SHT_REL)) &&
  87. strcmp(name, ".altinstructions") &&
  88. strcmp(name, ".altinstr_replacement");
  89. if (!copy)
  90. return;
  91. if (out->count >= out->max_count)
  92. fail("too many copied sections (max = %d)\n", out->max_count);
  93. if (in_idx == out->in_shstrndx)
  94. out->out_shstrndx = out->count;
  95. out->table[out->count] = *in;
  96. PUT_LE(&out->table[out->count].sh_name,
  97. BITSFUNC(find_shname)(out, name));
  98. /* elfutils requires that a strtab have the correct type. */
  99. if (!strcmp(name, ".fake_shstrtab"))
  100. PUT_LE(&out->table[out->count].sh_type, SHT_STRTAB);
  101. out->count++;
  102. }
  103. static void BITSFUNC(go)(void *addr, size_t len,
  104. FILE *outfile, const char *name)
  105. {
  106. int found_load = 0;
  107. unsigned long load_size = -1; /* Work around bogus warning */
  108. unsigned long data_size;
  109. ELF(Ehdr) *hdr = (ELF(Ehdr) *)addr;
  110. int i;
  111. unsigned long j;
  112. ELF(Shdr) *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr,
  113. *alt_sec = NULL;
  114. ELF(Dyn) *dyn = 0, *dyn_end = 0;
  115. const char *secstrings;
  116. uint64_t syms[NSYMS] = {};
  117. struct BITSFUNC(fake_sections) fake_sections = {};
  118. ELF(Phdr) *pt = (ELF(Phdr) *)(addr + GET_LE(&hdr->e_phoff));
  119. /* Walk the segment table. */
  120. for (i = 0; i < GET_LE(&hdr->e_phnum); i++) {
  121. if (GET_LE(&pt[i].p_type) == PT_LOAD) {
  122. if (found_load)
  123. fail("multiple PT_LOAD segs\n");
  124. if (GET_LE(&pt[i].p_offset) != 0 ||
  125. GET_LE(&pt[i].p_vaddr) != 0)
  126. fail("PT_LOAD in wrong place\n");
  127. if (GET_LE(&pt[i].p_memsz) != GET_LE(&pt[i].p_filesz))
  128. fail("cannot handle memsz != filesz\n");
  129. load_size = GET_LE(&pt[i].p_memsz);
  130. found_load = 1;
  131. } else if (GET_LE(&pt[i].p_type) == PT_DYNAMIC) {
  132. dyn = addr + GET_LE(&pt[i].p_offset);
  133. dyn_end = addr + GET_LE(&pt[i].p_offset) +
  134. GET_LE(&pt[i].p_memsz);
  135. }
  136. }
  137. if (!found_load)
  138. fail("no PT_LOAD seg\n");
  139. data_size = (load_size + 4095) / 4096 * 4096;
  140. /* Walk the dynamic table */
  141. for (i = 0; dyn + i < dyn_end &&
  142. GET_LE(&dyn[i].d_tag) != DT_NULL; i++) {
  143. typeof(dyn[i].d_tag) tag = GET_LE(&dyn[i].d_tag);
  144. if (tag == DT_REL || tag == DT_RELSZ || tag == DT_RELA ||
  145. tag == DT_RELENT || tag == DT_TEXTREL)
  146. fail("vdso image contains dynamic relocations\n");
  147. }
  148. /* Walk the section table */
  149. secstrings_hdr = addr + GET_LE(&hdr->e_shoff) +
  150. GET_LE(&hdr->e_shentsize)*GET_LE(&hdr->e_shstrndx);
  151. secstrings = addr + GET_LE(&secstrings_hdr->sh_offset);
  152. for (i = 0; i < GET_LE(&hdr->e_shnum); i++) {
  153. ELF(Shdr) *sh = addr + GET_LE(&hdr->e_shoff) +
  154. GET_LE(&hdr->e_shentsize) * i;
  155. if (GET_LE(&sh->sh_type) == SHT_SYMTAB)
  156. symtab_hdr = sh;
  157. if (!strcmp(secstrings + GET_LE(&sh->sh_name),
  158. ".altinstructions"))
  159. alt_sec = sh;
  160. }
  161. if (!symtab_hdr)
  162. fail("no symbol table\n");
  163. strtab_hdr = addr + GET_LE(&hdr->e_shoff) +
  164. GET_LE(&hdr->e_shentsize) * GET_LE(&symtab_hdr->sh_link);
  165. /* Walk the symbol table */
  166. for (i = 0;
  167. i < GET_LE(&symtab_hdr->sh_size) / GET_LE(&symtab_hdr->sh_entsize);
  168. i++) {
  169. int k;
  170. ELF(Sym) *sym = addr + GET_LE(&symtab_hdr->sh_offset) +
  171. GET_LE(&symtab_hdr->sh_entsize) * i;
  172. const char *name = addr + GET_LE(&strtab_hdr->sh_offset) +
  173. GET_LE(&sym->st_name);
  174. for (k = 0; k < NSYMS; k++) {
  175. if (!strcmp(name, required_syms[k].name)) {
  176. if (syms[k]) {
  177. fail("duplicate symbol %s\n",
  178. required_syms[k].name);
  179. }
  180. syms[k] = GET_LE(&sym->st_value);
  181. }
  182. }
  183. if (!strcmp(name, "fake_shstrtab")) {
  184. ELF(Shdr) *sh;
  185. fake_sections.in_shstrndx = GET_LE(&sym->st_shndx);
  186. fake_sections.shstrtab = addr + GET_LE(&sym->st_value);
  187. fake_sections.shstrtab_len = GET_LE(&sym->st_size);
  188. sh = addr + GET_LE(&hdr->e_shoff) +
  189. GET_LE(&hdr->e_shentsize) *
  190. fake_sections.in_shstrndx;
  191. fake_sections.shstr_offset = GET_LE(&sym->st_value) -
  192. GET_LE(&sh->sh_addr);
  193. }
  194. }
  195. /* Build the output section table. */
  196. if (!syms[sym_VDSO_FAKE_SECTION_TABLE_START] ||
  197. !syms[sym_VDSO_FAKE_SECTION_TABLE_END])
  198. fail("couldn't find fake section table\n");
  199. if ((syms[sym_VDSO_FAKE_SECTION_TABLE_END] -
  200. syms[sym_VDSO_FAKE_SECTION_TABLE_START]) % sizeof(ELF(Shdr)))
  201. fail("fake section table size isn't a multiple of sizeof(Shdr)\n");
  202. fake_sections.table = addr + syms[sym_VDSO_FAKE_SECTION_TABLE_START];
  203. fake_sections.table_offset = syms[sym_VDSO_FAKE_SECTION_TABLE_START];
  204. fake_sections.max_count = (syms[sym_VDSO_FAKE_SECTION_TABLE_END] -
  205. syms[sym_VDSO_FAKE_SECTION_TABLE_START]) /
  206. sizeof(ELF(Shdr));
  207. BITSFUNC(init_sections)(&fake_sections);
  208. for (i = 0; i < GET_LE(&hdr->e_shnum); i++) {
  209. ELF(Shdr) *sh = addr + GET_LE(&hdr->e_shoff) +
  210. GET_LE(&hdr->e_shentsize) * i;
  211. BITSFUNC(copy_section)(&fake_sections, i, sh,
  212. secstrings + GET_LE(&sh->sh_name));
  213. }
  214. if (!fake_sections.out_shstrndx)
  215. fail("didn't generate shstrndx?!?\n");
  216. PUT_LE(&hdr->e_shoff, fake_sections.table_offset);
  217. PUT_LE(&hdr->e_shentsize, sizeof(ELF(Shdr)));
  218. PUT_LE(&hdr->e_shnum, fake_sections.count);
  219. PUT_LE(&hdr->e_shstrndx, fake_sections.out_shstrndx);
  220. /* Validate mapping addresses. */
  221. for (i = 0; i < sizeof(special_pages) / sizeof(special_pages[0]); i++) {
  222. if (!syms[i])
  223. continue; /* The mapping isn't used; ignore it. */
  224. if (syms[i] % 4096)
  225. fail("%s must be a multiple of 4096\n",
  226. required_syms[i].name);
  227. if (syms[i] < data_size)
  228. fail("%s must be after the text mapping\n",
  229. required_syms[i].name);
  230. if (syms[sym_end_mapping] < syms[i] + 4096)
  231. fail("%s overruns end_mapping\n",
  232. required_syms[i].name);
  233. }
  234. if (syms[sym_end_mapping] % 4096)
  235. fail("end_mapping must be a multiple of 4096\n");
  236. if (!name) {
  237. fwrite(addr, load_size, 1, outfile);
  238. return;
  239. }
  240. fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
  241. fprintf(outfile, "#include <linux/linkage.h>\n");
  242. fprintf(outfile, "#include <asm/page_types.h>\n");
  243. fprintf(outfile, "#include <asm/vdso.h>\n");
  244. fprintf(outfile, "\n");
  245. fprintf(outfile,
  246. "static unsigned char raw_data[%lu] __page_aligned_data = {",
  247. data_size);
  248. for (j = 0; j < load_size; j++) {
  249. if (j % 10 == 0)
  250. fprintf(outfile, "\n\t");
  251. fprintf(outfile, "0x%02X, ", (int)((unsigned char *)addr)[j]);
  252. }
  253. fprintf(outfile, "\n};\n\n");
  254. fprintf(outfile, "static struct page *pages[%lu];\n\n",
  255. data_size / 4096);
  256. fprintf(outfile, "const struct vdso_image %s = {\n", name);
  257. fprintf(outfile, "\t.data = raw_data,\n");
  258. fprintf(outfile, "\t.size = %lu,\n", data_size);
  259. fprintf(outfile, "\t.text_mapping = {\n");
  260. fprintf(outfile, "\t\t.name = \"[vdso]\",\n");
  261. fprintf(outfile, "\t\t.pages = pages,\n");
  262. fprintf(outfile, "\t},\n");
  263. if (alt_sec) {
  264. fprintf(outfile, "\t.alt = %lu,\n",
  265. (unsigned long)GET_LE(&alt_sec->sh_offset));
  266. fprintf(outfile, "\t.alt_len = %lu,\n",
  267. (unsigned long)GET_LE(&alt_sec->sh_size));
  268. }
  269. for (i = 0; i < NSYMS; i++) {
  270. if (required_syms[i].export && syms[i])
  271. fprintf(outfile, "\t.sym_%s = 0x%" PRIx64 ",\n",
  272. required_syms[i].name, syms[i]);
  273. }
  274. fprintf(outfile, "};\n");
  275. }