Mercurial > ecos
annotate packages/services/objloader/current/src/objelf.c @ 2243:6fd46febe457
Warnings, cosmetic fixes from Anthony Tonizzo
| author | gthomas |
|---|---|
| date | Wed, 28 Jun 2006 12:48:46 +0000 |
| parents | 8bf90d16b837 |
| children | 74dbf4c3f2e1 |
| rev | line source |
|---|---|
| 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 * | |
| 2243 | 45 * Author(s): Anthony Tonizzo (atonizzo@gmail.com) |
| 2011 | 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 | |
| 2243 | 65 CYG_HAL_TABLE_BEGIN(cyg_ldr_table, ldr_table); |
| 66 CYG_HAL_TABLE_END(cyg_ldr_table_end, ldr_table); | |
| 2011 | 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 | |
| 2243 | 73 cyg_ldr_print_section_data(PELF_OBJECT p) |
| 2011 | 74 { |
| 2243 | 75 int i; |
| 76 char strname[32]; | |
| 77 char *p_strtab = (char*)p->sections[p->p_elfhdr->e_shstrndx]; | |
| 2011 | 78 |
| 2243 | 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++) | |
| 2011 | 82 { |
| 2243 | 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", | |
| 2011 | 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, | |
| 2243 | 92 p->p_sechdr[i].sh_info); |
| 2011 | 93 } |
| 2243 | 94 diag_printf("\n"); |
| 2011 | 95 } |
| 96 | |
| 97 void | |
| 2243 | 98 cyg_ldr_print_symbol_names(PELF_OBJECT p) |
| 2011 | 99 { |
| 2243 | 100 int i; |
| 2011 | 101 Elf32_Sym *p_symtab = (Elf32_Sym*)p->sections[p->hdrndx_symtab]; |
| 2243 | 102 char *p_strtab = (char*)p->sections[p->hdrndx_strtab]; |
| 103 char strname[32]; | |
| 2011 | 104 |
| 105 // Total number of entries in the symbol table. | |
| 2243 | 106 int symtab_entries = p->p_sechdr[p->hdrndx_symtab].sh_size / |
| 2011 | 107 p->p_sechdr[p->hdrndx_symtab].sh_entsize; |
| 2243 | 108 diag_printf("Num Value Size Ndx Name\n"); |
| 109 for (i = 1; i < symtab_entries; i++) | |
| 2011 | 110 { |
| 2243 | 111 sprintf(strname, "%d", i); |
| 112 while (strlen(strname) < 5) | |
| 113 strcat(strname, " "); | |
| 114 diag_printf(strname); | |
| 2011 | 115 |
| 2243 | 116 sprintf(strname, |
| 2011 | 117 "%08X %d", |
| 118 p_symtab[i].st_value, | |
| 2243 | 119 p_symtab[i].st_size); |
| 120 while (strlen(strname) < 15) | |
| 121 strcat(strname, " "); | |
| 122 diag_printf(strname); | |
| 2011 | 123 |
| 2243 | 124 sprintf(strname, "%d", p_symtab[i].st_shndx); |
| 125 while (strlen(strname) < 6) | |
| 126 strcat(strname, " "); | |
| 127 diag_printf(strname); | |
| 2011 | 128 |
| 2243 | 129 strncpy(strname, |
| 2011 | 130 p_strtab + p_symtab[i].st_name, |
| 2243 | 131 sizeof(strname) - 1); |
| 132 strname[strlen(p_strtab + p_symtab[i].st_name)] = '\0'; | |
| 133 diag_printf(strname); | |
| 134 diag_printf("\n"); | |
| 2011 | 135 } |
| 136 } | |
| 137 | |
| 138 void | |
| 2243 | 139 cyg_ldr_print_rel_names(PELF_OBJECT p) |
| 2011 | 140 { |
| 2243 | 141 int i, j, r_entries, sym_index; |
| 2011 | 142 Elf32_Sym *p_symtab = (Elf32_Sym*)p->sections[p->hdrndx_symtab]; |
| 2243 | 143 char *p_strtab = (char*)p->sections[p->hdrndx_strtab]; |
| 144 char *p_shstrtab = (char*)p->sections[p->p_elfhdr->e_shstrndx]; | |
| 2011 | 145 #if ELF_ARCH_RELTYPE == Elf_Rela |
| 146 Elf32_Rela* p_rela; | |
| 147 #else | |
| 148 Elf32_Rel* p_rel; | |
| 149 #endif | |
| 2243 | 150 char strname[32]; |
| 2011 | 151 |
| 2243 | 152 for (i = 1; i < p->p_elfhdr->e_shnum; i++) |
| 2011 | 153 { |
| 2243 | 154 if ((p->p_sechdr[i].sh_type == SHT_REL) || |
| 155 (p->p_sechdr[i].sh_type == SHT_RELA)) | |
| 2011 | 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 | |
| 2243 | 160 diag_printf("\n\nSymbols at: %s\n\n", |
| 161 p_shstrtab + p->p_sechdr[i].sh_name); | |
| 2011 | 162 #if ELF_ARCH_RELTYPE == Elf_Rela |
| 2243 | 163 p_rela = (Elf32_Rela*)cyg_ldr_load_elf_section(p, i); |
| 164 printf("Offset Info Name [+ Addend]\n"); | |
| 2011 | 165 #else |
| 2243 | 166 p_rel = (Elf32_Rel*)cyg_ldr_load_elf_section(p, i); |
| 167 printf("Offset Info Name\n"); | |
| 2011 | 168 #endif |
| 169 | |
| 2243 | 170 for (j = 0; j < r_entries; j++) |
| 2011 | 171 { |
| 2243 | 172 sprintf(strname, |
| 2011 | 173 "%08X %08X ", |
|
2013
8bf90d16b837
* include/objelf.h: Include hal_tables.h otherwise we get strange
asl
parents:
2011
diff
changeset
|
174 #if ELF_ARCH_RELTYPE == Elf_Rela |
| 2243 | 175 p_rela[j].r_offset, |
| 176 p_rela[j].r_info | |
|
2013
8bf90d16b837
* include/objelf.h: Include hal_tables.h otherwise we get strange
asl
parents:
2011
diff
changeset
|
177 #else |
| 2243 | 178 p_rel[j].r_offset, |
| 179 p_rel[j].r_info | |
|
2013
8bf90d16b837
* include/objelf.h: Include hal_tables.h otherwise we get strange
asl
parents:
2011
diff
changeset
|
180 #endif |
| 2243 | 181 ); |
| 2011 | 182 |
| 2243 | 183 diag_printf(strname); |
|
2013
8bf90d16b837
* include/objelf.h: Include hal_tables.h otherwise we get strange
asl
parents:
2011
diff
changeset
|
184 |
|
8bf90d16b837
* include/objelf.h: Include hal_tables.h otherwise we get strange
asl
parents:
2011
diff
changeset
|
185 #if ELF_ARCH_RELTYPE == Elf_Rela |
| 2243 | 186 cyg_uint8 sym_type = ELF32_R_SYM(p_rela[j].r_info); |
|
2013
8bf90d16b837
* include/objelf.h: Include hal_tables.h otherwise we get strange
asl
parents:
2011
diff
changeset
|
187 #else |
| 2243 | 188 cyg_uint8 sym_type = ELF32_R_SYM(p_rel[j].r_info); |
|
2013
8bf90d16b837
* include/objelf.h: Include hal_tables.h otherwise we get strange
asl
parents:
2011
diff
changeset
|
189 #endif |
| 2243 | 190 if (strlen (p_strtab + p_symtab[sym_type].st_name) > 0) |
| 191 diag_printf(p_strtab + p_symtab[sym_type].st_name); | |
| 2011 | 192 else |
| 193 { | |
| 194 // If the symbol name is not available, then print | |
| 195 // the name of the section. | |
| 196 sym_index = p_symtab[sym_type].st_shndx; | |
| 2243 | 197 diag_printf(p_shstrtab + p->p_sechdr[sym_index].sh_name); |
| 2011 | 198 } |
| 199 #if ELF_ARCH_RELTYPE == Elf_Rela | |
| 2243 | 200 if (p_rela[j].r_addend != 0) |
| 201 diag_printf(" + %08X", p_rela[j].r_addend); | |
| 2011 | 202 #endif |
| 2243 | 203 diag_printf("\n"); |
| 2011 | 204 } |
| 205 // After all the printing is done, the relocation table can | |
| 206 // be dumped. | |
| 2243 | 207 cyg_ldr_delete_elf_section(p, i); |
| 2011 | 208 } |
| 209 } | |
| 210 } | |
| 211 #endif // DEBUG_PRINT | |
| 212 | |
| 213 static void | |
| 2243 | 214 *cyg_ldr_local_address(PELF_OBJECT p, cyg_uint32 sym_index) |
| 2011 | 215 { |
| 216 cyg_uint32 data_sec, addr; | |
| 217 Elf32_Sym *p_symtab; | |
| 218 | |
| 2243 | 219 p_symtab = (Elf32_Sym*)cyg_ldr_section_address(p, p->hdrndx_symtab); |
| 2011 | 220 |
| 221 // Find out the section number in which the data for this symbol is | |
| 222 // located. | |
| 223 data_sec = p_symtab[sym_index].st_shndx; | |
| 224 | |
| 225 // From the section number we get the start of the memory area in | |
| 226 // memory. | |
| 2243 | 227 addr = (cyg_uint32)cyg_ldr_section_address(p, data_sec); |
| 2011 | 228 |
| 229 // And now return the address of the data. | |
| 2243 | 230 return (void*)(addr + p_symtab[sym_index].st_value); |
| 2011 | 231 } |
| 232 | |
|
2013
8bf90d16b837
* include/objelf.h: Include hal_tables.h otherwise we get strange
asl
parents:
2011
diff
changeset
|
233 void |
| 2243 | 234 *cyg_ldr_external_address(PELF_OBJECT p, cyg_uint32 sym_index) |
| 2011 | 235 { |
| 236 cyg_uint8* tmp2; | |
| 237 Elf32_Sym *p_symtab; | |
| 238 cyg_uint8 *p_strtab; | |
| 239 cyg_ldr_table_entry *entry = cyg_ldr_table; | |
| 240 | |
| 241 | |
| 2243 | 242 p_symtab = (Elf32_Sym*)cyg_ldr_section_address(p, p->hdrndx_symtab); |
| 243 p_strtab = (cyg_uint8*)cyg_ldr_section_address(p, p->hdrndx_strtab); | |
| 2011 | 244 |
| 245 // This is the name of the external reference to search. | |
| 246 tmp2 = p_strtab + p_symtab[sym_index].st_name; | |
| 2243 | 247 while (entry != cyg_ldr_table_end) |
| 2011 | 248 { |
| 2243 | 249 if (!strcmp((const char*)tmp2, entry->symbol_name )) |
| 2011 | 250 return entry->handler; |
| 251 entry++; | |
| 252 } | |
| 253 | |
| 254 // Symbol not found. | |
| 255 return 0; | |
| 256 } | |
| 257 | |
| 258 // input: | |
| 259 // p : Pointer to the elf file object | |
| 260 // sym_index : Index of the symbol to be searched (in the SYMTAB) | |
| 261 // | |
| 262 // out: | |
| 263 // 0 : Symbol not found | |
| 264 // Other : Address of the symbol in absolute memory. | |
| 265 void | |
| 2243 | 266 *cyg_ldr_symbol_address(PELF_OBJECT p, cyg_uint32 sym_index) |
| 2011 | 267 { |
| 268 cyg_uint32 addr; | |
| 2243 | 269 Elf32_Sym *p_symtab = (Elf32_Sym*)cyg_ldr_section_address(p, |
| 270 p->hdrndx_symtab); | |
| 271 cyg_uint8 sym_info = p_symtab[sym_index].st_info; | |
| 272 switch (ELF32_ST_TYPE(sym_info)) | |
| 2011 | 273 { |
| 274 case STT_NOTYPE: | |
| 275 case STT_FUNC: | |
| 276 case STT_OBJECT: | |
| 2243 | 277 switch (ELF32_ST_BIND(sym_info)) |
| 2011 | 278 { |
| 279 case STB_LOCAL: | |
| 280 case STB_GLOBAL: | |
| 2243 | 281 if (p_symtab[sym_index].st_shndx == SHN_UNDEF) |
| 282 return cyg_ldr_external_address(p, sym_index); | |
| 2011 | 283 else |
| 2243 | 284 return cyg_ldr_local_address(p, sym_index); |
| 2011 | 285 case STB_WEAK: |
| 2243 | 286 addr = (cyg_uint32)cyg_ldr_external_address(p, sym_index); |
| 287 if (addr != 0) | |
| 2011 | 288 return (void*)addr; |
| 289 else | |
| 2243 | 290 return cyg_ldr_local_address(p, sym_index); |
| 2011 | 291 default: |
| 292 return 0; | |
| 293 } | |
| 294 break; | |
| 295 case STT_SECTION: | |
| 296 // Return the starting address of a section, given its index. | |
| 2243 | 297 return (void*)cyg_ldr_section_address(p, p_symtab[sym_index].st_shndx); |
| 2011 | 298 default: |
| 299 return 0; | |
| 300 } | |
| 301 } | |
| 302 | |
| 303 // Loads the relocation information, relocates, and dumps the relocation | |
| 304 // information once the process is complete. | |
| 305 cyg_int32 | |
| 2243 | 306 cyg_ldr_relocate_section(PELF_OBJECT p, cyg_uint32 r_shndx) |
| 2011 | 307 { |
| 2243 | 308 int i, rc; |
| 2011 | 309 #if ELF_ARCH_RELTYPE == Elf_Rela |
| 2243 | 310 Elf32_Rela* p_rela = (Elf32_Rela*)cyg_ldr_load_elf_section(p, r_shndx); |
| 2011 | 311 #else |
| 2243 | 312 Elf32_Rel* p_rel = (Elf32_Rel*)cyg_ldr_load_elf_section(p, r_shndx); |
| 2011 | 313 #endif |
| 314 | |
| 315 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 0 | |
| 2243 | 316 Elf32_Sym *p_symtab = (Elf32_Sym*)cyg_ldr_section_address(p, |
| 317 p->hdrndx_symtab); | |
| 318 char *p_strtab = (char*)cyg_ldr_section_address(p, p->hdrndx_strtab); | |
| 319 char *p_shstrtab = (char*)cyg_ldr_section_address(p, | |
| 320 p->p_elfhdr->e_shstrndx); | |
| 2011 | 321 #endif |
| 322 | |
| 323 // Now we can get the address of the contents of the section to modify. | |
| 2243 | 324 cyg_uint32 r_target_shndx = p->p_sechdr[r_shndx].sh_info; |
| 325 cyg_uint32 r_target_addr = (cyg_uint32)cyg_ldr_section_address(p, | |
| 326 r_target_shndx); | |
| 2011 | 327 |
| 328 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 0 | |
| 2243 | 329 diag_printf("Relocating section \"%s\"\n", |
| 330 p_shstrtab + p->p_sechdr[r_target_shndx].sh_name); | |
| 2011 | 331 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 1 |
| 2243 | 332 diag_printf("Ndx Type Offset Name\"\n"); |
| 2011 | 333 #endif |
| 334 #endif | |
| 335 | |
| 336 // Perform relocatation for each of the members of this table. | |
| 2243 | 337 cyg_uint32 r_entries = p->p_sechdr[r_shndx].sh_size / |
| 338 p->p_sechdr[r_shndx].sh_entsize; | |
| 339 for (i = 0; i < r_entries; i++) | |
| 2011 | 340 { |
| 341 #if ELF_ARCH_RELTYPE == Elf_Rela | |
| 2243 | 342 Elf32_Addr r_offset = p_rela[i].r_offset; |
| 343 Elf32_Word r_type = ELF32_R_TYPE(p_rela[i].r_info); | |
| 344 cyg_uint32 sym_index = ELF32_R_SYM(p_rela[i].r_info); | |
| 345 Elf32_Sword r_addend = p_rela[i].r_addend; | |
| 2011 | 346 #else |
| 2243 | 347 Elf32_Addr r_offset = p_rel[i].r_offset; |
| 348 Elf32_Word r_type = ELF32_R_TYPE(p_rel[i].r_info); | |
| 349 cyg_uint32 sym_index = ELF32_R_SYM(p_rel[i].r_info); | |
| 350 Elf32_Sword r_addend = 0; | |
| 2011 | 351 #endif |
| 352 | |
| 2243 | 353 cyg_uint32 sym_value = (cyg_uint32)cyg_ldr_symbol_address(p, sym_index); |
| 354 if (sym_value == 0) | |
| 2011 | 355 { |
| 356 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 0 | |
| 2243 | 357 diag_printf("Unknown symbol value: %s Index: %d\n", |
| 2011 | 358 p_strtab + p_symtab[sym_index].st_name, |
| 2243 | 359 sym_index); |
| 2011 | 360 #endif |
| 361 return -1; | |
| 362 } | |
| 363 | |
| 364 // This is architecture dependent, and deals with whether we have | |
| 365 // '.rel' or '.rela' sections. | |
| 366 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 1 | |
| 2243 | 367 diag_printf("%04X %04X %08X ", |
| 2011 | 368 sym_index, |
| 369 r_type, | |
| 2243 | 370 r_offset); |
| 371 if (strlen(p_strtab + p_symtab[sym_index].st_name) > 0) | |
| 372 diag_printf(p_strtab + p_symtab[sym_index].st_name); | |
| 2011 | 373 else |
| 374 { | |
| 375 // If the symbol name is not available, then print | |
| 376 // the name of the section. | |
| 377 cyg_uint32 sec_ndx = p_symtab[sym_index].st_shndx; | |
| 2243 | 378 diag_printf(p_shstrtab + p->p_sechdr[sec_ndx].sh_name); |
| 2011 | 379 } |
| 2243 | 380 diag_printf("\n"); |
| 2011 | 381 #endif |
| 2243 | 382 rc = cyg_ldr_relocate(r_type, |
| 2011 | 383 r_target_addr + r_offset, |
| 2243 | 384 sym_value + r_addend); |
| 385 if (rc != 0) | |
| 2011 | 386 { |
| 387 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 0 | |
| 2243 | 388 diag_printf("Relocation error: Cannot find symbol: %s\n", |
| 389 p_strtab + p_symtab[sym_index].st_name); | |
| 2011 | 390 #endif |
| 391 return -1; | |
| 392 } | |
| 393 } | |
| 394 | |
| 395 // After the relocation is done, the relocation table can be dumped. | |
| 2243 | 396 cyg_ldr_delete_elf_section(p, r_shndx); |
| 2011 | 397 return 0; |
| 398 } | |
| 399 |
