Beispiel #1
0
Elf_Half elfio::load_sections( std::istream &stream ) {
	Elf_Half  entry_size = header->get_section_entry_size();
	Elf_Half  num		= header->get_sections_num();
	Elf64_Off offset	 = header->get_sections_offset();
	for ( Elf_Half i = 0; i < num; ++i ) {
		section* sec = create_section();
		sec->load( stream, (std::streamoff)offset + i * entry_size );
		sec->set_index( i );
		// To mark that the section is not permitted to reassign address
		// during layout calculation
		sec->set_address( sec->get_address() );
	}
	Elf_Half shstrndx = get_section_name_str_index();
	if ( SHN_UNDEF != shstrndx ) {
		string_section_accessor str_reader( sections[shstrndx] );
		for ( Elf_Half i = 0; i < num; ++i ) {
			Elf_Word offset = sections[i]->get_name_string_offset();
			const char* p = str_reader.get_string( offset );
			if ( p != 0 ) {
				sections[i]->set_name( p );
			}
		}
	}
	return num;
}
Beispiel #2
0
NTSTATUS create_section( object_t **obj, object_t *file, PLARGE_INTEGER psz, ULONG attribs, ULONG protect )
{
	section_t *sec;
	NTSTATUS r = create_section( &sec, file, psz, attribs, protect );
	if (r == STATUS_SUCCESS)
		*obj = sec;
	return r;
}
Beispiel #3
0
NTSTATUS section_factory::alloc_object(object_t** obj)
{
	NTSTATUS r = create_section( obj, file, SectionSize, Attributes, Protect );
	if (r < STATUS_SUCCESS)
		return r;
	if (!*obj)
		return STATUS_NO_MEMORY;
	return STATUS_SUCCESS;
}
Beispiel #4
0
section& section::operator=(const section &sec)
{
	title = sec.title;
	id = sec.id;
	level = sec.level;
	std::copy(sec.topics.begin(), sec.topics.end(), std::back_inserter(topics));
	std::transform(sec.sections.begin(), sec.sections.end(),
				   std::back_inserter(sections), create_section());
	return *this;
}
Beispiel #5
0
section::section(const section &sec) :
	title(sec.title),
	id(sec.id),
	topics(sec.topics),
	sections(),
	level(sec.level)
{
	std::transform(sec.sections.begin(), sec.sections.end(),
				   std::back_inserter(sections), create_section());
}
Beispiel #6
0
void elfio::create_mandatory_sections() {
	// Create null section without calling to 'add_section' as no string
	// section containing section names exists yet
	section *sec0 = create_section();
	sec0->set_index( 0 );
	sec0->set_name( "" );
	sec0->set_name_string_offset( 0 );
	set_section_name_str_index( 1 );
	section* shstrtab = sections.add( ".shstrtab" );
	shstrtab->set_type( SHT_STRTAB );
}
Beispiel #7
0
static pl_ini_pair* locate_or_create(pl_ini_file *file,
                                     const char *section_name,
                                     const char *key_name)
{
  pl_ini_section *section = find_section(file, section_name);
  pl_ini_pair *pair = NULL;

  if (section)
    pair = find_pair(section, key_name);
  else
  {
    /* Create section */
    section = create_section(section_name);

    if (!file->head)
      file->head = section;
    else
    {
      pl_ini_section *s;
      for (s = file->head; s->next; s = s->next); /* Find the tail */
      s->next = section;
    }
  }

  if (!pair)
  {
    /* Create pair */
    pair = (pl_ini_pair*)malloc(sizeof(pl_ini_pair));
    pair->key = strdup(key_name);
    pair->value = NULL;
    pair->next = NULL;

    if (!section->head)
      section->head = pair;
    else
    {
      pl_ini_pair *p;
      for (p = section->head; p->next; p = p->next); /* Find the tail */
      p->next = pair;
    }
  }

  return pair;
}
Beispiel #8
0
int pl_ini_load(pl_ini_file *file,
                const char *path)
{ 
  file->head = NULL;

  FILE *stream;
  if (!(stream = fopen(path, "r"))) 
    return 0;

  pl_ini_section *current_section = NULL;
  pl_ini_pair *tail;

  char string[PL_MAX_LINE_LENGTH],
       name[PL_MAX_LINE_LENGTH];
  char *ptr;
  int len;

  /* Create unnamed section */
  current_section = NULL;
  tail = NULL;

  while(!feof(stream) && fgets(string, sizeof(string), stream))
  {
    /* TODO: Skip whitespace */
    /* New section */
    if (string[0] == '[')
    {
      if ((ptr = strrchr(string, ']')))
      {
        len = ptr - string - 1;
        strncpy(name, string + 1, len);
        name[len] = '\0';

        if (!current_section)
          current_section = file->head = create_section(name);
        else
        {
          current_section->next = create_section(name);
          current_section = current_section->next;
        }

        tail = NULL;
      }
    }
    else if (string[0] =='#'); /* Do nothing - comment */
    else
    {
      /* No section defined - create empty section */
      if (!current_section)
      {
        current_section = create_section(strdup(""));
        file->head = current_section;
        tail = NULL;
      }

      pl_ini_pair *pair = create_pair(string);
      if (pair)
      {
        if (tail) tail->next = pair;
        else current_section->head = pair;
        tail = pair;
      }
    }
  }

  fclose(stream);
  return 1;
}
Beispiel #9
0
cfg_st *cfg_load(char *path)
{
    FILE *fp = fopen(path, "r");
    if (NULL == fp)
    {
      return NULL;
    }

    struct cfg_section *sec_head = NULL;
    struct cfg_section *sec_prev = NULL;
    struct cfg_section *sec_curr = NULL;

    struct cfg_obj *obj_curr = NULL;
    struct cfg_obj *obj_prev = NULL;

    char *line = NULL;
    size_t   n  = 0;
    ssize_t len = 0;

    while ((len = _readline(&line, &n, fp)) != -1)
    {
        char *s = line;
        #ifdef __DEBUG__
          printf("1_readline:%s\n",s);
        #endif
        if (is_comment(&s))
            continue;
        len = strlen(s);

        if (len>2 && s[0]=='[' && s[len-1]==']')
        {
            char *name = s + 1;
            *(name+len-1-1) = '\0';
            trim(name);

            if ((sec_curr = find_section(sec_head, name)) == NULL)
            {
                if ((sec_curr = create_section(sec_head, name)) == NULL)
                {
                    free(line);
                    return NULL;
                }

                if (sec_head == NULL)
                    sec_head = sec_curr;
                if (sec_prev != NULL)
                    sec_prev->next = sec_curr;

                sec_prev = sec_curr;
                obj_prev = NULL;
            }
            else
            {
                obj_prev = sec_curr->obj;
                while (obj_prev->next != NULL)
                    obj_prev = obj_prev->next;
            }
            continue;
        }

        char *delimiter = strchr(s, '=');
        if (delimiter == NULL)
            continue;
        *delimiter = '\0';

        char *name = s;
        trim(name);

        char *value = delimiter + 1;
        delimiter = strchr(value, '#');
        if (NULL != delimiter)
        {
          *delimiter = '\0';
        }
        trim(value);

        if (sec_curr == NULL)
        {
            if ((sec_curr = create_section(sec_head, "global")) == NULL)
            {
                free(line);

                return NULL;
            }

            if (sec_head == NULL)
                sec_head = sec_curr;
            sec_prev = sec_curr;
            obj_prev = NULL;
        }

        if ((obj_curr = find_obj(sec_curr, name)) == NULL)
        {
            obj_curr = create_obj(sec_head, name, value);
            if (obj_curr == NULL)
            {
                free(line);

                return NULL;
            }

            if (obj_prev)
                obj_prev->next = obj_curr;
            if (sec_curr->obj == NULL)
                sec_curr->obj = obj_curr;

            obj_prev = obj_curr;
        }
        else
        {
            char *old_value = obj_curr->value;

            if ((obj_curr->value = strdup(value)) == NULL)
            {
                cfg_free(sec_head);

                free(line);

                return NULL;
            }

            free(old_value);
        }
    }

    free(line);
    fclose(fp);

    if (sec_head == NULL)
    {
        if ((sec_head = calloc(1, sizeof(struct cfg_section))) == NULL)
            return NULL;
    }
    #ifdef __DEBUG__
      ini_print(sec_head);
    #endif
    
    return sec_head;
}
Beispiel #10
0
NTSTATUS thread_impl_t::create( CONTEXT *ctx, INITIAL_TEB *init_teb, BOOLEAN suspended )
{
	void *pLdrInitializeThunk;
	void *pKiUserApcDispatcher;
	PTEB pteb = NULL;
	BYTE *addr = 0;
	void *stack;
	NTSTATUS r;
	struct {
		void *pLdrInitializeThunk;
		void *unk1;
		void *pntdll;  /* set to pexe if running a win32 program */
		void *unk2;
		CONTEXT ctx;
		void *ret;	 /* return address (to KiUserApcDispatcher?) */
	} init_stack;

	/* allocate the TEB */
	LARGE_INTEGER sz;
	sz.QuadPart = PAGE_SIZE;
	r = create_section( &teb_section, NULL, &sz, SEC_COMMIT, PAGE_READWRITE );
	if (r < STATUS_SUCCESS)
		return r;

	r = teb_section->mapit( process->vm, addr, 0, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
	if (r < STATUS_SUCCESS)
		return r;

	teb = (PTEB) teb_section->get_kernel_address();

	pteb = (PTEB) addr;
	teb->Peb = (PPEB) process->PebBaseAddress;
	teb->Tib.Self = &pteb->Tib;
	teb->StaticUnicodeString.Buffer = pteb->StaticUnicodeBuffer;
	teb->StaticUnicodeString.MaximumLength = sizeof pteb->StaticUnicodeBuffer;
	teb->StaticUnicodeString.Length = sizeof pteb->StaticUnicodeBuffer;

	// FIXME: need a good thread test for these
	teb->DeallocationStack = init_teb->StackReserved;
	teb->Tib.StackBase = init_teb->StackCommit;
	teb->Tib.StackLimit = init_teb->StackReserved;

	get_client_id( &teb->ClientId );

	/* setup fs in the user address space */
	TebBaseAddress = pteb;

	/* find entry points */
	pLdrInitializeThunk = (BYTE*)process->pntdll + get_proc_address( ntdll_section, "LdrInitializeThunk" );
	if (!pLdrInitializeThunk)
		die("failed to find LdrInitializeThunk in ntdll\n");

	pKiUserApcDispatcher = (BYTE*)process->pntdll + get_proc_address( ntdll_section, "KiUserApcDispatcher" );
	if (!pKiUserApcDispatcher)
		die("failed to find KiUserApcDispatcher in ntdll\n");

	dprintf("LdrInitializeThunk = %p pKiUserApcDispatcher = %p\n",
		pLdrInitializeThunk, pKiUserApcDispatcher );

	// FIXME: should set initial registers then queue an APC

	/* set up the stack */
	stack = (BYTE*) ctx->Esp - sizeof init_stack;

	/* setup the registers */
	int err = set_initial_regs( pKiUserApcDispatcher, stack );
	if (0>err)
		dprintf("set_initial_regs failed (%d)\n", err);

	memset( &init_stack, 0, sizeof init_stack );
	init_stack.pntdll = process->pntdll;  /* set to pexe if running a win32 program */
	init_stack.pLdrInitializeThunk = pLdrInitializeThunk;

	/* copy the context onto the stack for NtContinue */
	memcpy( &init_stack.ctx, ctx, sizeof *ctx );
	init_stack.ret  = (void*) 0xf00baa;

	r = process->vm->copy_to_user( stack, &init_stack, sizeof init_stack );
	if (r < STATUS_SUCCESS)
		dprintf("failed to copy initial stack data\n");

	if (!suspended)
		resume( NULL );

	process->vm->set_tracer( addr, teb_trace );

	return STATUS_SUCCESS;
}
Beispiel #11
0
ini_t *ini_load(const char *path)
{
    FILE *fp = fopen(path, "r");
    if (fp == NULL)
        return NULL;

    struct ini_section *head = NULL;
    struct ini_section *prev = NULL;
    struct ini_section *curr = NULL;

    struct ini_arg *arg_curr = NULL;
    struct ini_arg *arg_prev = NULL;

    char *line  = NULL;
    size_t   n  = 0;
    ssize_t len = 0;

    while ((len = _getline(&line, &n, fp)) != -1)
    {
        char *s = line;
        if (is_comment(&s))
            continue;
        len = strlen(s);

        if (len >= 3 && s[0] == '[' && s[len - 1] == ']')
        {
            char *name = s + 1;
            while (isspace(*name))
                ++name;

            char *name_end = s + len - 1;
            *name_end-- = '\0';
            while (isspace(*name_end))
                *name_end-- = '\0';

            if ((curr = find_section(head, name)) == NULL)
            {
                if ((curr = create_section(head, name)) == NULL)
                {
                    free(line);

                    return NULL;
                }

                if (head == NULL)
                    head = curr;
                if (prev != NULL)
                    prev->next = curr;

                prev = curr;
                arg_prev = NULL;
            }
            else
            {
                arg_prev = curr->args;
                while (arg_prev->next != NULL)
                    arg_prev = arg_prev->next;
            }

            continue;
        }

        char *delimiter = strchr(s, '=');
        if (delimiter == NULL)
            continue;
        *delimiter = '\0';

        char *name = s;
        char *name_end = delimiter - 1;
        while (isspace(*name_end))
            *name_end-- = '\0';

        char *value = delimiter + 1;
        while (isspace(*value))
            value++;

        if (curr == NULL)
        {
            if ((curr = create_section(head, "global")) == NULL)
            {
                free(line);

                return NULL;
            }

            if (head == NULL)
                head = curr;
            prev = curr;
            arg_prev = NULL;
        }

        if ((arg_curr = find_arg(curr, name)) == NULL)
        {
            arg_curr = create_arg(head, name, value);
            if (arg_curr == NULL)
            {
                free(line);

                return NULL;
            }

            if (arg_prev)
                arg_prev->next = arg_curr;
            if (curr->args == NULL)
                curr->args = arg_curr;

            arg_prev = arg_curr;
        }
        else
        {
            char *old_value = arg_curr->value;

            if ((arg_curr->value = strdup(value)) == NULL)
            {
                ini_free(head);

                free(line);

                return NULL;
            }

            free(old_value);
        }
    }

    free(line);
    fclose(fp);

    if (head == NULL)
    {
        if ((head = calloc(1, sizeof(struct ini_section))) == NULL)
            return NULL;
    }

    ini_print(head);

    return head;
}
Beispiel #12
0
//--------------------------------------------------------------------------
//
//      load file into the database.
//
void idaapi load_file(linput_t *li, ushort /*neflag*/, const char * /*fileformatname*/)
{
//  int i;
  aif_header_t hd;
  if ( ph.id != PLFM_ARM )
    set_processor_type("arm", SETPROC_ALL|SETPROC_FATAL);
  lread(li,&hd,sizeof(hd));
  mf = uchar(match_zero_code(hd) - 1);
  if ( (hd.address_mode & 0xFF) != 32 )
  {
    if ( (hd.address_mode & 0xFF) != 0 )
      loader_failure("26-bit modules are not supported");
    msg("Old AIF format file...");
  }
  if ( hd.decompress_code != NOP )
    loader_failure("Compressed modules are not supported");
  if ( hd.self_reloc_code != NOP )
    loader_failure("Self-relocating modules are not supported");

  inf.baseaddr = 0;
  int isexec = is_bl(hd.entry_point);
  uint32 offset = sizeof(aif_header_t);
  uint32 start = hd.image_base;
  if ( isexec ) {
    start += sizeof(aif_header_t);
    hd.readonly_size -= sizeof(aif_header_t);
  }
  uint32 end = start + hd.readonly_size;
  file2base(li, offset, start, end, FILEREG_PATCHABLE);
  offset += hd.readonly_size;
  create_section(1, start, end, NAME_CODE, CLASS_CODE);
  if ( hd.readwrite_size != 0 ) {
    start = (hd.address_mode & AIF_SEP_DATA) ? hd.data_base : end;
    end = start + hd.readwrite_size;
    file2base(li, offset, start, end, FILEREG_PATCHABLE);
    offset += hd.readwrite_size;
    create_section(2, start, end, NAME_DATA, CLASS_DATA);
  }
  if ( hd.zero_init_size != 0 ) {
    start = end;
    end = start + hd.zero_init_size;
    create_section(3, start, end, NAME_BSS, CLASS_BSS);
  }
  create_filename_cmt();

  if ( isexec )
    hd.entry_point = hd.image_base
                   + offsetof(aif_header_t,entry_point)
                   + ((hd.entry_point & ~BLMASK) << 2)
                   + 8;
  inf.start_cs = 1;
  inf.startIP  = hd.entry_point;
  inf.beginEA  = hd.entry_point;

  if ( hd.debug_size != 0 ) {
    msg("Debugging information is present (%u bytes at file offset 0x%X)...\n",
                                                        hd.debug_size, offset);
    uchar *di = qalloc_array<uchar>(size_t(hd.debug_size));
    if ( di == NULL )
      nomem("AIF debugging info");
    qlseek(li,offset);
    lread(li, di, size_t(hd.debug_size));
    uchar *ptr = di;
    uchar *end = di + size_t(hd.debug_size);
    section_t *sect = NULL;
    while ( ptr < end )
    {
      size_t len = process_item(ptr, end-ptr, sect);
      if ( len == 0 )
      {
        warning("Corrupted debug info.");
        break;
      }
      ptr += len;
    }
    qfree(di);
  }

}