image.c File Reference

#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "elf_loader.h"
#include "spebase.h"
Include dependency graph for image.c:

Go to the source code of this file.

Data Structures

struct  image_handle

Functions

spe_program_handle_t_base_spe_image_open (const char *filename)
int _base_spe_image_close (spe_program_handle_t *handle)

Function Documentation

int _base_spe_image_close ( spe_program_handle_t handle  ) 

_base_spe_image_close unmaps an SPE ELF object that was previously mapped using spe_open_image.

Parameters:
handle handle to open file
Return values:
0 On success, spe_close_image returns 0.
-1 On failure, -1 is returned and errno is set appropriately.
Possible values for errno:
EINVAL From spe_close_image, this indicates that the file, specified by filename, was not previously mapped by a call to spe_open_image.

Definition at line 96 of file image.c.

References spe_program_handle::elf_image, image_handle::map_size, image_handle::speh, and spe_program_handle::toe_shadow.

00097 {
00098         int ret = 0;
00099         struct image_handle *ih;
00100 
00101         if (!handle) {
00102                 errno = EINVAL;
00103                 return -1;
00104         }
00105         
00106         ih = (struct image_handle *)handle;
00107 
00108         if (!ih->speh.elf_image || !ih->map_size) {
00109                 errno = EINVAL;
00110                 return -1;
00111         }
00112 
00113         if (ih->speh.toe_shadow)
00114                 free(ih->speh.toe_shadow);
00115 
00116         ret = munmap(ih->speh.elf_image, ih->map_size );
00117         free(handle);
00118 
00119         return ret;
00120 }

spe_program_handle_t* _base_spe_image_open ( const char *  filename  ) 

_base_spe_image_open maps an SPE ELF executable indicated by filename into system memory and returns the mapped address appropriate for use by the spe_create_thread API. It is often more convenient/appropriate to use the loading methodologies where SPE ELF objects are converted to PPE static or shared libraries with symbols which point to the SPE ELF objects after these special libraries are loaded. These libraries are then linked with the associated PPE code to provide a direct symbol reference to the SPE ELF object. The symbols in this scheme are equivalent to the address returned from the spe_open_image function. SPE ELF objects loaded using this function are not shared with other processes, but SPE ELF objects loaded using the other scheme, mentioned above, can be shared if so desired.

Parameters:
filename Specifies the filename of an SPE ELF executable to be loaded and mapped into system memory.
Returns:
On success, spe_open_image returns the address at which the specified SPE ELF object has been mapped. On failure, NULL is returned and errno is set appropriately.
Possible values for errno include:
EACCES The calling process does not have permission to access the specified file.
EFAULT The filename parameter points to an address that was not contained in the calling process`s address space.

A number of other errno values could be returned by the open(2), fstat(2), mmap(2), munmap(2), or close(2) system calls which may be utilized by the spe_open_image or spe_close_image functions.

See also:
spe_create_thread

Definition at line 37 of file image.c.

References _base_spe_toe_ear(), _base_spe_verify_spe_elf_image(), spe_program_handle::elf_image, spe_program_handle::handle_size, image_handle::map_size, image_handle::speh, and spe_program_handle::toe_shadow.

00038 {
00039         /* allocate an extra integer in the spe handle to keep the mapped size information */
00040         struct image_handle *ret;
00041         int binfd = -1, f_stat;
00042         struct stat statbuf;
00043         size_t ps = getpagesize ();
00044 
00045         ret = malloc(sizeof(struct image_handle));
00046         if (!ret)
00047                 return NULL;
00048 
00049         ret->speh.handle_size = sizeof(spe_program_handle_t);
00050         ret->speh.toe_shadow = NULL;
00051 
00052         binfd = open(filename, O_RDONLY);
00053         if (binfd < 0)
00054                 goto ret_err;
00055 
00056         f_stat = fstat(binfd, &statbuf);
00057         if (f_stat < 0)
00058                 goto ret_err;
00059 
00060         /* Sanity: is it executable ?
00061          */
00062         if(!(statbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
00063                 errno=EACCES;
00064                 goto ret_err;
00065         }
00066         
00067         /* now store the size at the extra allocated space */
00068         ret->map_size = (statbuf.st_size + ps - 1) & ~(ps - 1);
00069 
00070         ret->speh.elf_image = mmap(NULL, ret->map_size,
00071                                                         PROT_WRITE | PROT_READ,
00072                                                         MAP_PRIVATE, binfd, 0);
00073         if (ret->speh.elf_image == MAP_FAILED)
00074                 goto ret_err;
00075 
00076         /*Verify that this is a valid SPE ELF object*/
00077         if((_base_spe_verify_spe_elf_image((spe_program_handle_t *)ret)))
00078                 goto ret_err;
00079 
00080         if (_base_spe_toe_ear(&ret->speh))
00081                 goto ret_err;
00082 
00083         /* ok */
00084         close(binfd);
00085         return (spe_program_handle_t *)ret;
00086 
00087         /* err & cleanup */
00088 ret_err:
00089         if (binfd >= 0)
00090                 close(binfd);
00091 
00092         free(ret);
00093         return NULL;
00094 }

Here is the call graph for this function:


Generated by  doxygen 1.6.2