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.

{
int ret = 0;
struct image_handle *ih;
if (!handle) {
errno = EINVAL;
return -1;
}
ih = (struct image_handle *)handle;
if (!ih->speh.elf_image || !ih->map_size) {
errno = EINVAL;
return -1;
}
if (ih->speh.toe_shadow)
free(ih->speh.toe_shadow);
ret = munmap(ih->speh.elf_image, ih->map_size );
free(handle);
return ret;
}

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.

{
/* allocate an extra integer in the spe handle to keep the mapped size information */
struct image_handle *ret;
int binfd = -1, f_stat;
struct stat statbuf;
size_t ps = getpagesize ();
ret = malloc(sizeof(struct image_handle));
if (!ret)
return NULL;
ret->speh.handle_size = sizeof(spe_program_handle_t);
ret->speh.toe_shadow = NULL;
binfd = open(filename, O_RDONLY);
if (binfd < 0)
goto ret_err;
f_stat = fstat(binfd, &statbuf);
if (f_stat < 0)
goto ret_err;
/* Sanity: is it executable ?
*/
if(!(statbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
errno=EACCES;
goto ret_err;
}
/* now store the size at the extra allocated space */
ret->map_size = (statbuf.st_size + ps - 1) & ~(ps - 1);
ret->speh.elf_image = mmap(NULL, ret->map_size,
PROT_WRITE | PROT_READ,
MAP_PRIVATE, binfd, 0);
if (ret->speh.elf_image == MAP_FAILED)
goto ret_err;
/*Verify that this is a valid SPE ELF object*/
if((_base_spe_verify_spe_elf_image((spe_program_handle_t *)ret)))
goto ret_err;
if (_base_spe_toe_ear(&ret->speh))
goto ret_err;
/* ok */
close(binfd);
return (spe_program_handle_t *)ret;
/* err & cleanup */
ret_err:
if (binfd >= 0)
close(binfd);
free(ret);
return NULL;
}

Here is the call graph for this function: