Mercurial > ecos-v3_0-branch
annotate packages/services/objloader/current/src/objloader.c @ 2863:7d8c61e6225c default tip
* Added execute permissions to files missed in conversion from CVS
| author | alexs |
|---|---|
| date | Thu, 26 Mar 2009 20:39:18 +0000 |
| parents | 74dbf4c3f2e1 |
| children |
| rev | line source |
|---|---|
| 2011 | 1 /* ================================================================= |
| 2 * | |
| 3 * objelf.c | |
| 4 * | |
| 5 * An object loader for eCos | |
| 6 * | |
| 7 * ================================================================= | |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
8 * ####ECOSGPLCOPYRIGHTBEGIN#### |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
9 * ------------------------------------------- |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
10 * This file is part of eCos, the Embedded Configurable Operating System. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
11 * Copyright (C) 2005 Free Software Foundation, Inc. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
12 * |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
13 * eCos is free software; you can redistribute it and/or modify it under |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
14 * the terms of the GNU General Public License as published by the Free |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
15 * Software Foundation; either version 2 or (at your option) any later |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
16 * version. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
17 * |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
18 * eCos is distributed in the hope that it will be useful, but WITHOUT |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
21 * for more details. |
| 2011 | 22 * |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
23 * You should have received a copy of the GNU General Public License |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
24 * along with eCos; if not, write to the Free Software Foundation, Inc., |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
26 * |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
27 * As a special exception, if other files instantiate templates or use |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
28 * macros or inline functions from this file, or you compile this file |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
29 * and link it with other works to produce a work based on this file, |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
30 * this file does not by itself cause the resulting work to be covered by |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
31 * the GNU General Public License. However the source code for this file |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
32 * must still be made available in accordance with section (3) of the GNU |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
33 * General Public License v2. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
34 * |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
35 * This exception does not invalidate any other reasons why a work based |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
36 * on this file might be covered by the GNU General Public License. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
37 * ------------------------------------------- |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2243
diff
changeset
|
38 * ####ECOSGPLCOPYRIGHTEND#### |
| 2011 | 39 * ================================================================= |
| 40 * #####DESCRIPTIONBEGIN#### | |
| 41 * | |
| 2243 | 42 * Author(s): Anthony Tonizzo (atonizzo@gmail.com) |
| 2011 | 43 * Contributors: nickg@ecoscentric.com |
| 44 * Date: 2005-05-13 | |
| 45 * Purpose: | |
| 46 * Description: | |
| 47 * | |
| 48 * ####DESCRIPTIONEND#### | |
| 49 * | |
| 50 * ================================================================= | |
| 51 */ | |
| 52 | |
| 53 #include <cyg/infra/diag.h> // For diagnostic printing. | |
| 54 #include <cyg/infra/cyg_ass.h> | |
| 55 #include <stdlib.h> | |
| 56 #include <string.h> | |
| 57 | |
| 58 #include <pkgconf/objloader.h> | |
|
2013
8bf90d16b837
* include/objelf.h: Include hal_tables.h otherwise we get strange
asl
parents:
2011
diff
changeset
|
59 |
| 2011 | 60 #include <cyg/objloader/elf.h> |
| 61 #include <cyg/objloader/objelf.h> | |
| 62 #include <cyg/objloader/loader_fs.h> | |
| 63 | |
| 2243 | 64 char *cyg_ldr_last_error; |
| 2011 | 65 |
| 2243 | 66 void *cyg_ldr_malloc(size_t) CYGBLD_ATTRIB_WEAK; |
| 2011 | 67 void |
| 2243 | 68 *cyg_ldr_malloc(size_t s) |
| 2011 | 69 { |
| 2243 | 70 return malloc(s); |
| 2011 | 71 } |
| 72 | |
| 2243 | 73 void cyg_ldr_free(void *) CYGBLD_ATTRIB_WEAK; |
| 2011 | 74 void |
| 2243 | 75 cyg_ldr_free(void *s) |
| 2011 | 76 { |
| 2243 | 77 free(s); |
| 2011 | 78 } |
| 79 | |
| 80 void | |
| 2243 | 81 cyg_ldr_delete_elf_section(PELF_OBJECT p, cyg_uint32 idx) |
| 2011 | 82 { |
| 2243 | 83 cyg_ldr_free(p->sections[idx]); |
| 2011 | 84 p->sections[idx] = 0; |
| 85 } | |
| 86 | |
| 87 // Frees all the memory allocated for a particular ELF object. Also calls | |
| 88 // the close() function to close files or sockets, and finally frees up | |
| 89 // the ELF object altogether. | |
| 90 static void | |
| 2243 | 91 cyg_ldr_free_elf_object(PELF_OBJECT p) |
| 2011 | 92 { |
| 93 cyg_int32 i; | |
| 94 | |
| 2243 | 95 for (i = 0; i < p->p_elfhdr->e_shnum + 1; i++) |
| 96 if (p->sections[i]) | |
| 97 cyg_ldr_delete_elf_section(p, i); | |
| 2011 | 98 |
| 2243 | 99 if (p->sections != 0) |
| 100 cyg_ldr_free(p->sections); | |
| 2011 | 101 |
| 2243 | 102 if (p->p_sechdr != 0) |
| 103 cyg_ldr_free(p->p_sechdr); | |
| 2011 | 104 |
| 2243 | 105 if (p->p_elfhdr != 0) |
| 106 cyg_ldr_free(p->p_elfhdr); | |
| 2011 | 107 |
| 2243 | 108 p->close(p); |
| 109 cyg_ldr_free(p); | |
| 2011 | 110 } |
| 111 | |
| 112 static cyg_uint32 | |
| 2243 | 113 cyg_ldr_find_common_size(PELF_OBJECT p) |
| 2011 | 114 { |
| 2243 | 115 cyg_int32 i, common_size = 0; |
| 2011 | 116 Elf32_Sym *p_symtab = (Elf32_Sym*)p->sections[p->hdrndx_symtab]; |
| 117 | |
| 118 // Total number of entries in the symbol table. | |
| 2243 | 119 int symtab_entries = p->p_sechdr[p->hdrndx_symtab].sh_size / |
| 2011 | 120 p->p_sechdr[p->hdrndx_symtab].sh_entsize; |
| 2243 | 121 for (i = 1; i < symtab_entries; i++) |
| 122 if (p_symtab[i].st_shndx == SHN_COMMON) | |
| 2011 | 123 { |
| 124 // In the case of an SHN_COMMON symbol the st_value field holds | |
| 125 // alignment constraints. | |
| 126 cyg_uint32 boundary = p_symtab[i].st_value - 1; | |
| 127 | |
| 128 // Calculate the next byte boundary. | |
| 2243 | 129 common_size = (common_size + boundary) & ~boundary; |
| 2011 | 130 common_size += p_symtab[i].st_size; |
| 131 } | |
| 132 | |
| 133 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 0 | |
| 2243 | 134 diag_printf("common_size = %d\n", common_size); |
| 2011 | 135 #endif |
| 136 return common_size; | |
| 137 } | |
| 138 | |
| 139 // Allocates memory and loads the contents of a specific ELF section. | |
| 140 // Returns the address of the newly allocated memory, of 0 for any error. | |
| 2243 | 141 cyg_uint32 |
| 142 *cyg_ldr_load_elf_section(PELF_OBJECT p, cyg_uint32 idx) | |
| 2011 | 143 { |
| 2243 | 144 cyg_uint32 *addr = (cyg_uint32 *)cyg_ldr_malloc(p->p_sechdr[idx].sh_size); |
| 145 CYG_ASSERT(addr != 0, "Cannot malloc() section"); | |
| 146 if (addr == 0) | |
| 2011 | 147 { |
| 148 cyg_ldr_last_error = "ERROR IN MALLOC"; | |
| 149 return (void*)0; | |
| 150 } | |
| 2243 | 151 p->seek(p, p->p_sechdr[idx].sh_offset); |
| 152 p->read(p, sizeof(char), p->p_sechdr[idx].sh_size, addr); | |
| 2011 | 153 return addr; |
| 154 } | |
| 155 | |
| 156 // Returns the starting address of a section. If the section is not already | |
| 157 // loaded in memory, area for it will be allocated and the section will be | |
| 158 // loaded. | |
| 2243 | 159 cyg_uint32 |
| 160 *cyg_ldr_section_address(PELF_OBJECT p, cyg_uint32 idx) | |
| 2011 | 161 { |
| 2243 | 162 if (p->sections[idx] == 0) |
| 163 p->sections[idx] = cyg_ldr_load_elf_section(p, idx); | |
| 2011 | 164 |
| 165 return p->sections[idx]; | |
| 166 } | |
| 167 | |
| 168 void | |
| 2243 | 169 *cyg_ldr_find_symbol(void* handle, char* sym_name) |
| 2011 | 170 { |
| 171 PELF_OBJECT p = (PELF_OBJECT)handle; | |
| 2243 | 172 int i; |
| 173 char *p_strtab = (char*)p->sections[p->hdrndx_strtab]; | |
| 2011 | 174 Elf32_Sym *p_symtab = (Elf32_Sym*)p->sections[p->hdrndx_symtab]; |
| 2243 | 175 |
| 176 int symtab_entries = p->p_sechdr[p->hdrndx_symtab].sh_size / | |
| 2011 | 177 p->p_sechdr[p->hdrndx_symtab].sh_entsize; |
| 178 | |
| 2243 | 179 for (i = 0; i < symtab_entries; i++) |
| 2011 | 180 { |
| 2243 | 181 char* tmp2 = p_strtab + p_symtab[i].st_name; |
| 182 if (!strcmp(tmp2, sym_name)) | |
| 183 return cyg_ldr_symbol_address(p, i); | |
| 2011 | 184 } |
| 185 | |
| 186 // Symbol not found. | |
| 187 cyg_ldr_last_error = "SYMBOL NOT FOUND"; | |
| 188 return 0; | |
| 189 } | |
| 190 | |
| 2243 | 191 static char |
| 192 *cyg_ldr_sanity_check(PELF_OBJECT p) | |
| 2011 | 193 { |
| 2243 | 194 if ((p->p_elfhdr->e_ident[EI_MAG0] != ELFMAG0) || |
| 195 (p->p_elfhdr->e_ident[EI_MAG1] != ELFMAG1) || | |
| 196 (p->p_elfhdr->e_ident[EI_MAG2] != ELFMAG2 ) || | |
| 197 (p->p_elfhdr->e_ident[EI_MAG3] != ELFMAG3) || | |
| 198 (p->p_elfhdr->e_ident[EI_CLASS] != ELFCLASS32)) | |
| 2011 | 199 return "INVALID ELF HEADER"; |
| 200 | |
| 201 // We only work with relocatable files. No dynamic linking. | |
| 2243 | 202 if (p->p_elfhdr->e_type != ET_REL) |
| 2011 | 203 return "NOT RELOCATABLE"; |
| 204 | |
| 205 // These #defines are sitting in the hal. | |
| 2243 | 206 if (p->p_elfhdr->e_machine != ELF_ARCH_MACHINE_TYPE) |
| 2011 | 207 return "INVALID ARCHITECTURE"; |
| 208 | |
| 2243 | 209 if (p->p_elfhdr->e_ident[EI_DATA] != ELF_ARCH_ENDIANNESS) |
| 2011 | 210 return "INVALID ENDIAN"; |
| 211 return 0; } | |
| 212 | |
| 213 // Load only the ELF header and the sections header. These are the only | |
| 214 // sections loaded during library initialization. All the other sections | |
| 215 // will be loaded on demand when needed during the relocation process and, | |
| 216 // when possible, dumped after use. | |
| 217 static cyg_int32 | |
| 2243 | 218 cyg_ldr_load_sections(PELF_OBJECT p) |
| 2011 | 219 { |
| 2243 | 220 char *error_string; |
| 2011 | 221 cyg_int32 idx; |
| 222 | |
| 223 // Load the ELF header. | |
| 2243 | 224 p->p_elfhdr = (Elf32_Ehdr*)cyg_ldr_malloc(sizeof(Elf32_Ehdr)); |
| 225 CYG_ASSERT(p->p_elfhdr != 0, "Cannot malloc() p->p_elfhdr"); | |
| 226 if (p->p_elfhdr == 0) | |
| 2011 | 227 return -1; |
| 2243 | 228 p->seek(p, 0); |
| 229 p->read(p, sizeof(char), sizeof(Elf32_Ehdr), p->p_elfhdr ); | |
| 230 error_string = cyg_ldr_sanity_check(p); | |
| 231 if (error_string != 0) | |
| 2011 | 232 { |
| 233 cyg_ldr_last_error = "ERROR IN ELF HEADER"; | |
| 234 return -1; | |
| 235 } | |
| 236 | |
| 237 // Allocate an array that can hold an address to all the section of this | |
| 238 // library. This is not strictly optimal, since some sections do not | |
| 239 // need to be loaded all the time. Allocate an extra pointer for the | |
| 240 // COMMON area. | |
| 2243 | 241 p->sections = cyg_ldr_malloc((p->p_elfhdr->e_shnum + 1) * |
| 242 sizeof(cyg_uint32)); | |
| 243 CYG_ASSERT(p->sections != 0, "Cannot malloc() p->sections"); | |
| 244 if (p->sections == 0) | |
| 2011 | 245 { |
| 246 cyg_ldr_last_error = "ERROR IN MALLOC"; | |
| 247 return -1; | |
| 248 } | |
| 2243 | 249 memset(p->sections, 0, (p->p_elfhdr->e_shnum + 1) * |
| 250 sizeof(cyg_uint32)); | |
| 251 | |
| 252 // Now that the header is loaded, load the sections header. | |
| 253 p->p_sechdr = (Elf32_Shdr*)cyg_ldr_malloc( | |
| 254 p->p_elfhdr->e_shnum * p->p_elfhdr->e_shentsize); | |
| 255 CYG_ASSERT(p->p_sechdr != 0, "Cannot malloc() p->p_sechdr"); | |
| 256 if (p->p_sechdr == 0) | |
| 257 { | |
| 258 cyg_ldr_last_error = "ERROR IN MALLOC"; | |
| 259 return -1; | |
| 260 } | |
| 261 p->seek(p, p->p_elfhdr->e_shoff); | |
| 262 p->read(p, p->p_elfhdr->e_shentsize, p->p_elfhdr->e_shnum, p->p_sechdr); | |
| 2011 | 263 |
| 264 // Load the section header string table. This is a byte oriented table, | |
| 265 // so alignment is not an issue. | |
| 266 idx = p->p_elfhdr->e_shstrndx; | |
| 2243 | 267 p->sections[idx] = cyg_ldr_load_elf_section(p, idx); |
| 2011 | 268 return 0; |
| 269 } | |
| 270 | |
| 271 PELF_OBJECT | |
| 2243 | 272 cyg_ldr_open_library(CYG_ADDRWORD ptr, cyg_int32 mode) |
| 2011 | 273 { |
| 274 int (*fn)(void); | |
| 2243 | 275 int i; |
| 2011 | 276 |
| 277 // In the future there might be a switch() (against 'mode') that calls an | |
| 278 // open function other than cyg_ldr_open_library_fs(). These function | |
| 279 // fetch and open a library using ftp, http or libraries that are already | |
| 280 // in ROM. | |
| 2243 | 281 PELF_OBJECT e_obj = cyg_ldr_open_library_fs((char*)ptr); |
| 282 if (e_obj == 0) | |
| 2011 | 283 return 0; |
| 2243 | 284 int rc = cyg_ldr_load_sections(e_obj); |
| 285 if (rc != 0) | |
| 2011 | 286 { |
| 2243 | 287 cyg_ldr_free_elf_object(e_obj); |
| 2011 | 288 return 0; |
| 289 } | |
| 290 | |
| 291 // Find the section index for the .shstrtab section. The names of the | |
| 292 // sections are held here, and are the only way to identify them. | |
| 2243 | 293 char *p_shstrtab = (char*)cyg_ldr_section_address(e_obj, |
| 294 e_obj->p_elfhdr->e_shstrndx); | |
| 295 if (p_shstrtab == 0) | |
| 2011 | 296 { |
| 2243 | 297 cyg_ldr_free_elf_object(e_obj); |
| 2011 | 298 return 0; |
| 299 } | |
| 300 | |
| 301 // .symtab section and .strtab. We have to go through the section names | |
| 302 // to find where they are. | |
| 2243 | 303 for (i = 1; i < e_obj->p_elfhdr->e_shnum; i++) |
| 2011 | 304 { |
| 305 // Now look for the index of .symtab. These are the symbols needed for | |
| 306 // the symbol retrieval as well as relocation. | |
| 2243 | 307 if (!strcmp(p_shstrtab + e_obj->p_sechdr[i].sh_name, ELF_STRING_symtab)) |
| 2011 | 308 { |
| 309 e_obj->hdrndx_symtab = i; | |
| 2243 | 310 e_obj->sections[i] = cyg_ldr_load_elf_section(e_obj, i); |
| 311 if (e_obj->sections[i] == 0) | |
| 2011 | 312 { |
| 2243 | 313 cyg_ldr_free_elf_object(e_obj); |
| 2011 | 314 return 0; |
| 315 } | |
| 316 } | |
| 317 | |
| 318 // Load the table with the names of all the symbols. We need this | |
| 319 // to compare the name of external references symbols against the | |
| 320 // names in the in the CYG_HAL_TABLE provided by the user. | |
| 2243 | 321 if (!strcmp(p_shstrtab + e_obj->p_sechdr[i].sh_name, ELF_STRING_strtab)) |
| 2011 | 322 { |
| 323 e_obj->hdrndx_strtab = i; | |
| 2243 | 324 e_obj->sections[i] = cyg_ldr_load_elf_section(e_obj, i); |
| 325 if (e_obj->sections[i] == 0) | |
| 2011 | 326 { |
| 2243 | 327 cyg_ldr_free_elf_object(e_obj); |
| 2011 | 328 return 0; |
| 329 } | |
| 330 } | |
| 331 } | |
| 332 | |
| 2243 | 333 CYG_ASSERT(e_obj->hdrndx_symtab != 0, "No symtab index found"); |
| 334 CYG_ASSERT(e_obj->hdrndx_strtab != 0, "No strtab index found"); | |
| 2011 | 335 |
| 336 // Now look for symbols in the COMMON area. The COMMON symbols are a | |
| 337 // special case, because the area they reside in must be sized up | |
| 338 // and allocated separately from the other sections, which appear in | |
| 339 // the sections header and can be read out of the library itself. | |
| 340 // Extra room in the 'sections' array has already been allocated to hold | |
| 341 // the pointer to the commons area. | |
| 2243 | 342 cyg_uint32 common_size = cyg_ldr_find_common_size(e_obj); |
| 343 if (common_size != 0) | |
| 2011 | 344 { |
| 345 cyg_uint32 com_shndx = e_obj->p_elfhdr->e_shnum; | |
| 346 cyg_int32 com_offset = 0; | |
| 347 | |
| 2243 | 348 e_obj->sections[com_shndx] = (cyg_uint32*)cyg_ldr_malloc(common_size); |
| 349 CYG_ASSERT(e_obj->sections[com_shndx] != 0, | |
| 350 "Cannot malloc() the COMMONS"); | |
| 2011 | 351 |
| 2243 | 352 if (e_obj->sections[com_shndx] == 0) |
| 2011 | 353 { |
| 2243 | 354 cyg_ldr_free_elf_object(e_obj); |
| 2011 | 355 return 0; |
| 356 } | |
| 357 | |
| 358 // Now find all the symbols in the SHN_COMMON area and make | |
| 359 // them point to the newly allocated COM area. | |
| 2243 | 360 int symtab_entries = e_obj->p_sechdr[e_obj->hdrndx_symtab].sh_size / |
| 2011 | 361 e_obj->p_sechdr[e_obj->hdrndx_symtab].sh_entsize; |
| 2243 | 362 Elf32_Sym *p_symtab = (Elf32_Sym*)e_obj->sections[e_obj->hdrndx_symtab]; |
| 2011 | 363 |
| 364 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 1 | |
| 2243 | 365 diag_printf("Num Value Size Ndx Name\n"); |
| 2011 | 366 #endif |
| 2243 | 367 for (i = 1; i < symtab_entries; i++) |
| 2011 | 368 { |
| 369 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 1 | |
| 2243 | 370 cyg_uint8 *p_strtab = (cyg_uint8*)cyg_ldr_section_address(e_obj, |
| 371 e_obj->hdrndx_strtab); | |
| 2011 | 372 #endif |
| 2243 | 373 if (p_symtab[i].st_shndx == SHN_COMMON) |
| 2011 | 374 { |
| 375 cyg_uint32 boundary = p_symtab[i].st_value - 1; | |
| 376 // Calculate the next byte boundary. | |
| 2243 | 377 com_offset = (com_offset + boundary) & ~boundary; |
| 2011 | 378 p_symtab[i].st_shndx = com_shndx; |
| 379 p_symtab[i].st_value = com_offset; | |
| 380 com_offset += p_symtab[i].st_size; | |
| 381 } | |
| 382 | |
| 383 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 1 | |
| 2243 | 384 diag_printf("%03d %08X %04X %03d %s\n", |
| 2011 | 385 i, |
| 386 p_symtab[i].st_value, | |
| 387 p_symtab[i].st_size, | |
| 388 p_symtab[i].st_shndx, | |
| 2243 | 389 p_strtab + p_symtab[i].st_name); |
| 2011 | 390 #endif |
| 391 } | |
| 392 } | |
| 393 | |
| 394 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 0 | |
| 2243 | 395 cyg_ldr_print_section_data(e_obj); |
| 2011 | 396 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 1 |
| 2243 | 397 cyg_ldr_print_symbol_names(e_obj); |
| 2011 | 398 #endif |
| 399 #endif | |
| 400 | |
| 2243 | 401 for (i = 1; i < e_obj->p_elfhdr->e_shnum; i++) |
| 2011 | 402 { |
| 403 // Find all the '.rel' or '.rela' sections and relocate them. | |
| 2243 | 404 if ((e_obj->p_sechdr[i].sh_type == SHT_REL) || |
| 405 (e_obj->p_sechdr[i].sh_type == SHT_RELA)) | |
| 2011 | 406 { |
| 407 // Load and relocate the section. | |
| 2243 | 408 rc = cyg_ldr_relocate_section(e_obj, i); |
| 409 if (rc < 0) | |
| 2011 | 410 { |
| 411 #if CYGPKG_SERVICES_OBJLOADER_DEBUG_LEVEL > 0 | |
| 2243 | 412 ELFDEBUG("Relocation unsuccessful\n"); |
| 2011 | 413 #endif |
| 2243 | 414 cyg_ldr_free_elf_object(e_obj); |
| 2011 | 415 return 0; |
| 416 } | |
| 417 } | |
| 418 } | |
| 419 | |
| 420 // Synch up the caches before calling any function in the library. | |
| 421 cyg_ldr_flush_cache(); | |
| 422 | |
| 423 // Run the library initialization code. | |
| 2243 | 424 fn = cyg_ldr_find_symbol(e_obj, "library_open"); |
| 425 if (fn != 0) | |
| 2011 | 426 fn(); |
| 427 return ((void*)e_obj); | |
| 428 } | |
| 429 | |
| 2243 | 430 char |
| 431 *cyg_ldr_error(void) | |
| 2011 | 432 { |
| 2243 | 433 char* p = cyg_ldr_last_error; |
| 2011 | 434 cyg_ldr_last_error = NULL; |
| 435 return p; | |
| 436 } | |
| 437 | |
| 2243 | 438 void cyg_ldr_close_library(void* handle) |
| 2011 | 439 { |
| 440 void (*fn)(void); | |
| 441 | |
| 442 PELF_OBJECT p = (PELF_OBJECT)handle; | |
| 2243 | 443 fn = cyg_ldr_find_symbol(p, "library_close"); |
| 444 if (fn != 0) | |
| 2011 | 445 fn(); |
| 446 | |
| 2243 | 447 cyg_ldr_free_elf_object(p); |
| 2011 | 448 p = 0; |
| 449 } | |
| 450 |
