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 "types.h"
49 #include "ewrap.h"
50 #include "ArrayList.h"
51 #include "dload_api.h"
52 #include <stdarg.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include "file_ovr.h"
57 #if 0
58 #include "dlw_debug.h"
59 #include "dlw_dsbt.h"
60 #include "dlw_trgmem.h"
61 #endif
63 /*****************************************************************************/
64 /* Client Provided File I/O */
65 /*****************************************************************************/
66 /*****************************************************************************/
67 /* DLIF_FSEEK() - Seek to a position in specified file. */
68 /*****************************************************************************/
69 int DLIF_fseek(LOADER_FILE_DESC *stream, int32_t offset, int origin)
70 {
71 return fseek(stream, offset, origin);
72 }
74 /*****************************************************************************/
75 /* DLIF_FTELL() - Return the current position in the given file. */
76 /*****************************************************************************/
77 int32_t DLIF_ftell(LOADER_FILE_DESC *stream)
78 {
79 return ftell(stream);
80 }
82 /*****************************************************************************/
83 /* DLIF_FREAD() - Read data from file into a host-accessible data buffer */
84 /* that can be accessed through "ptr". */
85 /*****************************************************************************/
86 size_t DLIF_fread(void *ptr, size_t size, size_t nmemb,
87 LOADER_FILE_DESC *stream)
88 {
89 return fread(ptr, size, nmemb, stream);
90 }
92 /*****************************************************************************/
93 /* DLIF_FCLOSE() - Close a file that was opened on behalf of the core */
94 /* loader. Core loader has ownership of the file pointer, but client */
95 /* has access to file system. */
96 /*****************************************************************************/
97 int32_t DLIF_fclose(LOADER_FILE_DESC *fd)
98 {
99 return fclose(fd);
100 }
102 /*****************************************************************************/
103 /* Client Provided Host Memory Management */
104 /*****************************************************************************/
105 /*****************************************************************************/
106 /* DLIF_MALLOC() - Allocate host memory suitable for loader scratch space. */
107 /*****************************************************************************/
108 void* DLIF_malloc(size_t size)
109 {
110 return malloc(size*sizeof(uint8_t));
111 }
113 /*****************************************************************************/
114 /* DLIF_FREE() - Free host memory previously allocated with DLIF_malloc(). */
115 /*****************************************************************************/
116 void DLIF_free(void* ptr)
117 {
118 free(ptr);
119 }
121 /*****************************************************************************/
122 /* Client Provided Target Memory Management */
123 /*****************************************************************************/
125 /*****************************************************************************/
126 /* RIDL-Specific hack to facilitate target memory allocation. */
127 /*****************************************************************************/
128 /*****************************************************************************/
129 /* DLIF_ALLOCATE() - Return the load address of the segment/section */
130 /* described in its parameters and record the run address in */
131 /* run_address field of DLOAD_MEMORY_REQUEST. */
132 /*****************************************************************************/
133 BOOL DLIF_allocate(struct DLOAD_MEMORY_REQUEST *targ_req)
134 {
135 /*------------------------------------------------------------------------*/
136 /* Get pointers to API segment and file descriptors. */
137 /*------------------------------------------------------------------------*/
138 struct DLOAD_MEMORY_SEGMENT* obj_desc = targ_req->segment;
139 LOADER_FILE_DESC* f = targ_req->fp;
141 /*------------------------------------------------------------------------*/
142 /* Request target memory for this segment from the "blob". */
143 /*------------------------------------------------------------------------*/
144 if (!DLTMM_malloc(targ_req, obj_desc))
145 {
146 DLIF_error(DLET_MEMORY, "Failed to allocate target memory for segment.\n");
147 return 0;
148 }
150 /*------------------------------------------------------------------------*/
151 /* As required by API, copy the described segment into memory from file. */
152 /* We're the client, not the loader, so we can use fseek() and fread(). */
153 /*------------------------------------------------------------------------*/
154 /* ??? I don't think we want to do this if we are allocating target */
155 /* memory for the run only placement of this segment. If it is the */
156 /* load placement or both load and run placement, then we can do the */
157 /* copy. */
158 /*------------------------------------------------------------------------*/
159 memset(targ_req->host_address, 0, obj_desc->memsz_in_bytes);
160 fseek(f,targ_req->offset,SEEK_SET);
161 fread(targ_req->host_address,obj_desc->objsz_in_bytes,1,f);
163 /*------------------------------------------------------------------------*/
164 /* Once we have target address for this allocation, add debug information */
165 /* about this segment to the debug record for the module that is */
166 /* currently being loaded. */
167 /*------------------------------------------------------------------------*/
168 if (DLL_debug)
169 {
170 /*---------------------------------------------------------------------*/
171 /* Add information about this segment's location to the segment debug */
172 /* information associated with the module that is currently being */
173 /* loaded. */
174 /*---------------------------------------------------------------------*/
175 /* ??? We need a way to determine whether the target address in the */
176 /* segment applies to the load address of the segment or the */
177 /* run address. For the time being, we assume that it applies */
178 /* to both (that is, the dynamic loader does not support */
179 /* separate load and run placement for a given segment). */
180 /*---------------------------------------------------------------------*/
181 DLDBG_add_segment_record(obj_desc);
182 }
184 #if LOADER_DEBUG
185 if (debugging_on)
186 printf("DLIF_allocate: buffer 0x%x\n", targ_req->host_address);
187 #endif
189 /*------------------------------------------------------------------------*/
190 /* Target memory request was successful. */
191 /*------------------------------------------------------------------------*/
192 return 1;
193 }
195 /*****************************************************************************/
196 /* DLIF_RELEASE() - Unmap or free target memory that was previously */
197 /* allocated by DLIF_allocate(). */
198 /*****************************************************************************/
199 BOOL DLIF_release(struct DLOAD_MEMORY_SEGMENT* ptr)
200 {
201 #if LOADER_DEBUG
202 if (debugging_on)
203 printf("DLIF_free: %d bytes starting at 0x%x\n",
204 ptr->memsz_in_bytes, ptr->target_address);
205 #endif
207 /*------------------------------------------------------------------------*/
208 /* Find the target memory packet associated with this address and mark it */
209 /* as available (will also merge with adjacent free packets). */
210 /*------------------------------------------------------------------------*/
211 DLTMM_free(ptr->target_address);
213 return 1;
214 }
216 /*****************************************************************************/
217 /* DLIF_COPY() - Copy data from file to host-accessible memory. */
218 /* Returns a host pointer to the data in the host_address field of the */
219 /* DLOAD_MEMORY_REQUEST object. */
220 /*****************************************************************************/
221 BOOL DLIF_copy(struct DLOAD_MEMORY_REQUEST* targ_req)
222 {
223 targ_req->host_address = (void*)(targ_req->segment->target_address);
224 return 1;
225 }
227 /*****************************************************************************/
228 /* DLIF_READ() - Read content from target memory address into host- */
229 /* accessible buffer. */
230 /*****************************************************************************/
231 BOOL DLIF_read(void *ptr, size_t size, size_t nmemb, TARGET_ADDRESS src)
232 {
233 if (!memcpy(ptr, (const void *)src, size * nmemb))
234 return 0;
236 return 1;
237 }
239 /*****************************************************************************/
240 /* DLIF_WRITE() - Write updated (relocated) segment contents to target */
241 /* memory. */
242 /*****************************************************************************/
243 BOOL DLIF_write(struct DLOAD_MEMORY_REQUEST* req)
244 {
245 /*------------------------------------------------------------------------*/
246 /* Nothing to do since we are relocating directly into target memory. */
247 /*------------------------------------------------------------------------*/
248 return 1;
249 }
251 /*****************************************************************************/
252 /* DLIF_EXECUTE() - Transfer control to specified target address. */
253 /*****************************************************************************/
254 int32_t DLIF_execute(TARGET_ADDRESS exec_addr)
255 {
256 /*------------------------------------------------------------------------*/
257 /* This call will only work if the host and target are the same instance. */
258 /* The compiler may warn about this conversion from an object to a */
259 /* function pointer. */
260 /*------------------------------------------------------------------------*/
261 return ((int32_t(*)())(exec_addr))();
262 }
265 #if 0
266 /*****************************************************************************/
267 /* Client Provided Communication Mechanisms to assist with creation of */
268 /* DLLView debug information. Client needs to know exactly when a segment */
269 /* is being loaded or unloaded so that it can keep its debug information */
270 /* up to date. */
271 /*****************************************************************************/
272 /*****************************************************************************/
273 /* DLIF_LOAD_DEPENDENT() - Perform whatever maintenance is needed in the */
274 /* client when loading of a dependent file is initiated by the core */
275 /* loader. Open the dependent file on behalf of the core loader, */
276 /* then invoke the core loader to get it into target memory. The core */
277 /* loader assumes ownership of the dependent file pointer and must ask */
278 /* the client to close the file when it is no longer needed. */
279 /* */
280 /* If debug support is needed under the Braveheart model, then create */
281 /* a host version of the debug module record for this object. This */
282 /* version will get updated each time we allocate target memory for a */
283 /* segment that belongs to this module. When the load returns, the */
284 /* client will allocate memory for the debug module from target memory */
285 /* and write the host version of the debug module into target memory */
286 /* at the appropriate location. After this takes place the new debug */
287 /* module needs to be added to the debug module list. The client will */
288 /* need to update the tail of the DLModules list to link the new debug */
289 /* module onto the end of the list. */
290 /* */
291 /*****************************************************************************/
292 int DLIF_load_dependent(const char* so_name)
293 {
294 /*------------------------------------------------------------------------*/
295 /* Find the path and file name associated with the given so_name in the */
296 /* client's file registry. */
297 /*------------------------------------------------------------------------*/
298 /* If we can't find the so_name in the file registry (or if the registry */
299 /* is not implemented yet), we'll open the file using the so_name. */
300 /*------------------------------------------------------------------------*/
301 int to_ret = 0;
302 FILE* fp = fopen(so_name, "rb");
304 /*------------------------------------------------------------------------*/
305 /* We need to make sure that the file was properly opened. */
306 /*------------------------------------------------------------------------*/
307 if (!fp)
308 {
309 DLIF_error(DLET_FILE, "Can't open dependent file '%s'.\n", so_name);
310 return 0;
311 }
313 /*------------------------------------------------------------------------*/
314 /* If the dynamic loader is providing debug support for a DLL View plug- */
315 /* in or script of some sort, then we are going to create a host version */
316 /* of the debug module record for the so_name module. */
317 /*------------------------------------------------------------------------*/
318 /* In the Braveheart view of the world, debug support is only to be */
319 /* provided if the DLModules symbol is defined in the base image. */
320 /* We will set up a DLL_debug flag when the command to load the base */
321 /* image is issued. */
322 /*------------------------------------------------------------------------*/
323 if (DLL_debug)
324 DLDBG_add_host_record(so_name);
326 /*------------------------------------------------------------------------*/
327 /* Tell the core loader to proceed with loading the module. */
328 /*------------------------------------------------------------------------*/
329 /* Note that the client is turning ownership of the file pointer over to */
330 /* the core loader at this point. The core loader will need to ask the */
331 /* client to close the dependent file when it is done using the dependent */
332 /* file pointer. */
333 /*------------------------------------------------------------------------*/
334 to_ret = DLOAD_load(fp, 0, NULL);
336 /*------------------------------------------------------------------------*/
337 /* If the dependent load was successful, update the DLModules list in */
338 /* target memory as needed. */
339 /*------------------------------------------------------------------------*/
340 if (to_ret != 0)
341 {
342 /*---------------------------------------------------------------------*/
343 /* We will need to copy the information from our host version of the */
344 /* debug record into actual target memory. */
345 /*---------------------------------------------------------------------*/
346 if (DLL_debug)
347 {
348 /*---------------------------------------------------------------*/
349 /* Allocate target memory for the module's debug record. Use */
350 /* host version of the debug information to determine how much */
351 /* target memory we need and how it is to be filled in. */
352 /*---------------------------------------------------------------*/
353 /* Note that we don't go through the normal API functions to get */
354 /* target memory and write the debug information since we're not */
355 /* dealing with object file content here. The DLL View debug is */
356 /* supported entirely on the client side. */
357 /*---------------------------------------------------------------*/
358 DLDBG_add_target_record(to_ret);
359 }
360 }
362 /*------------------------------------------------------------------------*/
363 /* Report failure to load dependent. */
364 /*------------------------------------------------------------------------*/
365 else
366 DLIF_error(DLET_MISC, "Failed load of dependent file '%s'.\n", so_name);
368 return to_ret;
369 }
371 #endif
373 #if 0
374 /*****************************************************************************/
375 /* DLIF_UNLOAD_DEPENDENT() - Perform whatever maintenance is needed in the */
376 /* client when unloading of a dependent file is initiated by the core */
377 /* loader. Invoke the DLOAD_unload() function to get the core loader */
378 /* to release any target memory that is associated with the dependent */
379 /* file's segments. */
380 /*****************************************************************************/
381 void DLIF_unload_dependent(uint32_t handle)
382 {
383 /*------------------------------------------------------------------------*/
384 /* If the specified module is no longer needed, DLOAD_load() will spin */
385 /* through the object descriptors associated with the module and free up */
386 /* target memory that was allocated to any segment in the module. */
387 /*------------------------------------------------------------------------*/
388 /* If DLL debugging is enabled, find module debug record associated with */
389 /* this module and remove it from the DLL debug list. */
390 /*------------------------------------------------------------------------*/
391 if (DLOAD_unload(handle))
392 {
393 DSBT_release_entry(handle);
394 if (DLL_debug) DLDBG_rm_target_record(handle);
395 }
396 }
398 #endif
400 /*****************************************************************************/
401 /* Client Provided API Functions to Support Logging Warnings/Errors */
402 /*****************************************************************************/
404 /*****************************************************************************/
405 /* DLIF_WARNING() - Write out a warning message from the core loader. */
406 /*****************************************************************************/
407 void DLIF_warning(LOADER_WARNING_TYPE wtype, const char *fmt, ...)
408 {
409 va_list ap;
410 va_start(ap,fmt);
411 printf("<< D L O A D >> WARNING: ");
412 vprintf(fmt,ap);
413 va_end(ap);
414 }
416 /*****************************************************************************/
417 /* DLIF_ERROR() - Write out an error message from the core loader. */
418 /*****************************************************************************/
419 void DLIF_error(LOADER_ERROR_TYPE etype, const char *fmt, ...)
420 {
421 va_list ap;
422 va_start(ap,fmt);
423 printf("<< D L O A D >> ERROR: ");
424 vprintf(fmt,ap);
425 va_end(ap);
426 }
428 /*****************************************************************************
429 * END API FUNCTION DEFINITIONS
430 *****************************************************************************/