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