annotate packages/services/objloader/current/src/objloader.c @ 2243:6fd46febe457

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