1 |
786 |
skrzyp |
/* =================================================================
|
2 |
|
|
*
|
3 |
|
|
* test_mods.c
|
4 |
|
|
*
|
5 |
|
|
* Test program for the object loader.
|
6 |
|
|
*
|
7 |
|
|
* =================================================================
|
8 |
|
|
* ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
* -------------------------------------------
|
10 |
|
|
* This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
* Copyright (C) 2005, 2008 Free Software Foundation, Inc.
|
12 |
|
|
*
|
13 |
|
|
* eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
* the terms of the GNU General Public License as published by the Free
|
15 |
|
|
* Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
* version.
|
17 |
|
|
*
|
18 |
|
|
* eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
* for more details.
|
22 |
|
|
*
|
23 |
|
|
* You should have received a copy of the GNU General Public License
|
24 |
|
|
* along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
*
|
27 |
|
|
* As a special exception, if other files instantiate templates or use
|
28 |
|
|
* macros or inline functions from this file, or you compile this file
|
29 |
|
|
* and link it with other works to produce a work based on this file,
|
30 |
|
|
* this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
* the GNU General Public License. However the source code for this file
|
32 |
|
|
* must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
* General Public License v2.
|
34 |
|
|
*
|
35 |
|
|
* This exception does not invalidate any other reasons why a work based
|
36 |
|
|
* on this file might be covered by the GNU General Public License.
|
37 |
|
|
* -------------------------------------------
|
38 |
|
|
* ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
* =================================================================
|
40 |
|
|
* #####DESCRIPTIONBEGIN####
|
41 |
|
|
*
|
42 |
|
|
* Author(s): Anthony Tonizzo (atonizzo@gmail.com)
|
43 |
|
|
* Date: 2005-05-13
|
44 |
|
|
* Purpose:
|
45 |
|
|
* Description:
|
46 |
|
|
*
|
47 |
|
|
* ####DESCRIPTIONEND####
|
48 |
|
|
*
|
49 |
|
|
* =================================================================
|
50 |
|
|
*/
|
51 |
|
|
|
52 |
|
|
#include <pkgconf/system.h>
|
53 |
|
|
#include <cyg/kernel/kapi.h> // Kernel API.
|
54 |
|
|
#include <cyg/infra/diag.h> // For diagnostic printing.
|
55 |
|
|
#include <cyg/infra/testcase.h>
|
56 |
|
|
|
57 |
|
|
#include <unistd.h>
|
58 |
|
|
#include <stdlib.h>
|
59 |
|
|
#include <fcntl.h>
|
60 |
|
|
#include <sys/stat.h>
|
61 |
|
|
#include <errno.h>
|
62 |
|
|
#include <dirent.h>
|
63 |
|
|
#include <stdio.h>
|
64 |
|
|
|
65 |
|
|
#include <cyg/objloader/elf.h>
|
66 |
|
|
#include <cyg/objloader/objelf.h>
|
67 |
|
|
|
68 |
|
|
#ifdef CYGPKG_IO_FILEIO
|
69 |
|
|
#include <cyg/fileio/fileio.h>
|
70 |
|
|
#endif
|
71 |
|
|
|
72 |
|
|
// Test ROMFS data. Two example data files are generated so that
|
73 |
|
|
// the test will work on both big-endian and little-endian targets.
|
74 |
|
|
#if (CYG_BYTEORDER == CYG_LSBFIRST)
|
75 |
|
|
# include <cyg/objloader/testromfs_le.h>
|
76 |
|
|
#else
|
77 |
|
|
# include <cyg/objloader/testromfs_be.h>
|
78 |
|
|
#endif
|
79 |
|
|
|
80 |
|
|
//==========================================================================
|
81 |
|
|
|
82 |
|
|
MTAB_ENTRY(romfs_mte1,
|
83 |
|
|
"/",
|
84 |
|
|
"romfs",
|
85 |
|
|
"",
|
86 |
|
|
(CYG_ADDRWORD) &filedata[0]);
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
//==========================================================================
|
90 |
|
|
|
91 |
|
|
#define THREAD_STACK_SIZE 8192
|
92 |
|
|
#define THREAD_PRIORITY 12
|
93 |
|
|
|
94 |
|
|
cyg_uint8 thread_a_stack[THREAD_STACK_SIZE] __attribute__((__aligned__ (16)));
|
95 |
|
|
cyg_uint8 thread_b_stack[THREAD_STACK_SIZE] __attribute__((__aligned__ (16)));
|
96 |
|
|
|
97 |
|
|
cyg_thread thread_a_obj;
|
98 |
|
|
cyg_thread thread_b_obj;
|
99 |
|
|
cyg_handle_t thread_a_hdl;
|
100 |
|
|
cyg_handle_t thread_b_hdl;
|
101 |
|
|
|
102 |
|
|
#define SHOW_RESULT(_fn, _res) \
|
103 |
|
|
diag_printf("<FAIL>: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
|
104 |
|
|
|
105 |
|
|
int weak_fn_called = 0;
|
106 |
|
|
|
107 |
|
|
void weak_function(void)
|
108 |
|
|
{
|
109 |
|
|
CYG_TEST_PASS("Applications weak function called");
|
110 |
|
|
weak_fn_called++;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
void (*thread_a)(cyg_addrword_t);
|
114 |
|
|
void (*thread_b)(cyg_addrword_t);
|
115 |
|
|
|
116 |
|
|
// This is the main starting point for our example application.
|
117 |
|
|
void cyg_user_start(void)
|
118 |
|
|
{
|
119 |
|
|
int err;
|
120 |
|
|
void* lib_handle;
|
121 |
|
|
void (*fn)(void);
|
122 |
|
|
|
123 |
|
|
CYG_TEST_INIT();
|
124 |
|
|
|
125 |
|
|
CYG_TEST_INFO("Object loader module test started");
|
126 |
|
|
|
127 |
|
|
err = chdir("/");
|
128 |
|
|
|
129 |
|
|
if(err < 0)
|
130 |
|
|
SHOW_RESULT(chdir, err);
|
131 |
|
|
|
132 |
|
|
lib_handle = cyg_ldr_open_library((CYG_ADDRWORD)"/hello.o",
|
133 |
|
|
CYG_LDR_MODE_FILESYSTEM);
|
134 |
|
|
CYG_TEST_CHECK(lib_handle , "Unable to load object file to load");
|
135 |
|
|
|
136 |
|
|
fn = cyg_ldr_find_symbol(lib_handle, "print_message");
|
137 |
|
|
CYG_TEST_CHECK(fn , "Unable to find print_message function");
|
138 |
|
|
|
139 |
|
|
fn();
|
140 |
|
|
|
141 |
|
|
fn = cyg_ldr_find_symbol(lib_handle, "weak_function");
|
142 |
|
|
CYG_TEST_CHECK(fn , "Unable to find weak_function");
|
143 |
|
|
|
144 |
|
|
fn();
|
145 |
|
|
|
146 |
|
|
fn = cyg_ldr_find_symbol (lib_handle, "unresolvable_symbol");
|
147 |
|
|
CYG_TEST_CHECK(!fn , "Found none existing symbol!");
|
148 |
|
|
|
149 |
|
|
thread_a = cyg_ldr_find_symbol(lib_handle, "thread_a");
|
150 |
|
|
thread_b = cyg_ldr_find_symbol(lib_handle, "thread_b");
|
151 |
|
|
CYG_TEST_CHECK(thread_a && thread_b , "Unable to find thread functions");
|
152 |
|
|
|
153 |
|
|
// Create our two threads.
|
154 |
|
|
cyg_thread_create(THREAD_PRIORITY,
|
155 |
|
|
thread_a,
|
156 |
|
|
(cyg_addrword_t) 75,
|
157 |
|
|
"Thread A",
|
158 |
|
|
(void *)thread_a_stack,
|
159 |
|
|
THREAD_STACK_SIZE,
|
160 |
|
|
&thread_a_hdl,
|
161 |
|
|
&thread_a_obj);
|
162 |
|
|
|
163 |
|
|
cyg_thread_create(THREAD_PRIORITY + 1,
|
164 |
|
|
thread_b,
|
165 |
|
|
(cyg_addrword_t) 68,
|
166 |
|
|
"Thread B",
|
167 |
|
|
(void *)thread_b_stack,
|
168 |
|
|
THREAD_STACK_SIZE,
|
169 |
|
|
&thread_b_hdl,
|
170 |
|
|
&thread_b_obj);
|
171 |
|
|
|
172 |
|
|
// Resume the threads so they start when the scheduler begins.
|
173 |
|
|
cyg_thread_resume(thread_a_hdl);
|
174 |
|
|
cyg_thread_resume(thread_b_hdl);
|
175 |
|
|
|
176 |
|
|
cyg_scheduler_start();
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
// -----------------------------------------------------------------------------
|
180 |
|
|
// External symbols.
|
181 |
|
|
// -----------------------------------------------------------------------------
|
182 |
|
|
CYG_LDR_TABLE_KAPI_ALARM()
|
183 |
|
|
CYG_LDR_TABLE_KAPI_CLOCK()
|
184 |
|
|
CYG_LDR_TABLE_KAPI_COND()
|
185 |
|
|
CYG_LDR_TABLE_KAPI_COUNTER()
|
186 |
|
|
CYG_LDR_TABLE_KAPI_EXCEPTIONS()
|
187 |
|
|
CYG_LDR_TABLE_KAPI_FLAG()
|
188 |
|
|
CYG_LDR_TABLE_KAPI_INTERRUPTS()
|
189 |
|
|
CYG_LDR_TABLE_KAPI_MBOX()
|
190 |
|
|
CYG_LDR_TABLE_KAPI_MEMPOOL_FIX()
|
191 |
|
|
CYG_LDR_TABLE_KAPI_MEMPOOL_VAR()
|
192 |
|
|
CYG_LDR_TABLE_KAPI_MUTEX()
|
193 |
|
|
CYG_LDR_TABLE_KAPI_SCHEDULER()
|
194 |
|
|
CYG_LDR_TABLE_KAPI_SEMAPHORE()
|
195 |
|
|
CYG_LDR_TABLE_KAPI_THREAD()
|
196 |
|
|
CYG_LDR_TABLE_STRING()
|
197 |
|
|
CYG_LDR_TABLE_STDIO()
|
198 |
|
|
CYG_LDR_TABLE_INFRA_DIAG()
|
199 |
|
|
|
200 |
|
|
// Test case infrastructure function
|
201 |
|
|
CYG_LDR_TABLE_ENTRY(cyg_test_output_entry, "cyg_test_output", cyg_test_output);
|
202 |
|
|
CYG_LDR_TABLE_ENTRY(cyg_test_exit_entry, "cyg_test_exit", cyg_test_exit);
|
203 |
|
|
|
204 |
|
|
// Test function
|
205 |
|
|
CYG_LDR_TABLE_ENTRY(weak_function_entry, "weak_function", weak_function);
|
206 |
|
|
|
207 |
|
|
// Test Variable
|
208 |
|
|
CYG_LDR_TABLE_ENTRY(weak_fn_called_entry, "weak_fn_called", &weak_fn_called);
|