Do section relocation in architecture backend
[tinycc.git] / i386-link.c
blobb33882fabb054ae94af6b3665ba7645f27a9addc
1 #include "tcc.h"
2 #define HAVE_SECTION_RELOC
4 static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
6 void relocate_init(Section *sr)
8 qrel = (ElfW_Rel *) sr->data;
11 void relocate(TCCState *s1, ElfW_Rel *rel, int type, char *ptr, addr_t addr, addr_t val)
13 int sym_index, esym_index;
15 sym_index = ELFW(R_SYM)(rel->r_info);
17 switch (type) {
18 case R_386_32:
19 if (s1->output_type == TCC_OUTPUT_DLL) {
20 esym_index = s1->symtab_to_dynsym[sym_index];
21 qrel->r_offset = rel->r_offset;
22 if (esym_index) {
23 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
24 qrel++;
25 return;
26 } else {
27 qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
28 qrel++;
31 add32le(ptr, val);
32 return;
33 case R_386_PC32:
34 if (s1->output_type == TCC_OUTPUT_DLL) {
35 /* DLL relocation */
36 esym_index = s1->symtab_to_dynsym[sym_index];
37 if (esym_index) {
38 qrel->r_offset = rel->r_offset;
39 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
40 qrel++;
41 return;
44 add32le(ptr, val - addr);
45 return;
46 case R_386_PLT32:
47 add32le(ptr, val - addr);
48 return;
49 case R_386_GLOB_DAT:
50 case R_386_JMP_SLOT:
51 write32le(ptr, val);
52 return;
53 case R_386_GOTPC:
54 add32le(ptr, s1->got->sh_addr - addr);
55 return;
56 case R_386_GOTOFF:
57 add32le(ptr, val - s1->got->sh_addr);
58 return;
59 case R_386_GOT32:
60 case R_386_GOT32X:
61 /* we load the got offset */
62 add32le(ptr, s1->sym_attrs[sym_index].got_offset);
63 return;
64 case R_386_16:
65 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
66 output_file:
67 tcc_error("can only produce 16-bit binary files");
69 write16le(ptr, read16le(ptr) + val);
70 return;
71 case R_386_PC16:
72 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
73 goto output_file;
74 write16le(ptr, read16le(ptr) + val - addr);
75 return;
76 case R_386_RELATIVE:
77 /* do nothing */
78 return;
79 case R_386_COPY:
80 /* This reloction must copy initialized data from the library
81 to the program .bss segment. Currently made like for ARM
82 (to remove noise of defaukt case). Is this true?
84 return;
85 default:
86 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
87 type, (unsigned)addr, ptr, (unsigned)val);
88 return;