Fix relocs_info declaration in tcc.h
[tinycc.git] / i386-link.c
blobc9724816fc1f665d01487d95557f247591a13154
1 #ifdef TARGET_DEFS_ONLY
3 #define EM_TCC_TARGET EM_386
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
12 #define R_NUM R_386_NUM
14 #define ELF_START_ADDR 0x08048000
15 #define ELF_PAGE_SIZE 0x1000
17 #define HAVE_SECTION_RELOC
19 #else /* !TARGET_DEFS_ONLY */
21 #include "tcc.h"
23 static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
24 ST_DATA struct reloc_info relocs_info[R_NUM] = {
25 INIT_RELOC_INFO (R_386_32, 0, NO_GOTPLT_ENTRY, 0)
26 INIT_RELOC_INFO (R_386_PC32, 1, AUTO_GOTPLT_ENTRY, 0)
27 INIT_RELOC_INFO (R_386_PLT32, 1, ALWAYS_GOTPLT_ENTRY, 0)
28 INIT_RELOC_INFO (R_386_GLOB_DAT, 0, NO_GOTPLT_ENTRY, 0)
29 INIT_RELOC_INFO (R_386_JMP_SLOT, 1, NO_GOTPLT_ENTRY, 0)
30 INIT_RELOC_INFO (R_386_GOTPC, 0, BUILD_GOT_ONLY, 0)
31 INIT_RELOC_INFO (R_386_GOTOFF, 0, BUILD_GOT_ONLY, 0)
32 INIT_RELOC_INFO (R_386_GOT32, 0, ALWAYS_GOTPLT_ENTRY, 0)
33 INIT_RELOC_INFO (R_386_GOT32X, 0, ALWAYS_GOTPLT_ENTRY, 0)
34 INIT_RELOC_INFO (R_386_16, 0, NO_GOTPLT_ENTRY, 0)
35 INIT_RELOC_INFO (R_386_PC16, 1, AUTO_GOTPLT_ENTRY, 0)
38 void relocate_init(Section *sr)
40 qrel = (ElfW_Rel *) sr->data;
43 void relocate(TCCState *s1, ElfW_Rel *rel, int type, char *ptr, addr_t addr, addr_t val)
45 int sym_index, esym_index;
47 sym_index = ELFW(R_SYM)(rel->r_info);
49 switch (type) {
50 case R_386_32:
51 if (s1->output_type == TCC_OUTPUT_DLL) {
52 esym_index = s1->symtab_to_dynsym[sym_index];
53 qrel->r_offset = rel->r_offset;
54 if (esym_index) {
55 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
56 qrel++;
57 return;
58 } else {
59 qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
60 qrel++;
63 add32le(ptr, val);
64 return;
65 case R_386_PC32:
66 if (s1->output_type == TCC_OUTPUT_DLL) {
67 /* DLL relocation */
68 esym_index = s1->symtab_to_dynsym[sym_index];
69 if (esym_index) {
70 qrel->r_offset = rel->r_offset;
71 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
72 qrel++;
73 return;
76 add32le(ptr, val - addr);
77 return;
78 case R_386_PLT32:
79 add32le(ptr, val - addr);
80 return;
81 case R_386_GLOB_DAT:
82 case R_386_JMP_SLOT:
83 write32le(ptr, val);
84 return;
85 case R_386_GOTPC:
86 add32le(ptr, s1->got->sh_addr - addr);
87 return;
88 case R_386_GOTOFF:
89 add32le(ptr, val - s1->got->sh_addr);
90 return;
91 case R_386_GOT32:
92 case R_386_GOT32X:
93 /* we load the got offset */
94 add32le(ptr, s1->sym_attrs[sym_index].got_offset);
95 return;
96 case R_386_16:
97 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
98 output_file:
99 tcc_error("can only produce 16-bit binary files");
101 write16le(ptr, read16le(ptr) + val);
102 return;
103 case R_386_PC16:
104 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
105 goto output_file;
106 write16le(ptr, read16le(ptr) + val - addr);
107 return;
108 case R_386_RELATIVE:
109 /* do nothing */
110 return;
111 case R_386_COPY:
112 /* This reloction must copy initialized data from the library
113 to the program .bss segment. Currently made like for ARM
114 (to remove noise of defaukt case). Is this true?
116 return;
117 default:
118 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
119 type, (unsigned)addr, ptr, (unsigned)val);
120 return;
124 #endif /* !TARGET_DEFS_ONLY */