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