i386-link.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #ifdef TARGET_DEFS_ONLY
  2. #define EM_TCC_TARGET EM_386
  3. /* relocation type for 32 bit data relocation */
  4. #define R_DATA_32 R_386_32
  5. #define R_DATA_PTR R_386_32
  6. #define R_JMP_SLOT R_386_JMP_SLOT
  7. #define R_GLOB_DAT R_386_GLOB_DAT
  8. #define R_COPY R_386_COPY
  9. #define R_RELATIVE R_386_RELATIVE
  10. #define R_NUM R_386_NUM
  11. #define ELF_START_ADDR 0x08048000
  12. #define ELF_PAGE_SIZE 0x1000
  13. #define PCRELATIVE_DLLPLT 0
  14. #define RELOCATE_DLLPLT 0
  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_386_RELATIVE:
  23. case R_386_16:
  24. case R_386_32:
  25. case R_386_GOTPC:
  26. case R_386_GOTOFF:
  27. case R_386_GOT32:
  28. case R_386_GOT32X:
  29. case R_386_GLOB_DAT:
  30. case R_386_COPY:
  31. return 0;
  32. case R_386_PC16:
  33. case R_386_PC32:
  34. case R_386_PLT32:
  35. case R_386_JMP_SLOT:
  36. return 1;
  37. }
  38. tcc_error ("Unknown relocation type: %d", reloc_type);
  39. return -1;
  40. }
  41. /* Returns an enumerator to describe whether and when the relocation needs a
  42. GOT and/or PLT entry to be created. See tcc.h for a description of the
  43. different values. */
  44. int gotplt_entry_type (int reloc_type)
  45. {
  46. switch (reloc_type) {
  47. case R_386_RELATIVE:
  48. case R_386_16:
  49. case R_386_GLOB_DAT:
  50. case R_386_JMP_SLOT:
  51. case R_386_COPY:
  52. return NO_GOTPLT_ENTRY;
  53. case R_386_32:
  54. /* This relocations shouldn't normally need GOT or PLT
  55. slots if it weren't for simplicity in the code generator.
  56. See our caller for comments. */
  57. return AUTO_GOTPLT_ENTRY;
  58. case R_386_PC16:
  59. case R_386_PC32:
  60. return AUTO_GOTPLT_ENTRY;
  61. case R_386_GOTPC:
  62. case R_386_GOTOFF:
  63. return BUILD_GOT_ONLY;
  64. case R_386_GOT32:
  65. case R_386_GOT32X:
  66. case R_386_PLT32:
  67. return ALWAYS_GOTPLT_ENTRY;
  68. }
  69. tcc_error ("Unknown relocation type: %d", reloc_type);
  70. return -1;
  71. }
  72. ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
  73. {
  74. Section *plt = s1->plt;
  75. uint8_t *p;
  76. int modrm;
  77. unsigned plt_offset, relofs;
  78. /* on i386 if we build a DLL, we add a %ebx offset */
  79. if (s1->output_type == TCC_OUTPUT_DLL)
  80. modrm = 0xa3;
  81. else
  82. modrm = 0x25;
  83. /* empty PLT: create PLT0 entry that pushes the library identifier
  84. (GOT + PTR_SIZE) and jumps to ld.so resolution routine
  85. (GOT + 2 * PTR_SIZE) */
  86. if (plt->data_offset == 0) {
  87. p = section_ptr_add(plt, 16);
  88. p[0] = 0xff; /* pushl got + PTR_SIZE */
  89. p[1] = modrm + 0x10;
  90. write32le(p + 2, PTR_SIZE);
  91. p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
  92. p[7] = modrm;
  93. write32le(p + 8, PTR_SIZE * 2);
  94. }
  95. plt_offset = plt->data_offset;
  96. /* The PLT slot refers to the relocation entry it needs via offset.
  97. The reloc entry is created below, so its offset is the current
  98. data_offset */
  99. relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
  100. /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
  101. p = section_ptr_add(plt, 16);
  102. p[0] = 0xff; /* jmp *(got + x) */
  103. p[1] = modrm;
  104. write32le(p + 2, got_offset);
  105. p[6] = 0x68; /* push $xxx */
  106. write32le(p + 7, relofs);
  107. p[11] = 0xe9; /* jmp plt_start */
  108. write32le(p + 12, -(plt->data_offset));
  109. return plt_offset;
  110. }
  111. /* relocate the PLT: compute addresses and offsets in the PLT now that final
  112. address for PLT and GOT are known (see fill_program_header) */
  113. ST_FUNC void relocate_plt(TCCState *s1)
  114. {
  115. uint8_t *p, *p_end;
  116. if (!s1->plt)
  117. return;
  118. p = s1->plt->data;
  119. p_end = p + s1->plt->data_offset;
  120. if (p < p_end) {
  121. add32le(p + 2, s1->got->sh_addr);
  122. add32le(p + 8, s1->got->sh_addr);
  123. p += 16;
  124. while (p < p_end) {
  125. add32le(p + 2, s1->got->sh_addr);
  126. p += 16;
  127. }
  128. }
  129. }
  130. static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
  131. void relocate_init(Section *sr)
  132. {
  133. qrel = (ElfW_Rel *) sr->data;
  134. }
  135. void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
  136. {
  137. int sym_index, esym_index;
  138. sym_index = ELFW(R_SYM)(rel->r_info);
  139. switch (type) {
  140. case R_386_32:
  141. if (s1->output_type == TCC_OUTPUT_DLL) {
  142. esym_index = s1->sym_attrs[sym_index].dyn_index;
  143. qrel->r_offset = rel->r_offset;
  144. if (esym_index) {
  145. qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
  146. qrel++;
  147. return;
  148. } else {
  149. qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
  150. qrel++;
  151. }
  152. }
  153. add32le(ptr, val);
  154. return;
  155. case R_386_PC32:
  156. if (s1->output_type == TCC_OUTPUT_DLL) {
  157. /* DLL relocation */
  158. esym_index = s1->sym_attrs[sym_index].dyn_index;
  159. if (esym_index) {
  160. qrel->r_offset = rel->r_offset;
  161. qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
  162. qrel++;
  163. return;
  164. }
  165. }
  166. add32le(ptr, val - addr);
  167. return;
  168. case R_386_PLT32:
  169. add32le(ptr, val - addr);
  170. return;
  171. case R_386_GLOB_DAT:
  172. case R_386_JMP_SLOT:
  173. write32le(ptr, val);
  174. return;
  175. case R_386_GOTPC:
  176. add32le(ptr, s1->got->sh_addr - addr);
  177. return;
  178. case R_386_GOTOFF:
  179. add32le(ptr, val - s1->got->sh_addr);
  180. return;
  181. case R_386_GOT32:
  182. case R_386_GOT32X:
  183. /* we load the got offset */
  184. add32le(ptr, s1->sym_attrs[sym_index].got_offset);
  185. return;
  186. case R_386_16:
  187. if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
  188. output_file:
  189. tcc_error("can only produce 16-bit binary files");
  190. }
  191. write16le(ptr, read16le(ptr) + val);
  192. return;
  193. case R_386_PC16:
  194. if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
  195. goto output_file;
  196. write16le(ptr, read16le(ptr) + val - addr);
  197. return;
  198. case R_386_RELATIVE:
  199. #ifdef TCC_TARGET_PE
  200. add32le(ptr, val - s1->pe_imagebase);
  201. #endif
  202. /* do nothing */
  203. return;
  204. case R_386_COPY:
  205. /* This relocation must copy initialized data from the library
  206. to the program .bss segment. Currently made like for ARM
  207. (to remove noise of default case). Is this true?
  208. */
  209. return;
  210. default:
  211. fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
  212. type, (unsigned)addr, ptr, (unsigned)val);
  213. return;
  214. }
  215. }
  216. #endif /* !TARGET_DEFS_ONLY */