x86_64-link.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #ifdef TARGET_DEFS_ONLY
  2. #define EM_TCC_TARGET EM_X86_64
  3. /* relocation type for 32 bit data relocation */
  4. #define R_DATA_32 R_X86_64_32S
  5. #define R_DATA_PTR R_X86_64_64
  6. #define R_JMP_SLOT R_X86_64_JUMP_SLOT
  7. #define R_GLOB_DAT R_X86_64_GLOB_DAT
  8. #define R_COPY R_X86_64_COPY
  9. #define R_RELATIVE R_X86_64_RELATIVE
  10. #define R_NUM R_X86_64_NUM
  11. #define ELF_START_ADDR 0x400000
  12. #define ELF_PAGE_SIZE 0x200000
  13. #define PCRELATIVE_DLLPLT 1
  14. #define RELOCATE_DLLPLT 1
  15. #else /* !TARGET_DEFS_ONLY */
  16. #include "tcc.h"
  17. /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
  18. relocations, returns -1. */
  19. int code_reloc (int reloc_type)
  20. {
  21. switch (reloc_type) {
  22. case R_X86_64_32:
  23. case R_X86_64_32S:
  24. case R_X86_64_64:
  25. case R_X86_64_GOTPC32:
  26. case R_X86_64_GOTPC64:
  27. case R_X86_64_GOTPCREL:
  28. case R_X86_64_GOTPCRELX:
  29. case R_X86_64_REX_GOTPCRELX:
  30. case R_X86_64_GOTTPOFF:
  31. case R_X86_64_GOT32:
  32. case R_X86_64_GOT64:
  33. case R_X86_64_GLOB_DAT:
  34. case R_X86_64_COPY:
  35. case R_X86_64_RELATIVE:
  36. case R_X86_64_GOTOFF64:
  37. return 0;
  38. case R_X86_64_PC32:
  39. case R_X86_64_PC64:
  40. case R_X86_64_PLT32:
  41. case R_X86_64_PLTOFF64:
  42. case R_X86_64_JUMP_SLOT:
  43. return 1;
  44. }
  45. tcc_error ("Unknown relocation type: %d", reloc_type);
  46. return -1;
  47. }
  48. /* Returns an enumerator to describe whether and when the relocation needs a
  49. GOT and/or PLT entry to be created. See tcc.h for a description of the
  50. different values. */
  51. int gotplt_entry_type (int reloc_type)
  52. {
  53. switch (reloc_type) {
  54. case R_X86_64_GLOB_DAT:
  55. case R_X86_64_JUMP_SLOT:
  56. case R_X86_64_COPY:
  57. case R_X86_64_RELATIVE:
  58. return NO_GOTPLT_ENTRY;
  59. /* The following relocs wouldn't normally need GOT or PLT
  60. slots, but we need them for simplicity in the link
  61. editor part. See our caller for comments. */
  62. case R_X86_64_32:
  63. case R_X86_64_32S:
  64. case R_X86_64_64:
  65. case R_X86_64_PC32:
  66. case R_X86_64_PC64:
  67. return AUTO_GOTPLT_ENTRY;
  68. case R_X86_64_GOTTPOFF:
  69. return BUILD_GOT_ONLY;
  70. case R_X86_64_GOT32:
  71. case R_X86_64_GOT64:
  72. case R_X86_64_GOTPC32:
  73. case R_X86_64_GOTPC64:
  74. case R_X86_64_GOTOFF64:
  75. case R_X86_64_GOTPCREL:
  76. case R_X86_64_GOTPCRELX:
  77. case R_X86_64_REX_GOTPCRELX:
  78. case R_X86_64_PLT32:
  79. case R_X86_64_PLTOFF64:
  80. return ALWAYS_GOTPLT_ENTRY;
  81. }
  82. tcc_error ("Unknown relocation type: %d", reloc_type);
  83. return -1;
  84. }
  85. ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
  86. {
  87. Section *plt = s1->plt;
  88. uint8_t *p;
  89. int modrm;
  90. unsigned plt_offset, relofs;
  91. modrm = 0x25;
  92. /* empty PLT: create PLT0 entry that pushes the library identifier
  93. (GOT + PTR_SIZE) and jumps to ld.so resolution routine
  94. (GOT + 2 * PTR_SIZE) */
  95. if (plt->data_offset == 0) {
  96. p = section_ptr_add(plt, 16);
  97. p[0] = 0xff; /* pushl got + PTR_SIZE */
  98. p[1] = modrm + 0x10;
  99. write32le(p + 2, PTR_SIZE);
  100. p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
  101. p[7] = modrm;
  102. write32le(p + 8, PTR_SIZE * 2);
  103. }
  104. plt_offset = plt->data_offset;
  105. /* The PLT slot refers to the relocation entry it needs via offset.
  106. The reloc entry is created below, so its offset is the current
  107. data_offset */
  108. relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
  109. /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
  110. p = section_ptr_add(plt, 16);
  111. p[0] = 0xff; /* jmp *(got + x) */
  112. p[1] = modrm;
  113. write32le(p + 2, got_offset);
  114. p[6] = 0x68; /* push $xxx */
  115. /* On x86-64, the relocation is referred to by _index_ */
  116. write32le(p + 7, relofs / sizeof (ElfW_Rel));
  117. p[11] = 0xe9; /* jmp plt_start */
  118. write32le(p + 12, -(plt->data_offset));
  119. return plt_offset;
  120. }
  121. /* relocate the PLT: compute addresses and offsets in the PLT now that final
  122. address for PLT and GOT are known (see fill_program_header) */
  123. ST_FUNC void relocate_plt(TCCState *s1)
  124. {
  125. uint8_t *p, *p_end;
  126. if (!s1->plt)
  127. return;
  128. p = s1->plt->data;
  129. p_end = p + s1->plt->data_offset;
  130. if (p < p_end) {
  131. int x = s1->got->sh_addr - s1->plt->sh_addr - 6;
  132. add32le(p + 2, x);
  133. add32le(p + 8, x - 6);
  134. p += 16;
  135. while (p < p_end) {
  136. add32le(p + 2, x + s1->plt->data - p);
  137. p += 16;
  138. }
  139. }
  140. }
  141. static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
  142. void relocate_init(Section *sr)
  143. {
  144. qrel = (ElfW_Rel *) sr->data;
  145. }
  146. void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
  147. {
  148. int sym_index, esym_index;
  149. sym_index = ELFW(R_SYM)(rel->r_info);
  150. switch (type) {
  151. case R_X86_64_64:
  152. if (s1->output_type == TCC_OUTPUT_DLL) {
  153. esym_index = s1->sym_attrs[sym_index].dyn_index;
  154. qrel->r_offset = rel->r_offset;
  155. if (esym_index) {
  156. qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_64);
  157. qrel->r_addend = rel->r_addend;
  158. qrel++;
  159. break;
  160. } else {
  161. qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
  162. qrel->r_addend = read64le(ptr) + val;
  163. qrel++;
  164. }
  165. }
  166. add64le(ptr, val);
  167. break;
  168. case R_X86_64_32:
  169. case R_X86_64_32S:
  170. if (s1->output_type == TCC_OUTPUT_DLL) {
  171. /* XXX: this logic may depend on TCC's codegen
  172. now TCC uses R_X86_64_32 even for a 64bit pointer */
  173. qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
  174. /* Use sign extension! */
  175. qrel->r_addend = (int)read32le(ptr) + val;
  176. qrel++;
  177. }
  178. add32le(ptr, val);
  179. break;
  180. case R_X86_64_PC32:
  181. if (s1->output_type == TCC_OUTPUT_DLL) {
  182. /* DLL relocation */
  183. esym_index = s1->sym_attrs[sym_index].dyn_index;
  184. if (esym_index) {
  185. qrel->r_offset = rel->r_offset;
  186. qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32);
  187. /* Use sign extension! */
  188. qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
  189. qrel++;
  190. break;
  191. }
  192. }
  193. goto plt32pc32;
  194. case R_X86_64_PLT32:
  195. /* fallthrough: val already holds the PLT slot address */
  196. plt32pc32:
  197. {
  198. long long diff;
  199. diff = (long long)val - addr;
  200. if (diff < -2147483648LL || diff > 2147483647LL) {
  201. tcc_error("internal error: relocation failed");
  202. }
  203. add32le(ptr, diff);
  204. }
  205. break;
  206. case R_X86_64_PLTOFF64:
  207. add64le(ptr, val - s1->got->sh_addr + rel->r_addend);
  208. break;
  209. case R_X86_64_PC64:
  210. if (s1->output_type == TCC_OUTPUT_DLL) {
  211. /* DLL relocation */
  212. esym_index = s1->sym_attrs[sym_index].dyn_index;
  213. if (esym_index) {
  214. qrel->r_offset = rel->r_offset;
  215. qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC64);
  216. qrel->r_addend = read64le(ptr) + rel->r_addend;
  217. qrel++;
  218. break;
  219. }
  220. }
  221. add64le(ptr, val - addr);
  222. break;
  223. case R_X86_64_GLOB_DAT:
  224. case R_X86_64_JUMP_SLOT:
  225. /* They don't need addend */
  226. write64le(ptr, val - rel->r_addend);
  227. break;
  228. case R_X86_64_GOTPCREL:
  229. case R_X86_64_GOTPCRELX:
  230. case R_X86_64_REX_GOTPCRELX:
  231. add32le(ptr, s1->got->sh_addr - addr +
  232. s1->sym_attrs[sym_index].got_offset - 4);
  233. break;
  234. case R_X86_64_GOTPC32:
  235. add32le(ptr, s1->got->sh_addr - addr + rel->r_addend);
  236. break;
  237. case R_X86_64_GOTPC64:
  238. add64le(ptr, s1->got->sh_addr - addr + rel->r_addend);
  239. break;
  240. case R_X86_64_GOTTPOFF:
  241. add32le(ptr, val - s1->got->sh_addr);
  242. break;
  243. case R_X86_64_GOT32:
  244. /* we load the got offset */
  245. add32le(ptr, s1->sym_attrs[sym_index].got_offset);
  246. break;
  247. case R_X86_64_GOT64:
  248. /* we load the got offset */
  249. add64le(ptr, s1->sym_attrs[sym_index].got_offset);
  250. break;
  251. case R_X86_64_GOTOFF64:
  252. add64le(ptr, val - s1->got->sh_addr);
  253. break;
  254. case R_X86_64_RELATIVE:
  255. /* do nothing */
  256. break;
  257. }
  258. }
  259. #endif /* !TARGET_DEFS_ONLY */