libspe2  0.9a
Data Structures | Functions
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
handlehandle to open file
Return values
0On success, spe_close_image returns 0.
-1On 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.

97 {
98  int ret = 0;
99  struct image_handle *ih;
100 
101  if (!handle) {
102  errno = EINVAL;
103  return -1;
104  }
105 
106  ih = (struct image_handle *)handle;
107 
108  if (!ih->speh.elf_image || !ih->map_size) {
109  errno = EINVAL;
110  return -1;
111  }
112 
113  if (ih->speh.toe_shadow)
114  free(ih->speh.toe_shadow);
115 
116  ret = munmap(ih->speh.elf_image, ih->map_size );
117  free(handle);
118 
119  return ret;
120 }
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
filenameSpecifies 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.

38 {
39  /* allocate an extra integer in the spe handle to keep the mapped size information */
40  struct image_handle *ret;
41  int binfd = -1, f_stat;
42  struct stat statbuf;
43  size_t ps = getpagesize ();
44 
45  ret = malloc(sizeof(struct image_handle));
46  if (!ret)
47  return NULL;
48 
49  ret->speh.handle_size = sizeof(spe_program_handle_t);
50  ret->speh.toe_shadow = NULL;
51 
52  binfd = open(filename, O_RDONLY);
53  if (binfd < 0)
54  goto ret_err;
55 
56  f_stat = fstat(binfd, &statbuf);
57  if (f_stat < 0)
58  goto ret_err;
59 
60  /* Sanity: is it executable ?
61  */
62  if(!(statbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
63  errno=EACCES;
64  goto ret_err;
65  }
66 
67  /* now store the size at the extra allocated space */
68  ret->map_size = (statbuf.st_size + ps - 1) & ~(ps - 1);
69 
70  ret->speh.elf_image = mmap(NULL, ret->map_size,
71  PROT_WRITE | PROT_READ,
72  MAP_PRIVATE, binfd, 0);
73  if (ret->speh.elf_image == MAP_FAILED)
74  goto ret_err;
75 
76  /*Verify that this is a valid SPE ELF object*/
78  goto ret_err;
79 
80  if (_base_spe_toe_ear(&ret->speh))
81  goto ret_err;
82 
83  /* ok */
84  close(binfd);
85  return (spe_program_handle_t *)ret;
86 
87  /* err & cleanup */
88 ret_err:
89  if (binfd >= 0)
90  close(binfd);
91 
92  free(ret);
93  return NULL;
94 }

Here is the call graph for this function: