annotate packages/services/objloader/current/src/objloader.c @ 2729:74dbf4c3f2e1 after-copyright-change-20090129

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