elf-x86-x32.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * ELF object format helpers - x86:x32
  3. *
  4. * Copyright (C) 2012 Michael Urman and H.J. Lu
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <util.h>
  28. #include <libyasm.h>
  29. #define YASM_OBJFMT_ELF_INTERNAL
  30. #include "elf.h"
  31. #include "elf-machine.h"
  32. static elf_machine_ssym elf_x86_x32_ssyms[] = {
  33. {"plt", ELF_SSYM_SYM_RELATIVE, R_X86_64_PLT32, 32},
  34. {"gotpcrel", ELF_SSYM_SYM_RELATIVE, R_X86_64_GOTPCREL, 32},
  35. {"tlsgd", ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
  36. R_X86_64_TLSGD, 32},
  37. {"tlsld", ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
  38. R_X86_64_TLSLD, 32},
  39. {"gottpoff", ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
  40. R_X86_64_GOTTPOFF, 32},
  41. {"tpoff", ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
  42. R_X86_64_TPOFF32, 32},
  43. {"dtpoff", ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
  44. R_X86_64_DTPOFF32, 32},
  45. {"got", ELF_SSYM_SYM_RELATIVE, R_X86_64_GOT32, 32},
  46. {"tlsdesc", ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
  47. R_X86_64_GOTPC32_TLSDESC, 32},
  48. {"tlscall", ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
  49. R_X86_64_TLSDESC_CALL, 32}
  50. };
  51. static int
  52. elf_x86_x32_accepts_reloc(size_t val, yasm_symrec *wrt)
  53. {
  54. if (wrt) {
  55. const elf_machine_ssym *ssym = (elf_machine_ssym *)
  56. yasm_symrec_get_data(wrt, &elf_ssym_symrec_data);
  57. if (!ssym || val != ssym->size)
  58. return 0;
  59. return 1;
  60. }
  61. return (val&(val-1)) ? 0 : ((val & (8|16|32)) != 0);
  62. }
  63. static void
  64. elf_x86_x32_write_symtab_entry(unsigned char *bufp,
  65. elf_symtab_entry *entry,
  66. yasm_intnum *value_intn,
  67. yasm_intnum *size_intn)
  68. {
  69. YASM_WRITE_32_L(bufp, entry->name ? entry->name->index : 0);
  70. YASM_WRITE_32I_L(bufp, value_intn);
  71. YASM_WRITE_32I_L(bufp, size_intn);
  72. YASM_WRITE_8(bufp, ELF32_ST_INFO(entry->bind, entry->type));
  73. YASM_WRITE_8(bufp, ELF32_ST_OTHER(entry->vis));
  74. if (entry->sect) {
  75. elf_secthead *shead =
  76. yasm_section_get_data(entry->sect, &elf_section_data);
  77. if (!shead)
  78. yasm_internal_error(N_("symbol references section without data"));
  79. YASM_WRITE_16_L(bufp, shead->index);
  80. } else {
  81. YASM_WRITE_16_L(bufp, entry->index);
  82. }
  83. }
  84. static void
  85. elf_x86_x32_write_secthead(unsigned char *bufp, elf_secthead *shead)
  86. {
  87. YASM_WRITE_32_L(bufp, shead->name ? shead->name->index : 0);
  88. YASM_WRITE_32_L(bufp, shead->type);
  89. YASM_WRITE_32_L(bufp, shead->flags);
  90. YASM_WRITE_32_L(bufp, 0); /* vmem address */
  91. YASM_WRITE_32_L(bufp, shead->offset);
  92. YASM_WRITE_32I_L(bufp, shead->size);
  93. YASM_WRITE_32_L(bufp, shead->link);
  94. YASM_WRITE_32_L(bufp, shead->info);
  95. YASM_WRITE_32_L(bufp, shead->align);
  96. YASM_WRITE_32_L(bufp, shead->entsize);
  97. }
  98. static void
  99. elf_x86_x32_write_secthead_rel(unsigned char *bufp,
  100. elf_secthead *shead,
  101. elf_section_index symtab_idx,
  102. elf_section_index sindex)
  103. {
  104. yasm_intnum *nreloc;
  105. yasm_intnum *relocsize;
  106. YASM_WRITE_32_L(bufp, shead->rel_name ? shead->rel_name->index : 0);
  107. YASM_WRITE_32_L(bufp, SHT_RELA);
  108. YASM_WRITE_32_L(bufp, 0);
  109. YASM_WRITE_32_L(bufp, 0);
  110. YASM_WRITE_32_L(bufp, shead->rel_offset);
  111. nreloc = yasm_intnum_create_uint(shead->nreloc);
  112. relocsize = yasm_intnum_create_uint(RELOC32A_SIZE);
  113. yasm_intnum_calc(relocsize, YASM_EXPR_MUL, nreloc);
  114. YASM_WRITE_32I_L(bufp, relocsize); /* size */
  115. yasm_intnum_destroy(nreloc);
  116. yasm_intnum_destroy(relocsize);
  117. YASM_WRITE_32_L(bufp, symtab_idx); /* link: symtab index */
  118. YASM_WRITE_32_L(bufp, shead->index); /* info: relocated's index */
  119. YASM_WRITE_32_L(bufp, RELOC32_ALIGN); /* align */
  120. YASM_WRITE_32_L(bufp, RELOC32A_SIZE); /* entity size */
  121. }
  122. static void
  123. elf_x86_x32_handle_reloc_addend(yasm_intnum *intn,
  124. elf_reloc_entry *reloc,
  125. unsigned long offset)
  126. {
  127. /* .rela: copy value out as addend, replace original with 0 */
  128. reloc->addend = yasm_intnum_copy(intn);
  129. yasm_intnum_zero(intn);
  130. }
  131. static unsigned int
  132. elf_x86_x32_map_reloc_info_to_type(elf_reloc_entry *reloc)
  133. {
  134. if (reloc->wrt) {
  135. const elf_machine_ssym *ssym = (elf_machine_ssym *)
  136. yasm_symrec_get_data(reloc->wrt, &elf_ssym_symrec_data);
  137. if (!ssym || reloc->valsize != ssym->size)
  138. yasm_internal_error(N_("Unsupported WRT"));
  139. /* Force TLS type; this is required by the linker. */
  140. if (ssym->sym_rel & ELF_SSYM_THREAD_LOCAL) {
  141. elf_symtab_entry *esym;
  142. esym = yasm_symrec_get_data(reloc->reloc.sym, &elf_symrec_data);
  143. if (esym)
  144. esym->type = STT_TLS;
  145. }
  146. /* Map PC-relative GOT to appropriate relocation */
  147. if (reloc->rtype_rel && ssym->reloc == R_X86_64_GOT32)
  148. return (unsigned char) R_X86_64_GOTPCREL;
  149. return (unsigned char) ssym->reloc;
  150. } else if (reloc->is_GOT_sym && reloc->valsize == 32) {
  151. return (unsigned char) R_X86_64_GOTPC32;
  152. } else if (reloc->is_GOT_sym && reloc->valsize == 64) {
  153. yasm_internal_error(N_("Unsupported relocation size"));
  154. } else if (reloc->rtype_rel) {
  155. switch (reloc->valsize) {
  156. case 8: return (unsigned char) R_X86_64_PC8;
  157. case 16: return (unsigned char) R_X86_64_PC16;
  158. case 32: return (unsigned char) R_X86_64_PC32;
  159. default: yasm_internal_error(N_("Unsupported relocation size"));
  160. }
  161. } else {
  162. switch (reloc->valsize) {
  163. case 8: return (unsigned char) R_X86_64_8;
  164. case 16: return (unsigned char) R_X86_64_16;
  165. case 32: return (unsigned char) R_X86_64_32;
  166. case 64: return (unsigned char) R_X86_64_64;
  167. default: yasm_internal_error(N_("Unsupported relocation size"));
  168. }
  169. }
  170. return 0;
  171. }
  172. static void
  173. elf_x86_x32_write_reloc(unsigned char *bufp, elf_reloc_entry *reloc,
  174. unsigned int r_type, unsigned int r_sym)
  175. {
  176. YASM_WRITE_32I_L(bufp, reloc->reloc.addr);
  177. YASM_WRITE_32_L(bufp, ELF32_R_INFO((unsigned long)r_sym, (unsigned char)r_type));
  178. if (reloc->addend)
  179. YASM_WRITE_32I_L(bufp, reloc->addend);
  180. else {
  181. YASM_WRITE_32_L(bufp, 0);
  182. }
  183. }
  184. static void
  185. elf_x86_x32_write_proghead(unsigned char **bufpp,
  186. elf_offset secthead_addr,
  187. unsigned long secthead_count,
  188. elf_section_index shstrtab_index)
  189. {
  190. unsigned char *bufp = *bufpp;
  191. unsigned char *buf = bufp-4;
  192. YASM_WRITE_8(bufp, ELFCLASS32); /* elf class */
  193. YASM_WRITE_8(bufp, ELFDATA2LSB); /* data encoding :: MSB? */
  194. YASM_WRITE_8(bufp, EV_CURRENT); /* elf version */
  195. YASM_WRITE_8(bufp, ELFOSABI_SYSV); /* os/abi */
  196. YASM_WRITE_8(bufp, 0); /* SYSV v3 ABI=0 */
  197. while (bufp-buf < EI_NIDENT) /* e_ident padding */
  198. YASM_WRITE_8(bufp, 0);
  199. YASM_WRITE_16_L(bufp, ET_REL); /* e_type - object file */
  200. YASM_WRITE_16_L(bufp, EM_X86_64); /* e_machine - or others */
  201. YASM_WRITE_32_L(bufp, EV_CURRENT); /* elf version */
  202. YASM_WRITE_32_L(bufp, 0); /* e_entry */
  203. YASM_WRITE_32_L(bufp, 0); /* e_phoff */
  204. YASM_WRITE_32_L(bufp, secthead_addr); /* e_shoff secthead off */
  205. YASM_WRITE_32_L(bufp, 0); /* e_flags */
  206. YASM_WRITE_16_L(bufp, EHDR32_SIZE); /* e_ehsize */
  207. YASM_WRITE_16_L(bufp, 0); /* e_phentsize */
  208. YASM_WRITE_16_L(bufp, 0); /* e_phnum */
  209. YASM_WRITE_16_L(bufp, SHDR32_SIZE); /* e_shentsize */
  210. YASM_WRITE_16_L(bufp, secthead_count); /* e_shnum */
  211. YASM_WRITE_16_L(bufp, shstrtab_index); /* e_shstrndx */
  212. *bufpp = bufp;
  213. }
  214. const elf_machine_handler
  215. elf_machine_handler_x86_x32 = {
  216. "x86", "x32", ".rela",
  217. SYMTAB32_SIZE, SYMTAB32_ALIGN, RELOC32A_SIZE, SHDR32_SIZE, EHDR32_SIZE,
  218. elf_x86_x32_accepts_reloc,
  219. elf_x86_x32_write_symtab_entry,
  220. elf_x86_x32_write_secthead,
  221. elf_x86_x32_write_secthead_rel,
  222. elf_x86_x32_handle_reloc_addend,
  223. elf_x86_x32_map_reloc_info_to_type,
  224. elf_x86_x32_write_reloc,
  225. elf_x86_x32_write_proghead,
  226. elf_x86_x32_ssyms,
  227. sizeof(elf_x86_x32_ssyms)/sizeof(elf_x86_x32_ssyms[0]),
  228. 32
  229. };