|
2011
|
1 /* ================================================================= |
|
|
2 * |
|
|
3 * objelf.c |
|
|
4 * |
|
|
5 * Relocation routine for eCos loader. |
|
|
6 * |
|
|
7 * ================================================================= |
|
|
8 * ####ECOSGPLCOPYRIGHTBEGIN#### |
|
|
9 * ------------------------------------------- |
|
|
10 * This file is part of eCos, the Embedded Configurable Operating |
|
|
11 * System. |
|
|
12 * Copyright (C) 2005 eCosCentric Ltd. |
|
|
13 * |
|
|
14 * eCos is free software; you can redistribute it and/or modify it |
|
|
15 * under the terms of the GNU General Public License as published by |
|
|
16 * the Free Software Foundation; either version 2 or (at your option) |
|
|
17 * any later version. |
|
|
18 * |
|
|
19 * eCos is distributed in the hope that it will be useful, but |
|
|
20 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
|
22 * General Public License for more details. |
|
|
23 * |
|
|
24 * You should have received a copy of the GNU General Public License |
|
|
25 * along with eCos; if not, write to the Free Software Foundation, |
|
|
26 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
|
27 * |
|
|
28 * As a special exception, if other files instantiate templates or |
|
|
29 * use macros or inline functions from this file, or you compile this |
|
|
30 * file and link it with other works to produce a work based on this |
|
|
31 * file, this file does not by itself cause the resulting work to be |
|
|
32 * covered by the GNU General Public License. However the source code |
|
|
33 * for this file must still be made available in accordance with |
|
|
34 * section (3) of the GNU General Public License. |
|
|
35 * |
|
|
36 * This exception does not invalidate any other reasons why a work |
|
|
37 * based on this file might be covered by the GNU General Public |
|
|
38 * License. |
|
|
39 * |
|
|
40 * ------------------------------------------- |
|
|
41 * ####ECOSGPLCOPYRIGHTEND#### |
|
|
42 * ================================================================= |
|
|
43 * #####DESCRIPTIONBEGIN#### |
|
|
44 * |
|
|
45 * Author(s): atonizzo@lycos.com |
|
|
46 * Contributors: nickg@ecoscentric.com |
|
|
47 * Date: 2005-05-13 |
|
|
48 * Purpose: |
|
|
49 * Description: |
|
|
50 * |
|
|
51 * ####DESCRIPTIONEND#### |
|
|
52 * |
|
|
53 * ================================================================= |
|
|
54 */ |
|
|
55 |
|
|
56 #include <cyg/infra/diag.h> // For diagnostic printing. |
|
|
57 #include <pkgconf/io_fileio.h> |
|
|
58 #include <cyg/hal/hal_tables.h> |
|
|
59 #include <stdio.h> |
|
|
60 |
|
|
61 #include <pkgconf/objloader.h> |
|
|
62 #include <cyg/objloader/elf.h> |
|
|
63 #include <cyg/objloader/objelf.h> |
|
|
64 |
|
|
65 CYG_HAL_TABLE_BEGIN( cyg_ldr_table, ldr_table ); |
|
|
66 CYG_HAL_TABLE_END( cyg_ldr_table_end, ldr_table ); |
|
|
67 |
|
|
68 __externC cyg_ldr_table_entry cyg_ldr_table[]; |
|
|
69 __externC cyg_ldr_table_entry cyg_ldr_table_end[]; |
|
|
70 |
|
|
71 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 0 |
|
|
72 void |
|
|
73 cyg_ldr_print_section_data( PELF_OBJECT p ) |
|
|
74 { |
|
|
75 cyg_int32 i; |
|
|
76 cyg_uint8 strname[32]; |
|
|
77 cyg_uint8 *p_strtab = (cyg_uint8*)p->sections[p->p_elfhdr->e_shstrndx]; |
|
|
78 |
|
|
79 diag_printf( "\n\nSection Headers:\n" ); |
|
|
80 diag_printf( "[Nr] Name Addr Offset Size Info\n" ); |
|
|
81 for ( i = 0; i < p->p_elfhdr->e_shnum; i++ ) |
|
|
82 { |
|
|
83 sprintf( strname, "%s", p_strtab + p->p_sechdr[i].sh_name ); |
|
|
84 while ( strlen( strname ) < 20 ) |
|
|
85 strcat( strname, " " ); |
|
|
86 diag_printf( "[%02d] %s %08X %08X %08X %08X\n", |
|
|
87 i, |
|
|
88 strname, |
|
|
89 p->p_sechdr[i].sh_addr, |
|
|
90 p->p_sechdr[i].sh_offset, |
|
|
91 p->p_sechdr[i].sh_size, |
|
|
92 p->p_sechdr[i].sh_info ); |
|
|
93 } |
|
|
94 diag_printf( "\n" ); |
|
|
95 } |
|
|
96 |
|
|
97 void |
|
|
98 cyg_ldr_print_symbol_names( PELF_OBJECT p ) |
|
|
99 { |
|
|
100 cyg_int32 i, symtab_entries; |
|
|
101 Elf32_Sym *p_symtab = (Elf32_Sym*)p->sections[p->hdrndx_symtab]; |
|
|
102 cyg_uint8 *p_strtab = (cyg_uint8*)p->sections[p->hdrndx_strtab]; |
|
|
103 cyg_uint8 strname[32]; |
|
|
104 |
|
|
105 // Total number of entries in the symbol table. |
|
|
106 symtab_entries = p->p_sechdr[p->hdrndx_symtab].sh_size / |
|
|
107 p->p_sechdr[p->hdrndx_symtab].sh_entsize; |
|
|
108 diag_printf( "Num Value Size Ndx Name\n" ); |
|
|
109 for ( i = 1; i < symtab_entries; i++ ) |
|
|
110 { |
|
|
111 sprintf( strname, "%d", i ); |
|
|
112 while ( strlen( strname ) < 5 ) |
|
|
113 strcat( strname, " " ); |
|
|
114 diag_printf( strname ); |
|
|
115 |
|
|
116 sprintf( strname, |
|
|
117 "%08X %d", |
|
|
118 p_symtab[i].st_value, |
|
|
119 p_symtab[i].st_size ); |
|
|
120 while ( strlen( strname ) < 15 ) |
|
|
121 strcat( strname, " " ); |
|
|
122 diag_printf( strname ); |
|
|
123 |
|
|
124 sprintf( strname, "%d", p_symtab[i].st_shndx ); |
|
|
125 while ( strlen( strname ) < 6 ) |
|
|
126 strcat( strname, " " ); |
|
|
127 diag_printf( strname ); |
|
|
128 |
|
|
129 strncpy( strname, |
|
|
130 p_strtab + p_symtab[i].st_name, |
|
|
131 sizeof( strname ) - 1 ); |
|
|
132 strname[strlen( p_strtab + p_symtab[i].st_name )] = '\0'; |
|
|
133 diag_printf( strname ); |
|
|
134 diag_printf( "\n" ); |
|
|
135 } |
|
|
136 } |
|
|
137 |
|
|
138 void |
|
|
139 cyg_ldr_print_rel_names( PELF_OBJECT p ) |
|
|
140 { |
|
|
141 cyg_int32 i, j, r_entries, sym_index; |
|
|
142 Elf32_Sym *p_symtab = (Elf32_Sym*)p->sections[p->hdrndx_symtab]; |
|
|
143 cyg_uint8 *p_strtab = (cyg_uint8*)p->sections[p->hdrndx_strtab]; |
|
|
144 cyg_uint8 *p_shstrtab = (cyg_uint8*)p->sections[p->p_elfhdr->e_shstrndx]; |
|
|
145 #if ELF_ARCH_RELTYPE == Elf_Rela |
|
|
146 Elf32_Rela* p_rela; |
|
|
147 #else |
|
|
148 Elf32_Rel* p_rel; |
|
|
149 #endif |
|
|
150 cyg_uint8 strname[32]; |
|
|
151 |
|
|
152 for ( i = 1; i < p->p_elfhdr->e_shnum; i++ ) |
|
|
153 { |
|
|
154 if ( ( p->p_sechdr[i].sh_type == SHT_REL ) || |
|
|
155 ( p->p_sechdr[i].sh_type == SHT_RELA ) ) |
|
|
156 { |
|
|
157 // Calculate the total number of entries in the .rela section. |
|
|
158 r_entries = p->p_sechdr[i].sh_size / p->p_sechdr[i].sh_entsize; |
|
|
159 |
|
|
160 diag_printf( "\n\nSymbols at: %s\n\n", |
|
|
161 p_shstrtab + p->p_sechdr[i].sh_name ); |
|
|
162 #if ELF_ARCH_RELTYPE == Elf_Rela |
|
|
163 p_rela = (Elf32_Rela*)cyg_ldr_load_elf_section( p, i ); |
|
|
164 printf( "Offset Info Name [+ Addend]\n" ); |
|
|
165 #else |
|
|
166 p_rel = (Elf32_Rel*)cyg_ldr_load_elf_section( p, i ); |
|
|
167 printf( "Offset Info Name\n" ); |
|
|
168 #endif |
|
|
169 |
|
|
170 for ( j = 0; j < r_entries; j++ ) |
|
|
171 { |
|
|
172 sprintf( strname, |
|
|
173 "%08X %08X ", |
|
|
174 p_rela[j].r_offset, |
|
|
175 p_rela[j].r_info ); |
|
|
176 |
|
|
177 diag_printf( strname ); |
|
|
178 |
|
|
179 cyg_uint8 sym_type = ELF32_R_SYM( p_rela[j].r_info ); |
|
|
180 if ( strlen ( p_strtab + p_symtab[sym_type].st_name ) > 0 ) |
|
|
181 diag_printf( p_strtab + p_symtab[sym_type].st_name ); |
|
|
182 else |
|
|
183 { |
|
|
184 // If the symbol name is not available, then print |
|
|
185 // the name of the section. |
|
|
186 sym_index = p_symtab[sym_type].st_shndx; |
|
|
187 diag_printf( p_shstrtab + p->p_sechdr[sym_index].sh_name ); |
|
|
188 } |
|
|
189 #if ELF_ARCH_RELTYPE == Elf_Rela |
|
|
190 if ( p_rela[j].r_addend != 0 ) |
|
|
191 diag_printf( " + %08X", p_rela[j].r_addend ); |
|
|
192 #endif |
|
|
193 diag_printf( "\n" ); |
|
|
194 } |
|
|
195 // After all the printing is done, the relocation table can |
|
|
196 // be dumped. |
|
|
197 cyg_ldr_delete_elf_section( p, i ); |
|
|
198 } |
|
|
199 } |
|
|
200 } |
|
|
201 #endif // DEBUG_PRINT |
|
|
202 |
|
|
203 static void |
|
|
204 *cyg_ldr_local_address( PELF_OBJECT p, cyg_uint32 sym_index ) |
|
|
205 { |
|
|
206 cyg_uint32 data_sec, addr; |
|
|
207 Elf32_Sym *p_symtab; |
|
|
208 |
|
|
209 p_symtab = (Elf32_Sym*)cyg_ldr_section_address( p, p->hdrndx_symtab ); |
|
|
210 |
|
|
211 // Find out the section number in which the data for this symbol is |
|
|
212 // located. |
|
|
213 data_sec = p_symtab[sym_index].st_shndx; |
|
|
214 |
|
|
215 // From the section number we get the start of the memory area in |
|
|
216 // memory. |
|
|
217 addr = (cyg_uint32)cyg_ldr_section_address( p, data_sec ); |
|
|
218 |
|
|
219 // And now return the address of the data. |
|
|
220 return (void*)( addr + p_symtab[sym_index].st_value); |
|
|
221 } |
|
|
222 |
|
|
223 static void |
|
|
224 *cyg_ldr_external_address( PELF_OBJECT p, cyg_uint32 sym_index ) |
|
|
225 { |
|
|
226 cyg_uint8* tmp2; |
|
|
227 Elf32_Sym *p_symtab; |
|
|
228 cyg_uint8 *p_strtab; |
|
|
229 cyg_ldr_table_entry *entry = cyg_ldr_table; |
|
|
230 |
|
|
231 |
|
|
232 p_symtab = (Elf32_Sym*)cyg_ldr_section_address( p, p->hdrndx_symtab ); |
|
|
233 p_strtab = (cyg_uint8*)cyg_ldr_section_address( p, p->hdrndx_strtab ); |
|
|
234 |
|
|
235 // This is the name of the external reference to search. |
|
|
236 tmp2 = p_strtab + p_symtab[sym_index].st_name; |
|
|
237 while ( entry != cyg_ldr_table_end ) |
|
|
238 { |
|
|
239 if ( !strcmp( (const char*)tmp2, entry->symbol_name ) ) |
|
|
240 return entry->handler; |
|
|
241 entry++; |
|
|
242 } |
|
|
243 |
|
|
244 // Symbol not found. |
|
|
245 return 0; |
|
|
246 } |
|
|
247 |
|
|
248 // input: |
|
|
249 // p : Pointer to the elf file object |
|
|
250 // sym_index : Index of the symbol to be searched (in the SYMTAB) |
|
|
251 // |
|
|
252 // out: |
|
|
253 // 0 : Symbol not found |
|
|
254 // Other : Address of the symbol in absolute memory. |
|
|
255 void |
|
|
256 *cyg_ldr_symbol_address( PELF_OBJECT p, cyg_uint32 sym_index ) |
|
|
257 { |
|
|
258 cyg_uint32 addr; |
|
|
259 cyg_uint8 sym_info; |
|
|
260 Elf32_Sym *p_symtab; |
|
|
261 |
|
|
262 p_symtab = (Elf32_Sym*)cyg_ldr_section_address( p, p->hdrndx_symtab ); |
|
|
263 sym_info = p_symtab[sym_index].st_info; |
|
|
264 |
|
|
265 switch ( ELF32_ST_TYPE( sym_info ) ) |
|
|
266 { |
|
|
267 case STT_NOTYPE: |
|
|
268 case STT_FUNC: |
|
|
269 case STT_OBJECT: |
|
|
270 switch ( ELF32_ST_BIND( sym_info ) ) |
|
|
271 { |
|
|
272 case STB_LOCAL: |
|
|
273 case STB_GLOBAL: |
|
|
274 if ( p_symtab[sym_index].st_shndx == SHN_UNDEF ) |
|
|
275 return cyg_ldr_external_address( p, sym_index ); |
|
|
276 else |
|
|
277 return cyg_ldr_local_address( p, sym_index ); |
|
|
278 case STB_WEAK: |
|
|
279 addr = (cyg_uint32)cyg_ldr_external_address( p, sym_index ); |
|
|
280 if ( addr != 0 ) |
|
|
281 return (void*)addr; |
|
|
282 else |
|
|
283 return cyg_ldr_local_address( p, sym_index ); |
|
|
284 default: |
|
|
285 return 0; |
|
|
286 } |
|
|
287 break; |
|
|
288 case STT_SECTION: |
|
|
289 // Return the starting address of a section, given its index. |
|
|
290 return (void*)cyg_ldr_section_address( p, |
|
|
291 p_symtab[sym_index].st_shndx ); |
|
|
292 default: |
|
|
293 return 0; |
|
|
294 } |
|
|
295 } |
|
|
296 |
|
|
297 // Loads the relocation information, relocates, and dumps the relocation |
|
|
298 // information once the process is complete. |
|
|
299 cyg_int32 |
|
|
300 cyg_ldr_relocate_section( PELF_OBJECT p, cyg_uint32 r_shndx ) |
|
|
301 { |
|
|
302 cyg_int32 i, rc; |
|
|
303 cyg_uint32 r_entries, r_target_shndx, r_target_addr; |
|
|
304 cyg_uint32 sym_value, sym_index; |
|
|
305 Elf32_Addr r_offset; |
|
|
306 Elf32_Word r_type; |
|
|
307 Elf32_Sword r_addend; |
|
|
308 #if ELF_ARCH_RELTYPE == Elf_Rela |
|
|
309 Elf32_Rela* p_rela = (Elf32_Rela*)cyg_ldr_load_elf_section( p, r_shndx ); |
|
|
310 #else |
|
|
311 Elf32_Rel* p_rel = (Elf32_Rel*)cyg_ldr_load_elf_section( p, r_shndx ); |
|
|
312 #endif |
|
|
313 |
|
|
314 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 0 |
|
|
315 Elf32_Sym *p_symtab = (Elf32_Sym*)cyg_ldr_section_address( p, |
|
|
316 p->hdrndx_symtab ); |
|
|
317 cyg_uint8 *p_strtab = (cyg_uint8*)cyg_ldr_section_address( p, |
|
|
318 p->hdrndx_strtab ); |
|
|
319 cyg_uint8 *p_shstrtab = (cyg_uint8*)cyg_ldr_section_address( p, |
|
|
320 p->p_elfhdr->e_shstrndx ); |
|
|
321 #endif |
|
|
322 |
|
|
323 // Now we can get the address of the contents of the section to modify. |
|
|
324 r_target_shndx = p->p_sechdr[r_shndx].sh_info; |
|
|
325 r_target_addr = (cyg_uint32)cyg_ldr_section_address( p, r_target_shndx ); |
|
|
326 |
|
|
327 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 0 |
|
|
328 diag_printf( "Relocating section \"%s\"\n", |
|
|
329 p_shstrtab + p->p_sechdr[r_target_shndx].sh_name ); |
|
|
330 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 1 |
|
|
331 diag_printf( "Ndx Type Offset Name\"\n" ); |
|
|
332 #endif |
|
|
333 #endif |
|
|
334 |
|
|
335 // Perform relocatation for each of the members of this table. |
|
|
336 r_entries = p->p_sechdr[r_shndx].sh_size / p->p_sechdr[r_shndx].sh_entsize; |
|
|
337 for ( i = 0; i < r_entries; i++ ) |
|
|
338 { |
|
|
339 #if ELF_ARCH_RELTYPE == Elf_Rela |
|
|
340 r_offset = p_rela[i].r_offset; |
|
|
341 r_type = ELF32_R_TYPE( p_rela[i].r_info ); |
|
|
342 sym_index = ELF32_R_SYM( p_rela[i].r_info ); |
|
|
343 r_addend = p_rela[i].r_addend; |
|
|
344 #else |
|
|
345 r_offset = p_rel[i].r_offset; |
|
|
346 r_type = ELF32_R_TYPE( p_rel[i].r_info ); |
|
|
347 sym_index = ELF32_R_SYM( p_rel[i].r_info ); |
|
|
348 r_addend = 0; |
|
|
349 #endif |
|
|
350 |
|
|
351 sym_value = (cyg_uint32)cyg_ldr_symbol_address( p, sym_index ); |
|
|
352 if ( sym_value == 0 ) |
|
|
353 { |
|
|
354 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 0 |
|
|
355 diag_printf( "Unknown symbol value: %s Index: %d\n", |
|
|
356 p_strtab + p_symtab[sym_index].st_name, |
|
|
357 sym_index ); |
|
|
358 #endif |
|
|
359 return -1; |
|
|
360 } |
|
|
361 |
|
|
362 // This is architecture dependent, and deals with whether we have |
|
|
363 // '.rel' or '.rela' sections. |
|
|
364 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 1 |
|
|
365 diag_printf( "%04X %04X %08X ", |
|
|
366 sym_index, |
|
|
367 r_type, |
|
|
368 r_offset ); |
|
|
369 if ( strlen ( p_strtab + p_symtab[sym_index].st_name ) > 0 ) |
|
|
370 diag_printf( p_strtab + p_symtab[sym_index].st_name ); |
|
|
371 else |
|
|
372 { |
|
|
373 // If the symbol name is not available, then print |
|
|
374 // the name of the section. |
|
|
375 cyg_uint32 sec_ndx = p_symtab[sym_index].st_shndx; |
|
|
376 diag_printf( p_shstrtab + p->p_sechdr[sec_ndx].sh_name ); |
|
|
377 } |
|
|
378 diag_printf( "\n" ); |
|
|
379 #endif |
|
|
380 rc = cyg_ldr_relocate( r_type, |
|
|
381 r_target_addr + r_offset, |
|
|
382 sym_value + r_addend ); |
|
|
383 if ( rc != 0 ) |
|
|
384 { |
|
|
385 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 0 |
|
|
386 diag_printf( "Relocation error: Cannot find symbol: %s\n", |
|
|
387 p_strtab + p_symtab[sym_index].st_name ); |
|
|
388 #endif |
|
|
389 return -1; |
|
|
390 } |
|
|
391 } |
|
|
392 |
|
|
393 // After the relocation is done, the relocation table can be dumped. |
|
|
394 cyg_ldr_delete_elf_section( p, r_shndx ); |
|
|
395 return 0; |
|
|
396 } |
|
|
397 |