示例#1
0
static pool_sSegment *
mapSegment (
  pwr_tStatus		*sts,
  pool_sHead		*php,
  pool_sSegment		*psp
)
{
  pwr_tStatus		lsts;
  sect_sHead		*shp;
  pwr_tBoolean		created;
  char			name[16];
  pool_sGhead		*gphp = php->gphp;

  /* Map the section */

  segName(name, gphp->name, psp->gpsp->generation);

  shp = sect_Alloc(&lsts, &created, &psp->sect, psp->gpsp->size << pool_cOffsGranul, name);
  if (shp == NULL) errh_ReturnOrBugcheck(NULL, sts, lsts, "");

  if (created) errh_Bugcheck(POOL__NOTCREATED, "pool was not created"); 

  psp->generation = psp->gpsp->generation;
  psp->base = psp->sect.base;

  return psp;
}
示例#2
0
static pool_sSegment *
newSegment (
  pwr_tStatus		*sts,
  pool_sHead		*php,
  size_t		size		/* Requested size in pool_sEntry units */
)
{
  pool_sGhead		*gphp;
  pool_sGsegment	*gpsp;
  pool_sSegment		*psp;
  sect_sHead		*shp;
  pwr_tBoolean		created;
  pwr_tUInt32		i;
  char			name[16];

  if (size << pool_cOffsAlign > pool_cMaxSize)
    pwr_Return(NULL, sts, POOL__TOOBIG);
    
  gphp = php->gphp;

  /* Find an empty slot */

  for (i = 0; i < pool_cSegs; i++)
    if (gphp->seg[i].generation == 0)
      break;

  if (i >= pool_cSegs)
    pwr_Return(NULL, sts, POOL__NOSLOT);

  gpsp = &gphp->seg[i];
  psp = &php->seg[i];

  /* Allocate the section */

  segName(name, gphp->name, gphp->generation),

  shp = sect_Alloc(sts, &created, &psp->sect, size << pool_cOffsAlign, name);
  if (shp == NULL) return NULL;
  if (!created) errh_Bugcheck(POOL__ALLREXIST, ""); 

  gpsp->type = pool_eSegType_dynamic;
  gpsp->size = size << (pool_cOffsAlign - pool_cOffsGranul);
  gpsp->fragmax = gpsp->fragsize = size;
  gpsp->generation = psp->generation = gphp->generation++;
  gpsp->alloccnt = 0;
  gpsp->fragcnt = gpsp->fragmaxcnt = 1;
  psp->base = psp->sect.base;
  psp->base->size = size;
  psp->base->next = pool_cNOffset;
  gpsp->freeroot.next = 0;

  return psp;
}
bool ImageLoader::overlapsWithAddressRange(const void* start, const void* end) const
{
	for(unsigned int i=0, e=segmentCount(); i < e; ++i) {
		const uint8_t* segStart = (const uint8_t*)segActualLoadAddress(i);
		const uint8_t* segEnd = (const uint8_t*)segActualEndAddress(i);
		if ( strcmp(segName(i), "__UNIXSTACK") == 0 ) {
			// __UNIXSTACK never slides.  This is the only place that cares
			// and checking for that segment name in segActualLoadAddress()
			// is too expensive.
			segStart -= getSlide();
			segEnd -= getSlide();
		}
		if ( (start <= segStart) && (segStart < end) )
			return true;
		if ( (start <= segEnd) && (segEnd < end) )
			return true;
		if ( (segStart < start) && (end < segEnd) )
			return true;
	}
	return false;
}
示例#4
0
sect_sHead *
sect_Alloc (
  pwr_tStatus		*sts,
  pwr_tBoolean		*created,
  sect_sHead		*shp,
  size_t		size,
  char			*name,
  unsigned int		flags
)
{
  pwr_tStatus		lsts = 1;
  sect_sHead		*lshp = NULL;

  if (shp == NULL) shp = lshp = (sect_sHead *) calloc(1, sizeof(*lshp));
  if (shp == NULL) pwr_Return(NULL, sts, 2/*SECT__VIRMEM*/);
  
  do {
    if (shp->flags.b.mapped) {
      lsts = 2/*SECT__WASMAPPED*/;
      break;
    }

    shp->base = NULL;
    segName(shp, name);

#if defined(OS_ELN) || defined (OS_VMS)
    shp->namedsc.dsc$w_length	= strlen(name);
    shp->namedsc.dsc$b_dtype	= DSC$K_DTYPE_T;
    shp->namedsc.dsc$b_class	= DSC$K_CLASS_S;
    shp->namedsc.dsc$a_pointer	= shp->name;
#endif

#if defined(OS_ELN)
    ker$create_area(&lsts, &shp->area, &shp->base, size, &shp->namedsc, NULL);
    if (EVEN(lsts)) break;

    shp->flags.b.mapped = 1;
    *created = (lsts != KER$_AREA_EXISTS);
    if (*created) memset(shp->base, 0, size);

#elif defined(OS_VMS)
    {
      pwr_tUInt32	inadr[2];
      pwr_tUInt32	retadr[2];

      inadr[0] = 10000;
      inadr[1] = inadr[0] + size;

      lsts = sys$crmpsc(inadr, retadr, 0, 
			SEC$M_WRT | SEC$M_EXPREG | SEC$M_GBL | SEC$M_PAGFIL,
			&shp->namedsc, NULL, 0, 0,
			(((size + 511) / 512) + 1), 0, 0, 0);

      if (EVEN(lsts)) {
	errh_Error("sect_Alloc, new section: %s, size = %d\n%m", name, size, lsts);
	break;
      }

      shp->base		    = (void *)retadr[0];
      shp->sectadr[0]	    = retadr[0];
      shp->sectadr[1]	    = retadr[1];
      shp->size		    = retadr[1] - retadr[0] + 1;
      shp->flags.b.mapped   = 1;

      pwr_Assert(size <= shp->size);
      *created = (lsts == SS$_CREATED);
      if (*created)
	errh_Info("sect_Alloc, new section: %s, size = %d", name, size);
    }

#elif defined(OS_LYNX)
    {
      int    shm_fd;                                              
      int    shMemFlags = O_RDWR;              
      mode_t shMemMode  = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; 

      *created = 0;
      /* This is the only way I know to find out if the memory is created */
      shm_fd = shm_open(shp->name, shMemFlags, shMemMode); 

      if (sect_must_create && shm_fd != -1) { /* Do we need to check errno ? */
       // printf("sect_Alloc: %s already exists. It will be unlinked and created\n", shp->name); 
        if (shm_unlink(shp->name) == -1) {
          lsts = 2;             
	  perror("sect_Alloc: shm_unlink");                
          break;
	}

        shMemFlags |= O_CREAT | O_EXCL; 
        shm_fd = shm_open(shp->name, shMemFlags, shMemMode); 
        if(shm_fd == -1) {
          lsts = 2;             
	  perror("sect_Alloc: shm_open, O_CREATE");                
          break;
	}
	*created = 1;
      }
      else if(shm_fd == -1) {
	if (errno == ENOENT) { /* It didn't exist */
          shMemFlags |= O_CREAT | O_EXCL; 
        shm_fd = shm_open(shp->name, shMemFlags, shMemMode); 

          if(shm_fd == -1) {
            lsts = 2;             
	    perror("sect_Alloc: shm_open, O_CREATE ");                
            break;
	  } else
	    *created = 1;
	} else {
          lsts = 2;             
	  perror("sect_Alloc: shm_open ");                
          break;
	}
      } 
      
      /* Set size */
      if (ftruncate(shm_fd, size) != 0) {
	close(shm_fd);
	if (*created)
	  shm_unlink(shp->name);
        lsts = 2;
	perror("sect_Alloc: ftruncate ");                
	break;
      }

      shp->base = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
      if (shp->base == MAP_FAILED) {
	close(shm_fd);
	if (*created)
	  shm_unlink(shp->name);
        lsts = 2;
	perror("sect_Alloc: mmap ");                
        break;
      }
     
      close(shm_fd);

      shp->size		    = size;
      shp->flags.b.mapped   = 1;
      lsts = 1; 
    }
#elif defined OS_POSIX
    {
      int    shm_fd;                                              
      int    shMemFlags = O_RDWR;              
      mode_t shMemMode  = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; 
      key_t  shm_key;
      int    shm_id;

      *created = 0;
      /* Create unique key */
      shm_fd = open(shp->name, shMemFlags, shMemMode); 

      if (sect_must_create && shm_fd != -1) { /* Do we need to check errno ? */
        errh_Info("sect_Alloc: %s already exists. It will be unlinked and created\n", shp->name); 
        if (unlink(shp->name) == -1) {
          lsts = 2;             
          errh_Error("sect_Alloc: unlink failed on !%s! errno: %d", shp->name, errno);
          break;
        }
        shMemFlags |= O_CREAT | O_EXCL; 
        shm_fd = open(shp->name, shMemFlags, shMemMode); 
        if(shm_fd == -1) {
          lsts = 2;             
          errh_Error("sect_Alloc: open O_CREATE failed errno: %d", errno);
          break;
        }
        *created = 1;
      } else if(shm_fd == -1) {
        if (errno == ENOENT) { /* It didn't exist */
	  if ( !(flags & sect_mFlags_Create)) {
	    lsts = 2;
	    errh_Error("sect_Alloc: Couldn't attach shm section size");
	    break;
	  }
          shMemFlags |= O_CREAT | O_EXCL; 
          shm_fd = open(shp->name, shMemFlags, shMemMode); 
          if(shm_fd == -1) {
            lsts = 2;             
            errh_Error("sect_Alloc: open O_CREATE | O_EXCL failed errno: %d",
                      errno);
            break;
          } else {
            *created = 1;
          }
        } else {
          lsts = 2;             
          errh_Error("sect_Alloc: open failed errno: %d", errno);
          break;
        }
      } 

      /* Get shm key */
      shm_key = ftok(shp->name, 'P');
      close(shm_fd);

      /* Create shm section */
      shm_id = shmget(shm_key, size, IPC_CREAT | 0660);
      if (shm_id == -1) {
        lsts = 2;
        errh_Error("sect_Alloc: Couldn't attach/create shm section size: %d. Check shmmax.", size);
        break;
      }

      shp->base = shmat(shm_id, (void *)NULL, 0);
      if (shp->base == (void *)-1) {
        if (*created)
          unlink(shp->name);
        lsts = 2;
        errh_Error("sect_Alloc: shmat failed");
        break;
      }
     
      shp->size		    = size;
      shp->flags.b.mapped   = 1;
      lsts = 1; 
    }
#endif

    if (*created) errh_Info("Mapped %s, base: %x, size: %d", shp->name, shp->base, shp->size);

    pwr_Return(shp, sts, lsts);
  } while (0);

  /* Something went wrong, clean up!  */

  if (lshp != NULL) free(lshp);
  pwr_Return(NULL, sts, lsts);
}