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