gen-vdso-elfn.c.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Post-process a vdso elf image for inclusion into qemu.
  3. * Elf size specialization.
  4. *
  5. * Copyright 2023 Linaro, Ltd.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0-or-later
  8. */
  9. static void elfN(bswap_ehdr)(ElfN(Ehdr) *ehdr)
  10. {
  11. bswaps(&ehdr->e_type); /* Object file type */
  12. bswaps(&ehdr->e_machine); /* Architecture */
  13. bswaps(&ehdr->e_version); /* Object file version */
  14. bswaps(&ehdr->e_entry); /* Entry point virtual address */
  15. bswaps(&ehdr->e_phoff); /* Program header table file offset */
  16. bswaps(&ehdr->e_shoff); /* Section header table file offset */
  17. bswaps(&ehdr->e_flags); /* Processor-specific flags */
  18. bswaps(&ehdr->e_ehsize); /* ELF header size in bytes */
  19. bswaps(&ehdr->e_phentsize); /* Program header table entry size */
  20. bswaps(&ehdr->e_phnum); /* Program header table entry count */
  21. bswaps(&ehdr->e_shentsize); /* Section header table entry size */
  22. bswaps(&ehdr->e_shnum); /* Section header table entry count */
  23. bswaps(&ehdr->e_shstrndx); /* Section header string table index */
  24. }
  25. static void elfN(bswap_phdr)(ElfN(Phdr) *phdr)
  26. {
  27. bswaps(&phdr->p_type); /* Segment type */
  28. bswaps(&phdr->p_flags); /* Segment flags */
  29. bswaps(&phdr->p_offset); /* Segment file offset */
  30. bswaps(&phdr->p_vaddr); /* Segment virtual address */
  31. bswaps(&phdr->p_paddr); /* Segment physical address */
  32. bswaps(&phdr->p_filesz); /* Segment size in file */
  33. bswaps(&phdr->p_memsz); /* Segment size in memory */
  34. bswaps(&phdr->p_align); /* Segment alignment */
  35. }
  36. static void elfN(bswap_shdr)(ElfN(Shdr) *shdr)
  37. {
  38. bswaps(&shdr->sh_name);
  39. bswaps(&shdr->sh_type);
  40. bswaps(&shdr->sh_flags);
  41. bswaps(&shdr->sh_addr);
  42. bswaps(&shdr->sh_offset);
  43. bswaps(&shdr->sh_size);
  44. bswaps(&shdr->sh_link);
  45. bswaps(&shdr->sh_info);
  46. bswaps(&shdr->sh_addralign);
  47. bswaps(&shdr->sh_entsize);
  48. }
  49. static void elfN(bswap_sym)(ElfN(Sym) *sym)
  50. {
  51. bswaps(&sym->st_name);
  52. bswaps(&sym->st_value);
  53. bswaps(&sym->st_size);
  54. bswaps(&sym->st_shndx);
  55. }
  56. static void elfN(bswap_dyn)(ElfN(Dyn) *dyn)
  57. {
  58. bswaps(&dyn->d_tag); /* Dynamic type tag */
  59. bswaps(&dyn->d_un.d_ptr); /* Dynamic ptr or val, in union */
  60. }
  61. static void elfN(search_symtab)(ElfN(Shdr) *shdr, unsigned sym_idx,
  62. void *buf, bool need_bswap)
  63. {
  64. unsigned str_idx = shdr[sym_idx].sh_link;
  65. ElfN(Sym) *target_sym = buf + shdr[sym_idx].sh_offset;
  66. unsigned sym_n = shdr[sym_idx].sh_size / sizeof(*target_sym);
  67. const char *str = buf + shdr[str_idx].sh_offset;
  68. for (unsigned i = 0; i < sym_n; ++i) {
  69. const char *name;
  70. ElfN(Sym) sym;
  71. memcpy(&sym, &target_sym[i], sizeof(sym));
  72. if (need_bswap) {
  73. elfN(bswap_sym)(&sym);
  74. }
  75. name = str + sym.st_name;
  76. if (sigreturn_sym && strcmp(sigreturn_sym, name) == 0) {
  77. sigreturn_addr = sym.st_value;
  78. }
  79. if (rt_sigreturn_sym && strcmp(rt_sigreturn_sym, name) == 0) {
  80. rt_sigreturn_addr = sym.st_value;
  81. }
  82. }
  83. }
  84. static void elfN(bswap_ps_hdrs)(ElfN(Ehdr) *ehdr)
  85. {
  86. ElfN(Phdr) *phdr = (void *)ehdr + ehdr->e_phoff;
  87. ElfN(Shdr) *shdr = (void *)ehdr + ehdr->e_shoff;
  88. ElfN(Half) i;
  89. for (i = 0; i < ehdr->e_phnum; ++i) {
  90. elfN(bswap_phdr)(&phdr[i]);
  91. }
  92. for (i = 0; i < ehdr->e_shnum; ++i) {
  93. elfN(bswap_shdr)(&shdr[i]);
  94. }
  95. }
  96. static void elfN(process)(FILE *outf, void *buf, long len, bool need_bswap)
  97. {
  98. ElfN(Ehdr) *ehdr = buf;
  99. ElfN(Phdr) *phdr;
  100. ElfN(Shdr) *shdr;
  101. unsigned phnum, shnum;
  102. unsigned dynamic_ofs = 0;
  103. unsigned dynamic_addr = 0;
  104. unsigned symtab_idx = 0;
  105. unsigned dynsym_idx = 0;
  106. unsigned first_segsz = 0;
  107. int errors = 0;
  108. if (need_bswap) {
  109. elfN(bswap_ehdr)(buf);
  110. elfN(bswap_ps_hdrs)(buf);
  111. }
  112. phnum = ehdr->e_phnum;
  113. phdr = buf + ehdr->e_phoff;
  114. shnum = ehdr->e_shnum;
  115. shdr = buf + ehdr->e_shoff;
  116. for (unsigned i = 0; i < shnum; ++i) {
  117. switch (shdr[i].sh_type) {
  118. case SHT_SYMTAB:
  119. symtab_idx = i;
  120. break;
  121. case SHT_DYNSYM:
  122. dynsym_idx = i;
  123. break;
  124. }
  125. }
  126. /*
  127. * Validate the VDSO is created as we expect: that PT_PHDR,
  128. * PT_DYNAMIC, and PT_NOTE located in a writable data segment.
  129. * PHDR and DYNAMIC require relocation, and NOTE will get the
  130. * linux version number.
  131. */
  132. for (unsigned i = 0; i < phnum; ++i) {
  133. if (phdr[i].p_type != PT_LOAD) {
  134. continue;
  135. }
  136. if (first_segsz != 0) {
  137. fprintf(stderr, "Multiple LOAD segments\n");
  138. errors++;
  139. }
  140. if (phdr[i].p_offset != 0) {
  141. fprintf(stderr, "LOAD segment does not cover EHDR\n");
  142. errors++;
  143. }
  144. if (phdr[i].p_vaddr != 0) {
  145. fprintf(stderr, "LOAD segment not loaded at address 0\n");
  146. errors++;
  147. }
  148. /*
  149. * Extend the program header to cover the entire VDSO, so that
  150. * load_elf_vdso() loads everything, including section headers.
  151. *
  152. * Require that there is no .bss, since it would break this
  153. * approach.
  154. */
  155. if (phdr[i].p_filesz != phdr[i].p_memsz) {
  156. fprintf(stderr, "LOAD segment's filesz and memsz differ\n");
  157. errors++;
  158. }
  159. if (phdr[i].p_filesz > len) {
  160. fprintf(stderr, "LOAD segment is larger than the whole VDSO\n");
  161. errors++;
  162. }
  163. phdr[i].p_filesz = len;
  164. phdr[i].p_memsz = len;
  165. first_segsz = len;
  166. if (first_segsz < ehdr->e_phoff + phnum * sizeof(*phdr)) {
  167. fprintf(stderr, "LOAD segment does not cover PHDRs\n");
  168. errors++;
  169. }
  170. if ((phdr[i].p_flags & (PF_R | PF_W)) != (PF_R | PF_W)) {
  171. fprintf(stderr, "LOAD segment is not read-write\n");
  172. errors++;
  173. }
  174. }
  175. for (unsigned i = 0; i < phnum; ++i) {
  176. const char *which;
  177. switch (phdr[i].p_type) {
  178. case PT_PHDR:
  179. which = "PT_PHDR";
  180. break;
  181. case PT_NOTE:
  182. which = "PT_NOTE";
  183. break;
  184. case PT_DYNAMIC:
  185. dynamic_ofs = phdr[i].p_offset;
  186. dynamic_addr = phdr[i].p_vaddr;
  187. which = "PT_DYNAMIC";
  188. break;
  189. default:
  190. continue;
  191. }
  192. if (first_segsz < phdr[i].p_vaddr + phdr[i].p_filesz) {
  193. fprintf(stderr, "LOAD segment does not cover %s\n", which);
  194. errors++;
  195. }
  196. }
  197. if (errors) {
  198. exit(EXIT_FAILURE);
  199. }
  200. /* Relocate the program headers. */
  201. for (unsigned i = 0; i < phnum; ++i) {
  202. output_reloc(outf, buf, &phdr[i].p_vaddr);
  203. output_reloc(outf, buf, &phdr[i].p_paddr);
  204. }
  205. /* Relocate the section headers. */
  206. for (unsigned i = 0; i < shnum; ++i) {
  207. output_reloc(outf, buf, &shdr[i].sh_addr);
  208. }
  209. /* Relocate the DYNAMIC entries. */
  210. if (dynamic_addr) {
  211. ElfN(Dyn) *target_dyn = buf + dynamic_ofs;
  212. __typeof(((ElfN(Dyn) *)target_dyn)->d_tag) tag;
  213. do {
  214. ElfN(Dyn) dyn;
  215. memcpy(&dyn, target_dyn, sizeof(dyn));
  216. if (need_bswap) {
  217. elfN(bswap_dyn)(&dyn);
  218. }
  219. tag = dyn.d_tag;
  220. switch (tag) {
  221. case DT_HASH:
  222. case DT_SYMTAB:
  223. case DT_STRTAB:
  224. case DT_VERDEF:
  225. case DT_VERSYM:
  226. case DT_PLTGOT:
  227. case DT_ADDRRNGLO ... DT_ADDRRNGHI:
  228. /* These entries store an address in the entry. */
  229. output_reloc(outf, buf, &target_dyn->d_un.d_val);
  230. break;
  231. case DT_NULL:
  232. case DT_STRSZ:
  233. case DT_SONAME:
  234. case DT_DEBUG:
  235. case DT_FLAGS:
  236. case DT_FLAGS_1:
  237. case DT_SYMBOLIC:
  238. case DT_BIND_NOW:
  239. case DT_VERDEFNUM:
  240. case DT_VALRNGLO ... DT_VALRNGHI:
  241. /* These entries store an integer in the entry. */
  242. break;
  243. case DT_SYMENT:
  244. if (dyn.d_un.d_val != sizeof(ElfN(Sym))) {
  245. fprintf(stderr, "VDSO has incorrect dynamic symbol size\n");
  246. errors++;
  247. }
  248. break;
  249. case DT_REL:
  250. case DT_RELSZ:
  251. case DT_RELA:
  252. case DT_RELASZ:
  253. /*
  254. * These entries indicate that the VDSO was built incorrectly.
  255. * It should not have any real relocations.
  256. * ??? The RISC-V toolchain will emit these even when there
  257. * are no relocations. Validate zeros.
  258. */
  259. if (dyn.d_un.d_val != 0) {
  260. fprintf(stderr, "VDSO has dynamic relocations\n");
  261. errors++;
  262. }
  263. break;
  264. case DT_RELENT:
  265. case DT_RELAENT:
  266. case DT_TEXTREL:
  267. /* These entries store an integer in the entry. */
  268. /* Should not be required; see above. */
  269. break;
  270. case DT_NEEDED:
  271. case DT_VERNEED:
  272. case DT_PLTREL:
  273. case DT_JMPREL:
  274. case DT_RPATH:
  275. case DT_RUNPATH:
  276. fprintf(stderr, "VDSO has external dependencies\n");
  277. errors++;
  278. break;
  279. case PT_LOPROC + 3:
  280. if (ehdr->e_machine == EM_PPC64) {
  281. break; /* DT_PPC64_OPT: integer bitmask */
  282. }
  283. goto do_default;
  284. default:
  285. do_default:
  286. /* This is probably something target specific. */
  287. fprintf(stderr, "VDSO has unknown DYNAMIC entry (%lx)\n",
  288. (unsigned long)tag);
  289. errors++;
  290. break;
  291. }
  292. target_dyn++;
  293. } while (tag != DT_NULL);
  294. if (errors) {
  295. exit(EXIT_FAILURE);
  296. }
  297. }
  298. /* Relocate the dynamic symbol table. */
  299. if (dynsym_idx) {
  300. ElfN(Sym) *target_sym = buf + shdr[dynsym_idx].sh_offset;
  301. unsigned sym_n = shdr[dynsym_idx].sh_size / sizeof(*target_sym);
  302. for (unsigned i = 0; i < sym_n; ++i) {
  303. output_reloc(outf, buf, &target_sym[i].st_value);
  304. }
  305. }
  306. /* Search both dynsym and symtab for the signal return symbols. */
  307. if (dynsym_idx) {
  308. elfN(search_symtab)(shdr, dynsym_idx, buf, need_bswap);
  309. }
  310. if (symtab_idx) {
  311. elfN(search_symtab)(shdr, symtab_idx, buf, need_bswap);
  312. }
  313. if (need_bswap) {
  314. elfN(bswap_ps_hdrs)(buf);
  315. elfN(bswap_ehdr)(buf);
  316. }
  317. }