Rev 145 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 145 | Rev 158 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * TCCMEOS.C - KolibriOS/MenuetOS file output for the TinyC Compiler |
2 | * TCCMEOS.C - KolibriOS/MenuetOS file output for the TinyC Compiler |
3 | * |
3 | * |
4 | * Copyright (c) 2006 Andrey Khalyavin |
4 | * Copyright (c) 2006 Andrey Khalyavin |
5 | * |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2 of the License, or (at your option) any later version. |
9 | * version 2 of the License, or (at your option) any later version. |
10 | * |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Lesser General Public License for more details. |
14 | * Lesser General Public License for more details. |
15 | * |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public |
16 | * You should have received a copy of the GNU Lesser General Public |
17 | * License along with this library; if not, write to the Free Software |
17 | * License along with this library; if not, write to the Free Software |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | */ |
19 | */ |
20 | 20 | ||
21 | typedef struct { |
21 | typedef struct { |
22 | char magic[8]; |
22 | char magic[8]; |
23 | int version; |
23 | int version; |
24 | int entry_point; |
24 | int entry_point; |
25 | int image_size; |
25 | int image_size; |
26 | int memory_size; |
26 | int memory_size; |
27 | int stack; |
27 | int stack; |
28 | int params; |
28 | int params; |
29 | int argv; |
29 | int argv; |
30 | } IMAGE_MEOS_FILE_HEADER,*PIMAGE_MEOS_FILE_HEADER; |
30 | } IMAGE_MEOS_FILE_HEADER,*PIMAGE_MEOS_FILE_HEADER; |
31 | typedef struct _meos_section_info{ |
31 | typedef struct _meos_section_info{ |
32 | int sh_addr; |
32 | int sh_addr; |
33 | void* data; |
33 | void* data; |
34 | int data_size; |
34 | int data_size; |
35 | int sec_num; |
35 | int sec_num; |
36 | struct _meos_section_info* next; |
36 | struct _meos_section_info* next; |
37 | } meos_section_info; |
37 | } meos_section_info; |
38 | typedef struct { |
38 | typedef struct { |
39 | TCCState* s1; |
39 | TCCState* s1; |
40 | IMAGE_MEOS_FILE_HEADER header; |
40 | IMAGE_MEOS_FILE_HEADER header; |
41 | meos_section_info* code_sections; |
41 | meos_section_info* code_sections; |
42 | meos_section_info* data_sections; |
42 | meos_section_info* data_sections; |
43 | meos_section_info* bss_sections; |
43 | meos_section_info* bss_sections; |
44 | } me_info; |
44 | } me_info; |
45 | 45 | ||
46 | meos_section_info* findsection(me_info* me,int num) |
46 | meos_section_info* findsection(me_info* me,int num) |
47 | { |
47 | { |
48 | meos_section_info* si; |
48 | meos_section_info* si; |
49 | for(si=me->code_sections;si;si=si->next) |
49 | for(si=me->code_sections;si;si=si->next) |
50 | { |
50 | { |
51 | if (si->sec_num==num) |
51 | if (si->sec_num==num) |
52 | return si; |
52 | return si; |
53 | } |
53 | } |
54 | for (si=me->data_sections;si;si=si->next) |
54 | for (si=me->data_sections;si;si=si->next) |
55 | { |
55 | { |
56 | if (si->sec_num==num) |
56 | if (si->sec_num==num) |
57 | return si; |
57 | return si; |
58 | } |
58 | } |
59 | for (si=me->bss_sections;si;si=si->next) |
59 | for (si=me->bss_sections;si;si=si->next) |
60 | { |
60 | { |
61 | if (si->sec_num==num) |
61 | if (si->sec_num==num) |
62 | return si; |
62 | return si; |
63 | } |
63 | } |
64 | return (meos_section_info*)0; |
64 | return (meos_section_info*)0; |
65 | } |
65 | } |
66 | 66 | ||
67 | void build_reloc(me_info* me) |
67 | void build_reloc(me_info* me) |
68 | { |
68 | { |
69 | int flag; |
69 | int flag; |
70 | Elf32_Rel *rel, *rel_, *rel_end; |
70 | Elf32_Rel *rel, *rel_, *rel_end; |
71 | Section *sr; |
71 | Section *sr; |
72 | meos_section_info* s; |
72 | meos_section_info* s; |
73 | meos_section_info* ss; |
73 | meos_section_info* ss; |
74 | s=me->code_sections; |
74 | s=me->code_sections; |
75 | rel=0; |
75 | rel=0; |
76 | rel_end=0; |
76 | rel_end=0; |
77 | flag=0; |
77 | flag=0; |
78 | for(;;) |
78 | for(;;) |
79 | { |
79 | { |
80 | sr=me->s1->sections[s->sec_num]->reloc; |
80 | sr=me->s1->sections[s->sec_num]->reloc; |
81 | if (sr) |
81 | if (sr) |
82 | { |
82 | { |
83 | rel = (Elf32_Rel *) sr->data; |
83 | rel = (Elf32_Rel *) sr->data; |
84 | rel_end = (Elf32_Rel *) (sr->data + sr->data_offset); |
84 | rel_end = (Elf32_Rel *) (sr->data + sr->data_offset); |
85 | } |
85 | } |
86 | rel_=rel; |
86 | rel_=rel; |
87 | while (rel_ |
87 | while (rel_ |
88 | rel=rel_; |
88 | rel=rel_; |
89 | int type = ELF32_R_TYPE(rel->r_info); |
89 | int type = ELF32_R_TYPE(rel->r_info); |
90 | rel_=rel+1; |
90 | rel_=rel+1; |
91 | if (type != R_386_PC32 && type != R_386_32) |
91 | if (type != R_386_PC32 && type != R_386_32) |
92 | continue; |
92 | continue; |
93 | int sym = ELF32_R_SYM(rel->r_info); |
93 | int sym = ELF32_R_SYM(rel->r_info); |
94 | if (sym>symtab_section->data_offset/sizeof(Elf32_Sym)) |
94 | if (sym>symtab_section->data_offset/sizeof(Elf32_Sym)) |
95 | continue; |
95 | continue; |
96 | Elf32_Sym* esym = ((Elf32_Sym *)symtab_section->data)+sym; |
96 | Elf32_Sym* esym = ((Elf32_Sym *)symtab_section->data)+sym; |
97 | int sect=esym->st_shndx; |
97 | int sect=esym->st_shndx; |
98 | ss=findsection(me,sect); |
98 | ss=findsection(me,sect); |
99 | if (ss==0) continue; |
99 | if (ss==0) continue; |
100 | if (rel->r_offset>s->data_size) |
100 | if (rel->r_offset>s->data_size) |
101 | continue; |
101 | continue; |
102 | if (type==R_386_PC32) |
102 | if (type==R_386_PC32) |
103 | *(int*)(rel->r_offset+s->data)+=ss->sh_addr+esym->st_value-rel->r_offset-s->sh_addr; |
103 | *(int*)(rel->r_offset+s->data)+=ss->sh_addr+esym->st_value-rel->r_offset-s->sh_addr; |
104 | else if (type==R_386_32) |
104 | else if (type==R_386_32) |
105 | *(int*)(rel->r_offset+s->data)+=ss->sh_addr+esym->st_value; |
105 | *(int*)(rel->r_offset+s->data)+=ss->sh_addr+esym->st_value; |
106 | } |
106 | } |
107 | rel=rel_; |
107 | rel=rel_; |
108 | s=s->next; |
108 | s=s->next; |
109 | if (s==0) |
109 | if (s==0) |
110 | { |
110 | { |
111 | if (flag) break; |
111 | if (flag) break; |
112 | s=me->data_sections; |
112 | s=me->data_sections; |
113 | if (s==0) break; |
113 | if (s==0) break; |
114 | flag=1; |
114 | flag=1; |
115 | continue; |
115 | continue; |
116 | } |
116 | } |
117 | } |
117 | } |
118 | } |
118 | } |
119 | 119 | ||
120 | void assign_addresses(me_info* me) |
120 | void assign_addresses(me_info* me) |
121 | { |
121 | { |
122 | int i; |
122 | int i; |
123 | meos_section_info* si; |
123 | meos_section_info* si; |
124 | for (i=1;i |
124 | for (i=1;i |
125 | { |
125 | { |
126 | Section* s=me->s1->sections[i]; |
126 | Section* s=me->s1->sections[i]; |
127 | if (strcmp(".text",s->name)==0) |
127 | if (strcmp(".text",s->name)==0) |
128 | { |
128 | { |
129 | si=tcc_malloc(sizeof(meos_section_info)); |
129 | si=tcc_malloc(sizeof(meos_section_info)); |
130 | si->data=s->data; |
130 | si->data=s->data; |
131 | si->data_size=s->data_offset; |
131 | si->data_size=s->data_offset; |
132 | si->next=me->code_sections; |
132 | si->next=me->code_sections; |
133 | si->sec_num=i; |
133 | si->sec_num=i; |
134 | me->code_sections=si; |
134 | me->code_sections=si; |
135 | continue; |
135 | continue; |
136 | } |
136 | } |
137 | if (strcmp(".data",s->name)==0) |
137 | if (strcmp(".data",s->name)==0) |
138 | { |
138 | { |
139 | si=tcc_malloc(sizeof(meos_section_info)); |
139 | si=tcc_malloc(sizeof(meos_section_info)); |
140 | si->data=s->data; |
140 | si->data=s->data; |
141 | si->data_size=s->data_offset; |
141 | si->data_size=s->data_offset; |
142 | si->next=me->data_sections; |
142 | si->next=me->data_sections; |
143 | si->sec_num=i; |
143 | si->sec_num=i; |
144 | me->data_sections=si; |
144 | me->data_sections=si; |
145 | continue; |
145 | continue; |
146 | } |
146 | } |
147 | if (strcmp(".bss",s->name)==0) |
147 | if (strcmp(".bss",s->name)==0) |
148 | { |
148 | { |
149 | si=tcc_malloc(sizeof(meos_section_info)); |
149 | si=tcc_malloc(sizeof(meos_section_info)); |
150 | si->data_size=s->data_offset; |
150 | si->data_size=s->data_offset; |
151 | si->next=me->bss_sections; |
151 | si->next=me->bss_sections; |
152 | si->sec_num=i; |
152 | si->sec_num=i; |
153 | me->bss_sections=si; |
153 | me->bss_sections=si; |
154 | continue; |
154 | continue; |
155 | } |
155 | } |
156 | } |
156 | } |
157 | int addr; |
157 | int addr; |
158 | addr=sizeof(IMAGE_MEOS_FILE_HEADER); |
158 | addr=sizeof(IMAGE_MEOS_FILE_HEADER); |
159 | for (si=me->code_sections;si;si=si->next) |
159 | for (si=me->code_sections;si;si=si->next) |
160 | { |
160 | { |
161 | si->sh_addr=addr; |
161 | si->sh_addr=addr; |
162 | addr+=si->data_size; |
162 | addr+=si->data_size; |
163 | } |
163 | } |
164 | for (si=me->data_sections;si;si=si->next) |
164 | for (si=me->data_sections;si;si=si->next) |
165 | { |
165 | { |
166 | si->sh_addr=addr; |
166 | si->sh_addr=addr; |
167 | addr+=si->data_size; |
167 | addr+=si->data_size; |
168 | } |
168 | } |
169 | me->header.image_size=addr; |
169 | me->header.image_size=addr; |
170 | for (si=me->bss_sections;si;si=si->next) |
170 | for (si=me->bss_sections;si;si=si->next) |
171 | { |
171 | { |
172 | si->sh_addr=addr; |
172 | si->sh_addr=addr; |
173 | addr+=si->data_size; |
173 | addr+=si->data_size; |
174 | } |
174 | } |
175 | addr+=4096; |
175 | addr+=4096; |
176 | addr=(addr+4)&(~3); |
176 | addr=(addr+4)&(~3); |
177 | me->header.stack=addr; |
177 | me->header.stack=addr; |
178 | me->header.memory_size=addr; |
178 | me->header.memory_size=addr; |
179 | build_reloc(me); |
179 | build_reloc(me); |
180 | } |
180 | } |
181 | int tcc_find_symbol_me(me_info* me, const char *sym_name) |
181 | int tcc_find_symbol_me(me_info* me, const char *sym_name) |
182 | { |
182 | { |
183 | int i; |
183 | int i; |
184 | int symtab; |
184 | int symtab; |
185 | int strtab; |
185 | int strtab; |
186 | symtab=0; |
186 | symtab=0; |
187 | strtab=0; |
187 | strtab=0; |
188 | for (i=1;i |
188 | for (i=1;i |
189 | { |
189 | { |
190 | Section* s; |
190 | Section* s; |
191 | s=me->s1->sections[i]; |
191 | s=me->s1->sections[i]; |
192 | if (strcmp(s->name,".symtab")==0) |
192 | if (strcmp(s->name,".symtab")==0) |
193 | { |
193 | { |
194 | symtab=i; |
194 | symtab=i; |
195 | } |
195 | } |
196 | if (strcmp(s->name,".strtab")==0) |
196 | if (strcmp(s->name,".strtab")==0) |
197 | { |
197 | { |
198 | strtab=i; |
198 | strtab=i; |
199 | } |
199 | } |
200 | } |
200 | } |
201 | if (symtab==0 || strtab==0) |
201 | if (symtab==0 || strtab==0) |
202 | return 0; |
202 | return 0; |
203 | Elf32_Sym* s,*se; |
203 | Elf32_Sym* s,*se; |
204 | char* name; |
204 | char* name; |
205 | s=(Elf32_Sym*)me->s1->sections[symtab]->data; |
205 | s=(Elf32_Sym*)me->s1->sections[symtab]->data; |
206 | se=(Elf32_Sym*)(((void*)s)+me->s1->sections[symtab]->data_offset); |
206 | se=(Elf32_Sym*)(((void*)s)+me->s1->sections[symtab]->data_offset); |
207 | name=(char*)me->s1->sections[strtab]->data; |
207 | name=(char*)me->s1->sections[strtab]->data; |
208 | while (s |
208 | while (s |
209 | { |
209 | { |
210 | if (strcmp(name+s->st_name,sym_name)==0) |
210 | if (strcmp(name+s->st_name,sym_name)==0) |
211 | { |
211 | { |
212 | return s->st_value+findsection(me,s->st_shndx)->sh_addr; |
212 | return s->st_value+findsection(me,s->st_shndx)->sh_addr; |
213 | } |
213 | } |
214 | s++; |
214 | s++; |
215 | } |
215 | } |
216 | return 0; |
216 | return 0; |
217 | } |
217 | } |
218 | const char* me_magic="MENUET01"; |
218 | const char* me_magic="MENUET01"; |
219 | int tcc_output_me(TCCState* s1,const char *filename) |
219 | int tcc_output_me(TCCState* s1,const char *filename) |
220 | { |
220 | { |
221 | me_info me; |
221 | me_info me; |
222 | int i; |
222 | int i; |
223 | FILE* f; |
223 | FILE* f; |
224 | //printf("%d\n",s1->nb_sections); |
224 | //printf("%d\n",s1->nb_sections); |
225 | memset(&me,0,sizeof(me)); |
225 | memset(&me,0,sizeof(me)); |
226 | me.s1=s1; |
226 | me.s1=s1; |
227 | relocate_common_syms(); |
227 | relocate_common_syms(); |
228 | assign_addresses(&me); |
228 | assign_addresses(&me); |
- | 229 | me.header.version=1; |
|
229 | me.header.entry_point=tcc_find_symbol_me(&me,"start"); |
230 | me.header.entry_point=tcc_find_symbol_me(&me,"start"); |
230 | me.header.params= tcc_find_symbol_me(&me,"__argv"); // <-- |
231 | me.header.params= tcc_find_symbol_me(&me,"__argv"); // <-- |
231 | me.header.argv= tcc_find_symbol_me(&me,"__path"); // <-- |
232 | me.header.argv= tcc_find_symbol_me(&me,"__path"); // <-- |
232 | 233 | ||
233 | f=fopen(filename,"wb"); |
234 | f=fopen(filename,"wb"); |
234 | for (i=0;i<8;i++) |
235 | for (i=0;i<8;i++) |
235 | me.header.magic[i]=me_magic[i]; |
236 | me.header.magic[i]=me_magic[i]; |
236 | /*me.header.magic[0]='M';me.header.magic[1]='E'; |
237 | /*me.header.magic[0]='M';me.header.magic[1]='E'; |
237 | me.header.magic[2]='N';me.header.magic[3]='U'; |
238 | me.header.magic[2]='N';me.header.magic[3]='U'; |
238 | me.header.magic[4]='E';me.header.magic[5]='T'; |
239 | me.header.magic[4]='E';me.header.magic[5]='T'; |
239 | me.header.magic[6]='0';me.header.magic[7]='1';*/ |
240 | me.header.magic[6]='0';me.header.magic[7]='1';*/ |
240 | fwrite(&me.header,1,sizeof(IMAGE_MEOS_FILE_HEADER),f); |
241 | fwrite(&me.header,1,sizeof(IMAGE_MEOS_FILE_HEADER),f); |
241 | meos_section_info* si; |
242 | meos_section_info* si; |
242 | for(si=me.code_sections;si;si=si->next) |
243 | for(si=me.code_sections;si;si=si->next) |
243 | fwrite(si->data,1,si->data_size,f); |
244 | fwrite(si->data,1,si->data_size,f); |
244 | for (si=me.data_sections;si;si=si->next) |
245 | for (si=me.data_sections;si;si=si->next) |
245 | fwrite(si->data,1,si->data_size,f); |
246 | fwrite(si->data,1,si->data_size,f); |
246 | fclose(f); |
247 | fclose(f); |
247 | return 0; |
248 | return 0; |
248 | }8;i++) |
249 | }8;i++) |
249 | >-- |
250 | >-- |
250 | >-- |
251 | >-- |
251 | > |
252 | > |
252 | > |
253 | > |
253 | > |
254 | > |