[ipc/ipcdev.git] / qnx / src / ipc3x_dev / ti / syslink / procMgr / hlos / knl / loaders / Elf / Qnx / DLOAD / DLWRAPPER / dlw_client.c
1 /*
2 * dlw_client.c
3 *
4 * RIDL implementation of client functions required by dynamic loader API.
5 * Please see list of client-required API functions in dload_api.h.
6 *
7 * This implementation of RIDL is expected to run on the DSP. It uses C6x
8 * RTS functions for file I/O and memory management (both host and target
9 * memory).
10 *
11 * A loader that runs on a GPP for the purposes of loading C6x code onto a
12 * DSP will likely need to re-write all of the functions contained in this
13 * module.
14 *
15 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
16 *
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 *
22 * Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 *
25 * Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the
28 * distribution.
29 *
30 * Neither the name of Texas Instruments Incorporated nor the names of
31 * its contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
48 #include "ArrayList.h"
49 #include "dload_api.h"
50 #include <stdarg.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include "dlw_debug.h"
54 #include "dlw_dsbt.h"
55 #include "dlw_trgmem.h"
57 #define LOADER_DEBUG 0
58 /*****************************************************************************/
59 /* Client Provided File I/O */
60 /*****************************************************************************/
61 /*****************************************************************************/
62 /* DLIF_FSEEK() - Seek to a position in specified file. */
63 /*****************************************************************************/
64 int DLIF_fseek(LOADER_FILE_DESC *stream, int32_t offset, int origin)
65 {
66 return fseek(stream, offset, origin);
67 }
69 /*****************************************************************************/
70 /* DLIF_FTELL() - Return the current position in the given file. */
71 /*****************************************************************************/
72 int32_t DLIF_ftell(LOADER_FILE_DESC *stream)
73 {
74 return ftell(stream);
75 }
77 /*****************************************************************************/
78 /* DLIF_FREAD() - Read data from file into a host-accessible data buffer */
79 /* that can be accessed through "ptr". */
80 /*****************************************************************************/
81 size_t DLIF_fread(void *ptr, size_t size, size_t nmemb,
82 LOADER_FILE_DESC *stream)
83 {
84 return fread(ptr, size, nmemb, stream);
85 }
87 /*****************************************************************************/
88 /* DLIF_FCLOSE() - Close a file that was opened on behalf of the core */
89 /* loader. Core loader has ownership of the file pointer, but client */
90 /* has access to file system. */
91 /*****************************************************************************/
92 int32_t DLIF_fclose(LOADER_FILE_DESC *fd)
93 {
94 return fclose(fd);
95 }
97 /*****************************************************************************/
98 /* Client Provided Host Memory Management */
99 /*****************************************************************************/
100 /*****************************************************************************/
101 /* DLIF_MALLOC() - Allocate host memory suitable for loader scratch space. */
102 /*****************************************************************************/
103 void* DLIF_malloc(size_t size)
104 {
105 return malloc(size*sizeof(uint8_t));
106 }
108 /*****************************************************************************/
109 /* DLIF_FREE() - Free host memory previously allocated with DLIF_malloc(). */
110 /*****************************************************************************/
111 void DLIF_free(void* ptr)
112 {
113 free(ptr);
114 }
116 /*****************************************************************************/
117 /* Client Provided Target Memory Management */
118 /*****************************************************************************/
120 /*****************************************************************************/
121 /* RIDL-Specific hack to facilitate target memory allocation. */
122 /*****************************************************************************/
123 /*****************************************************************************/
124 /* DLIF_ALLOCATE() - Return the load address of the segment/section */
125 /* described in its parameters and record the run address in */
126 /* run_address field of DLOAD_MEMORY_REQUEST. */
127 /*****************************************************************************/
128 BOOL DLIF_allocate(struct DLOAD_MEMORY_REQUEST *targ_req)
129 {
130 /*------------------------------------------------------------------------*/
131 /* Get pointers to API segment and file descriptors. */
132 /*------------------------------------------------------------------------*/
133 struct DLOAD_MEMORY_SEGMENT* obj_desc = targ_req->segment;
134 LOADER_FILE_DESC* f = targ_req->fp;
136 /*------------------------------------------------------------------------*/
137 /* Request target memory for this segment from the "blob". */
138 /*------------------------------------------------------------------------*/
139 if (!DLTMM_malloc(targ_req, obj_desc))
140 {
141 DLIF_error(DLET_MEMORY, "Failed to allocate target memory for segment.\n");
142 return 0;
143 }
145 /*------------------------------------------------------------------------*/
146 /* As required by API, copy the described segment into memory from file. */
147 /* We're the client, not the loader, so we can use fseek() and fread(). */
148 /*------------------------------------------------------------------------*/
149 /* ??? I don't think we want to do this if we are allocating target */
150 /* memory for the run only placement of this segment. If it is the */
151 /* load placement or both load and run placement, then we can do the */
152 /* copy. */
153 /*------------------------------------------------------------------------*/
154 memset(targ_req->host_address, 0, obj_desc->memsz_in_bytes);
155 fseek(f,targ_req->offset,SEEK_SET);
156 fread(targ_req->host_address,obj_desc->objsz_in_bytes,1,f);
158 /*------------------------------------------------------------------------*/
159 /* Once we have target address for this allocation, add debug information */
160 /* about this segment to the debug record for the module that is */
161 /* currently being loaded. */
162 /*------------------------------------------------------------------------*/
163 if (DLL_debug)
164 {
165 /*---------------------------------------------------------------------*/
166 /* Add information about this segment's location to the segment debug */
167 /* information associated with the module that is currently being */
168 /* loaded. */
169 /*---------------------------------------------------------------------*/
170 /* ??? We need a way to determine whether the target address in the */
171 /* segment applies to the load address of the segment or the */
172 /* run address. For the time being, we assume that it applies */
173 /* to both (that is, the dynamic loader does not support */
174 /* separate load and run placement for a given segment). */
175 /*---------------------------------------------------------------------*/
176 DLDBG_add_segment_record(obj_desc);
177 }
179 #if LOADER_DEBUG
180 if (debugging_on)
181 printf("DLIF_allocate: buffer 0x%x\n", targ_req->host_address);
182 #endif
184 /*------------------------------------------------------------------------*/
185 /* Target memory request was successful. */
186 /*------------------------------------------------------------------------*/
187 return 1;
188 }
190 /*****************************************************************************/
191 /* DLIF_RELEASE() - Unmap or free target memory that was previously */
192 /* allocated by DLIF_allocate(). */
193 /*****************************************************************************/
194 BOOL DLIF_release(struct DLOAD_MEMORY_SEGMENT* ptr)
195 {
196 #if LOADER_DEBUG
197 if (debugging_on)
198 printf("DLIF_free: %d bytes starting at 0x%x\n",
199 ptr->memsz_in_bytes, ptr->target_address);
200 #endif
202 /*------------------------------------------------------------------------*/
203 /* Find the target memory packet associated with this address and mark it */
204 /* as available (will also merge with adjacent free packets). */
205 /*------------------------------------------------------------------------*/
206 DLTMM_free(ptr->target_address);
208 return 1;
209 }
211 /*****************************************************************************/
212 /* DLIF_COPY() - Copy data from file to host-accessible memory. */
213 /* Returns a host pointer to the data in the host_address field of the */
214 /* DLOAD_MEMORY_REQUEST object. */
215 /*****************************************************************************/
216 BOOL DLIF_copy(struct DLOAD_MEMORY_REQUEST* targ_req)
217 {
218 targ_req->host_address = (void*)(targ_req->segment->target_address);
219 return 1;
220 }
222 /*****************************************************************************/
223 /* DLIF_READ() - Read content from target memory address into host- */
224 /* accessible buffer. */
225 /*****************************************************************************/
226 BOOL DLIF_read(void *ptr, size_t size, size_t nmemb, TARGET_ADDRESS src)
227 {
228 if (!memcpy(ptr, (const void *)src, size * nmemb))
229 return 0;
231 return 1;
232 }
234 /*****************************************************************************/
235 /* DLIF_WRITE() - Write updated (relocated) segment contents to target */
236 /* memory. */
237 /*****************************************************************************/
238 BOOL DLIF_write(struct DLOAD_MEMORY_REQUEST* req)
239 {
240 /*------------------------------------------------------------------------*/
241 /* Nothing to do since we are relocating directly into target memory. */
242 /*------------------------------------------------------------------------*/
243 return 1;
244 }
246 /*****************************************************************************/
247 /* DLIF_EXECUTE() - Transfer control to specified target address. */
248 /*****************************************************************************/
249 int32_t DLIF_execute(TARGET_ADDRESS exec_addr)
250 {
251 /*------------------------------------------------------------------------*/
252 /* This call will only work if the host and target are the same instance. */
253 /* The compiler may warn about this conversion from an object to a */
254 /* function pointer. */
255 /*------------------------------------------------------------------------*/
256 return ((int32_t(*)())(exec_addr))();
257 }
260 /*****************************************************************************/
261 /* Client Provided Communication Mechanisms to assist with creation of */
262 /* DLLView debug information. Client needs to know exactly when a segment */
263 /* is being loaded or unloaded so that it can keep its debug information */
264 /* up to date. */
265 /*****************************************************************************/
266 /*****************************************************************************/
267 /* DLIF_LOAD_DEPENDENT() - Perform whatever maintenance is needed in the */
268 /* client when loading of a dependent file is initiated by the core */
269 /* loader. Open the dependent file on behalf of the core loader, */
270 /* then invoke the core loader to get it into target memory. The core */
271 /* loader assumes ownership of the dependent file pointer and must ask */
272 /* the client to close the file when it is no longer needed. */
273 /* */
274 /* If debug support is needed under the Braveheart model, then create */
275 /* a host version of the debug module record for this object. This */
276 /* version will get updated each time we allocate target memory for a */
277 /* segment that belongs to this module. When the load returns, the */
278 /* client will allocate memory for the debug module from target memory */
279 /* and write the host version of the debug module into target memory */
280 /* at the appropriate location. After this takes place the new debug */
281 /* module needs to be added to the debug module list. The client will */
282 /* need to update the tail of the DLModules list to link the new debug */
283 /* module onto the end of the list. */
284 /* */
285 /*****************************************************************************/
286 int DLIF_load_dependent(const char* so_name)
287 {
288 /*------------------------------------------------------------------------*/
289 /* Find the path and file name associated with the given so_name in the */
290 /* client's file registry. */
291 /*------------------------------------------------------------------------*/
292 /* If we can't find the so_name in the file registry (or if the registry */
293 /* is not implemented yet), we'll open the file using the so_name. */
294 /*------------------------------------------------------------------------*/
295 int to_ret = 0;
296 FILE* fp = fopen(so_name, "rb");
298 /*------------------------------------------------------------------------*/
299 /* We need to make sure that the file was properly opened. */
300 /*------------------------------------------------------------------------*/
301 if (!fp)
302 {
303 DLIF_error(DLET_FILE, "Can't open dependent file '%s'.\n", so_name);
304 return 0;
305 }
307 /*------------------------------------------------------------------------*/
308 /* If the dynamic loader is providing debug support for a DLL View plug- */
309 /* in or script of some sort, then we are going to create a host version */
310 /* of the debug module record for the so_name module. */
311 /*------------------------------------------------------------------------*/
312 /* In the Braveheart view of the world, debug support is only to be */
313 /* provided if the DLModules symbol is defined in the base image. */
314 /* We will set up a DLL_debug flag when the command to load the base */
315 /* image is issued. */
316 /*------------------------------------------------------------------------*/
317 if (DLL_debug)
318 DLDBG_add_host_record(so_name);
320 /*------------------------------------------------------------------------*/
321 /* Tell the core loader to proceed with loading the module. */
322 /*------------------------------------------------------------------------*/
323 /* Note that the client is turning ownership of the file pointer over to */
324 /* the core loader at this point. The core loader will need to ask the */
325 /* client to close the dependent file when it is done using the dependent */
326 /* file pointer. */
327 /*------------------------------------------------------------------------*/
328 to_ret = DLOAD_load(fp, 0, NULL);
330 /*------------------------------------------------------------------------*/
331 /* If the dependent load was successful, update the DLModules list in */
332 /* target memory as needed. */
333 /*------------------------------------------------------------------------*/
334 if (to_ret != 0)
335 {
336 /*---------------------------------------------------------------------*/
337 /* We will need to copy the information from our host version of the */
338 /* debug record into actual target memory. */
339 /*---------------------------------------------------------------------*/
340 if (DLL_debug)
341 {
342 /*---------------------------------------------------------------*/
343 /* Allocate target memory for the module's debug record. Use */
344 /* host version of the debug information to determine how much */
345 /* target memory we need and how it is to be filled in. */
346 /*---------------------------------------------------------------*/
347 /* Note that we don't go through the normal API functions to get */
348 /* target memory and write the debug information since we're not */
349 /* dealing with object file content here. The DLL View debug is */
350 /* supported entirely on the client side. */
351 /*---------------------------------------------------------------*/
352 DLDBG_add_target_record(to_ret);
353 }
354 }
356 /*------------------------------------------------------------------------*/
357 /* Report failure to load dependent. */
358 /*------------------------------------------------------------------------*/
359 else
360 DLIF_error(DLET_MISC, "Failed load of dependent file '%s'.\n", so_name);
362 return to_ret;
363 }
365 /*****************************************************************************/
366 /* DLIF_UNLOAD_DEPENDENT() - Perform whatever maintenance is needed in the */
367 /* client when unloading of a dependent file is initiated by the core */
368 /* loader. Invoke the DLOAD_unload() function to get the core loader */
369 /* to release any target memory that is associated with the dependent */
370 /* file's segments. */
371 /*****************************************************************************/
372 void DLIF_unload_dependent(uint32_t handle)
373 {
374 /*------------------------------------------------------------------------*/
375 /* If the specified module is no longer needed, DLOAD_load() will spin */
376 /* through the object descriptors associated with the module and free up */
377 /* target memory that was allocated to any segment in the module. */
378 /*------------------------------------------------------------------------*/
379 /* If DLL debugging is enabled, find module debug record associated with */
380 /* this module and remove it from the DLL debug list. */
381 /*------------------------------------------------------------------------*/
382 if (DLOAD_unload(handle))
383 {
384 DSBT_release_entry(handle);
385 if (DLL_debug) DLDBG_rm_target_record(handle);
386 }
387 }
389 /*****************************************************************************/
390 /* Client Provided API Functions to Support Logging Warnings/Errors */
391 /*****************************************************************************/
393 /*****************************************************************************/
394 /* DLIF_WARNING() - Write out a warning message from the core loader. */
395 /*****************************************************************************/
396 void DLIF_warning(LOADER_WARNING_TYPE wtype, const char *fmt, ...)
397 {
398 va_list ap;
399 va_start(ap,fmt);
400 printf("<< D L O A D >> WARNING: ");
401 vprintf(fmt,ap);
402 va_end(ap);
403 }
405 /*****************************************************************************/
406 /* DLIF_ERROR() - Write out an error message from the core loader. */
407 /*****************************************************************************/
408 void DLIF_error(LOADER_ERROR_TYPE etype, const char *fmt, ...)
409 {
410 va_list ap;
411 va_start(ap,fmt);
412 printf("<< D L O A D >> ERROR: ");
413 vprintf(fmt,ap);
414 va_end(ap);
415 }
417 /*****************************************************************************
418 * END API FUNCTION DEFINITIONS
419 *****************************************************************************/