vdso2c.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
  3. */
  4. /*
  5. * This file is included up to twice from vdso2c.c. It generates code for
  6. * 32-bit and 64-bit vDSOs. We will eventually need both for 64-bit builds,
  7. * since 32-bit vDSOs will then be built for 32-bit userspace.
  8. */
  9. static void BITSFUNC(go)(void *raw_addr, size_t raw_len,
  10. void *stripped_addr, size_t stripped_len,
  11. FILE *outfile, const char *name)
  12. {
  13. int found_load = 0;
  14. unsigned long load_size = -1; /* Work around bogus warning */
  15. unsigned long mapping_size;
  16. int i;
  17. unsigned long j;
  18. ELF(Shdr) *symtab_hdr = NULL, *strtab_hdr;
  19. ELF(Ehdr) *hdr = (ELF(Ehdr) *)raw_addr;
  20. ELF(Dyn) *dyn = 0, *dyn_end = 0;
  21. INT_BITS syms[NSYMS] = {};
  22. ELF(Phdr) *pt = (ELF(Phdr) *)(raw_addr + GET_BE(&hdr->e_phoff));
  23. /* Walk the segment table. */
  24. for (i = 0; i < GET_BE(&hdr->e_phnum); i++) {
  25. if (GET_BE(&pt[i].p_type) == PT_LOAD) {
  26. if (found_load)
  27. fail("multiple PT_LOAD segs\n");
  28. if (GET_BE(&pt[i].p_offset) != 0 ||
  29. GET_BE(&pt[i].p_vaddr) != 0)
  30. fail("PT_LOAD in wrong place\n");
  31. if (GET_BE(&pt[i].p_memsz) != GET_BE(&pt[i].p_filesz))
  32. fail("cannot handle memsz != filesz\n");
  33. load_size = GET_BE(&pt[i].p_memsz);
  34. found_load = 1;
  35. } else if (GET_BE(&pt[i].p_type) == PT_DYNAMIC) {
  36. dyn = raw_addr + GET_BE(&pt[i].p_offset);
  37. dyn_end = raw_addr + GET_BE(&pt[i].p_offset) +
  38. GET_BE(&pt[i].p_memsz);
  39. }
  40. }
  41. if (!found_load)
  42. fail("no PT_LOAD seg\n");
  43. if (stripped_len < load_size)
  44. fail("stripped input is too short\n");
  45. /* Walk the dynamic table */
  46. for (i = 0; dyn + i < dyn_end &&
  47. GET_BE(&dyn[i].d_tag) != DT_NULL; i++) {
  48. typeof(dyn[i].d_tag) tag = GET_BE(&dyn[i].d_tag);
  49. typeof(dyn[i].d_un.d_val) val = GET_BE(&dyn[i].d_un.d_val);
  50. if ((tag == DT_RELSZ || tag == DT_RELASZ) && (val != 0))
  51. fail("vdso image contains dynamic relocations\n");
  52. }
  53. /* Walk the section table */
  54. for (i = 0; i < GET_BE(&hdr->e_shnum); i++) {
  55. ELF(Shdr) *sh = raw_addr + GET_BE(&hdr->e_shoff) +
  56. GET_BE(&hdr->e_shentsize) * i;
  57. if (GET_BE(&sh->sh_type) == SHT_SYMTAB)
  58. symtab_hdr = sh;
  59. }
  60. if (!symtab_hdr)
  61. fail("no symbol table\n");
  62. strtab_hdr = raw_addr + GET_BE(&hdr->e_shoff) +
  63. GET_BE(&hdr->e_shentsize) * GET_BE(&symtab_hdr->sh_link);
  64. /* Walk the symbol table */
  65. for (i = 0;
  66. i < GET_BE(&symtab_hdr->sh_size) / GET_BE(&symtab_hdr->sh_entsize);
  67. i++) {
  68. int k;
  69. ELF(Sym) *sym = raw_addr + GET_BE(&symtab_hdr->sh_offset) +
  70. GET_BE(&symtab_hdr->sh_entsize) * i;
  71. const char *name = raw_addr + GET_BE(&strtab_hdr->sh_offset) +
  72. GET_BE(&sym->st_name);
  73. for (k = 0; k < NSYMS; k++) {
  74. if (!strcmp(name, required_syms[k].name)) {
  75. if (syms[k]) {
  76. fail("duplicate symbol %s\n",
  77. required_syms[k].name);
  78. }
  79. /*
  80. * Careful: we use negative addresses, but
  81. * st_value is unsigned, so we rely
  82. * on syms[k] being a signed type of the
  83. * correct width.
  84. */
  85. syms[k] = GET_BE(&sym->st_value);
  86. }
  87. }
  88. }
  89. /* Validate mapping addresses. */
  90. if (syms[sym_vvar_start] % 8192)
  91. fail("vvar_begin must be a multiple of 8192\n");
  92. if (!name) {
  93. fwrite(stripped_addr, stripped_len, 1, outfile);
  94. return;
  95. }
  96. mapping_size = (stripped_len + 8191) / 8192 * 8192;
  97. fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
  98. fprintf(outfile, "#include <linux/cache.h>\n");
  99. fprintf(outfile, "#include <asm/vdso.h>\n");
  100. fprintf(outfile, "\n");
  101. fprintf(outfile,
  102. "static unsigned char raw_data[%lu] __ro_after_init __aligned(8192)= {",
  103. mapping_size);
  104. for (j = 0; j < stripped_len; j++) {
  105. if (j % 10 == 0)
  106. fprintf(outfile, "\n\t");
  107. fprintf(outfile, "0x%02X, ",
  108. (int)((unsigned char *)stripped_addr)[j]);
  109. }
  110. fprintf(outfile, "\n};\n\n");
  111. fprintf(outfile, "const struct vdso_image %s_builtin = {\n", name);
  112. fprintf(outfile, "\t.data = raw_data,\n");
  113. fprintf(outfile, "\t.size = %lu,\n", mapping_size);
  114. for (i = 0; i < NSYMS; i++) {
  115. if (required_syms[i].export && syms[i])
  116. fprintf(outfile, "\t.sym_%s = %" PRIi64 ",\n",
  117. required_syms[i].name, (int64_t)syms[i]);
  118. }
  119. fprintf(outfile, "};\n");
  120. }