示例#1
0
文件: execfile.c 项目: wodz/open21xx
static void fixup_symbols( void )
{
    bbtree_node_t *parent;
    OBJECT_FILE *file;
    Elf_Scn *section;
    Elf32_Shdr *shdr;
    Elf32_Sym *symbol;
    global_t *global_sym;
    int i;
    int compare;
    char *sym_name;

    file = objects;
    while (file)
    {
        section = NULL;
        while ((section = elf_nextscn(file->elf, section)) != 0)
        {
            shdr = elf32_getshdr(section);
            elfcheck( shdr != NULL );
            if ( (shdr->sh_type == SHT_PROGBITS ||
                  shdr->sh_type == SHT_NOBITS) &&
                 (shdr->sh_flags & SHF_REFERENCED) == 0)
            {
                yyerror( "Section %s in file %s is not referenced\n",
                         file->strings + shdr->sh_name, file->name );
            }
        }
        symbol = file->symbols + 1;
        for ( i = 1 ; i < file->symbol_count ; ++i )
        {
            sym_name = elf_strptr( file->elf, file->string_index,
                                   symbol->st_name);
            if (symbol->st_shndx != SHN_UNDEF)
            {
                exec_getsection( file->elf, symbol->st_shndx,
                                 &shdr, NULL );
                symbol->st_value += shdr->sh_addr;
                if (i >= file->first_global)
                {
                    parent = bbtree_preinsert( &globals, sym_name,
                                               &compare );
                    if (!parent || compare != 0)
                    {
                        global_sym = (global_t *)malloc(sizeof(*global_sym));
                        if (global_sym)
                        {
                            global_sym->file = file;
                            global_sym->name = sym_name;
                            global_sym->symbol = symbol;
                            bbtree_insert( &globals, parent, &global_sym->node,
                                           compare );
                        }
                        else
                        {
                            yyerror( "Failed to allocate memory\n" );
                        }

                    }
                    else
                    {
                        yyerror( "Global symbol \"%s\" multiply defined\n",
                                 sym_name );
                    }
                }
            }
            else if (i < file->first_global)
            {
                yyerror( "Symbol \"%s\" is undefined but local\n",
                         sym_name );
            }
            ++symbol;
        }
        file = file->next_object;
    }
}
示例#2
0
文件: execfile.c 项目: wodz/open21xx
static OBJECT_FILE *object_open( const char *file_name )
{
    OBJECT_FILE *file;
    Elf_Scn *section;
    Elf_Data *symtab;
    Elf32_Shdr *shdr;
    int compare;

    // search for the file in the list of already open objects
    for (file = objects ; file ; file = file->next_object)
    {
        compare = strcmp( file_name, file->name );
        if ( compare == 0 ) // if we found it
        {
            return file;
        }
        else if ( compare < 0 )  // if we didn't find it
        {
            break;
        }
    }

    // try to add a new file to the list of open objects
    file = (OBJECT_FILE *)malloc( sizeof(OBJECT_FILE) );
    assert(file);
    if ( file )
    {
        memset( file, 0, sizeof(*file) );
        if ((file->fd = open(file_name, O_RDONLY | O_BINARY)) != -1)
        {
            file->elf = elf_begin(file->fd, ELF_C_READ, NULL);
            if (file->elf)
            {
                file->ehdr = elf32_getehdr(file->elf);
                if (file->ehdr)
                {
                    if ( file->ehdr->e_machine == out_file_machine )
                    {
                        Elf_Data *string_data;
                        OBJECT_FILE **scan;
                        int compare;

                        exec_getsection( file->elf, file->ehdr->e_shstrndx,
                                         NULL, &string_data );
                        file->strings = (char *)string_data->d_buf;
                        file->name = strdup( file_name );

                        section = NULL;
                        while ((section =
                                elf_nextscn(file->elf, section)) != 0)
                        {
                            shdr = elf32_getshdr(section);
                            elfcheck( shdr != NULL );
                            if (shdr->sh_type == SHT_SYMTAB)
                            {
                                symtab = exec_getdata(section);
                                file->symbols = symtab->d_buf;
                                file->symbol_count = symtab->d_size /
                                    sizeof(Elf32_Sym);
                                file->first_global = shdr->sh_info;
                                file->string_index = shdr->sh_link;
                                if ( file->symbol_count > max_symbols )
                                {
                                    max_symbols = file->symbol_count;
                                }
                                break;
                            }
                        }

                        // ordered insert into the file list
                        assert( file->name );
                        for ( scan = &objects ; *scan ;
                              scan = &(*scan)->next_object )
                        {
                            compare = strcmp(file_name, (*scan)->name );
                            // if this file already exists we should have found
                            // it instead of creating it
                            assert( compare != 0 );
                            if (compare < 0)
                            {
                                break;
                            }
                        }
                        file->next_object = *scan;
                        *scan = file;
                        return file;
                    }
                    else
                    {
                        yyerror(
                            "Input file machine type doesn't match architecture" );
                    }
                }
                else
                {
                    yyerror( "File %s is not a valid ELF file: %s",
                             file_name, elf_errmsg(-1) );
                }
                elf_end(file->elf);
            }
            else
            {
                yyerror( "Elf_begin on %s failed: %s",
                         file_name, elf_errmsg(-1) );
            }
            close( file->fd );
        }
        else
        {
            yyerror( "File %s not found", file_name );
        }
        free( file );
        file = NULL;
    }
    return file;
}
示例#3
0
文件: execfile.c 项目: wodz/open21xx
int add_object_sections(NAME_LIST *section_name_list)
{
    OBJECT_FILE **current;
    Elf_Scn *elf_section;
    Elf32_Shdr *shdr;
    NAME_LIST *section_name;
    Elf_Scn **array;
    int scns_available;
    int scns_added;
    array_index_t link_index;
    int page_links;
    const char *input_name;

    scns_added = scns_available = 0;
    array = NULL;
    array_index_init( &link_index, &link_array );
    current = (OBJECT_FILE **)array_index_next_page(
        &link_index, &page_links );
    while (current)
    {
        elf_section = NULL;
        while ((elf_section = elf_nextscn((*current)->elf, elf_section)) != 0)
        {
            shdr = elf32_getshdr(elf_section);
            elfcheck( shdr != NULL );
            if ( shdr->sh_type == SHT_PROGBITS ||
                 shdr->sh_type == SHT_NOBITS )
            {
                input_name = (*current)->strings+shdr->sh_name;
                for ( section_name = section_name_list ; section_name ;
                      section_name = section_name->next )
                {
                    if (strcmp( input_name, section_name->name) == 0)
                    {
                        if ((shdr->sh_flags & SHF_REFERENCED) == 0)
                        {
                            if ( scns_added >= scns_available )
                            {
                                array_update( &scn_array, scns_added );
                                scns_added = 0;
                                array = array_alloc( &scn_array,
                                                     &scns_available );
                            }
                            *array++ = elf_section;
                            ++scns_added;
//                            elf_flagshdr( elf_section, ELF_C_SET, SHF_REFERENCED );
                            shdr->sh_flags |= SHF_REFERENCED;
                        }
                        else
                        {
                            yyerror( "Input section is referenced twice" );
                        }
                        break;
                    }
                }
            }
        }
        ++current;
        if ( --page_links == 0 )
        {
            current = (OBJECT_FILE **)array_index_next_page(
                &link_index, &page_links );
        }
    }
    array_update( &scn_array, scns_added );
    return 1;
}
示例#4
0
/*
 * verify the ELF structure of a module
 */
static int module_verify_elf(struct module_verify_data *mvdata)
{
	const Elf_Ehdr *hdr = mvdata->hdr;
	const Elf_Shdr *section, *section2, *secstop;
	const Elf_Rela *relas, *rela, *relastop;
	const Elf_Rel *rels, *rel, *relstop;
	const Elf_Sym *symbol, *symstop;
	size_t size, sssize, *secsize, tmp, tmp2;
	long last;
	int line;

	size = mvdata->size;
	mvdata->nsects = hdr->e_shnum;

#define elfcheck(X) \
do { if (unlikely(!(X))) { line = __LINE__; goto elfcheck_error; } } while(0)

#define seccheck(X) \
do { if (unlikely(!(X))) { line = __LINE__; goto seccheck_error; } } while(0)

#define symcheck(X) \
do { if (unlikely(!(X))) { line = __LINE__; goto symcheck_error; } } while(0)

#define relcheck(X) \
do { if (unlikely(!(X))) { line = __LINE__; goto relcheck_error; } } while(0)

#define relacheck(X) \
do { if (unlikely(!(X))) { line = __LINE__; goto relacheck_error; } } while(0)

	/* validate the ELF header */
	elfcheck(hdr->e_ehsize < size);
	elfcheck(hdr->e_entry == 0);
	elfcheck(hdr->e_phoff == 0);
	elfcheck(hdr->e_phnum == 0);

	elfcheck(hdr->e_shnum < SHN_LORESERVE);
	elfcheck(hdr->e_shoff < size);
	elfcheck(hdr->e_shoff >= hdr->e_ehsize);
	elfcheck((hdr->e_shoff & (sizeof(long) - 1)) == 0);
	elfcheck(hdr->e_shstrndx > 0);
	elfcheck(hdr->e_shstrndx < hdr->e_shnum);
	elfcheck(hdr->e_shentsize == sizeof(Elf_Shdr));

	tmp = (size_t) hdr->e_shentsize * (size_t) hdr->e_shnum;
	elfcheck(tmp <= size - hdr->e_shoff);

	/* allocate a table to hold in-file section sizes */
	mvdata->secsizes = kmalloc(hdr->e_shnum * sizeof(size_t), GFP_KERNEL);
	if (!mvdata->secsizes)
		return -ENOMEM;

	memset(mvdata->secsizes, 0, hdr->e_shnum * sizeof(size_t));

	/* validate the ELF section headers */
	mvdata->sections = mvdata->buffer + hdr->e_shoff;
	secstop = mvdata->sections + mvdata->nsects;

	sssize = mvdata->sections[hdr->e_shstrndx].sh_size;
	elfcheck(sssize > 0);

	section = mvdata->sections;
	seccheck(section->sh_type == SHT_NULL);
	seccheck(section->sh_size == 0);
	seccheck(section->sh_offset == 0);

	secsize = mvdata->secsizes + 1;
	for (section++; section < secstop; secsize++, section++) {
		seccheck(section->sh_name < sssize);
		seccheck(section->sh_link < hdr->e_shnum);

		if (section->sh_entsize > 0)
			seccheck(section->sh_size % section->sh_entsize == 0);

		seccheck(section->sh_offset >= hdr->e_ehsize);
		seccheck(section->sh_offset < size);

		/* determine the section's in-file size */
		tmp = size - section->sh_offset;
		if (section->sh_offset < hdr->e_shoff)
			tmp = hdr->e_shoff - section->sh_offset;

		for (section2 = mvdata->sections + 1; section2 < secstop; section2++) {
			if (section->sh_offset < section2->sh_offset) {
				tmp2 = section2->sh_offset - section->sh_offset;
				if (tmp2 < tmp)
					tmp = tmp2;
			}
		}
		*secsize = tmp;

		_debug("Section %ld: %zx bytes at %lx\n",
		       section - mvdata->sections,
		       *secsize,
		       section->sh_offset);

		/* perform section type specific checks */
		switch (section->sh_type) {
		case SHT_NOBITS:
			break;

		case SHT_REL:
			seccheck(section->sh_entsize == sizeof(Elf_Rel));
			goto more_rel_checks;

		case SHT_RELA:
			seccheck(section->sh_entsize == sizeof(Elf_Rela));
		more_rel_checks:
			seccheck(section->sh_info > 0);
			seccheck(section->sh_info < hdr->e_shnum);
			goto more_sec_checks;

		case SHT_SYMTAB:
			seccheck(section->sh_entsize == sizeof(Elf_Sym));
			goto more_sec_checks;

		default:
		more_sec_checks:
			/* most types of section must be contained entirely
			 * within the file */
			seccheck(section->sh_size <= *secsize);
			break;
		}
	}

	/* validate the ELF section names */
	section = &mvdata->sections[hdr->e_shstrndx];

	seccheck(section->sh_offset != hdr->e_shoff);

	mvdata->secstrings = mvdata->buffer + section->sh_offset;

	last = -1;
	for (section = mvdata->sections + 1; section < secstop; section++) {
		const char *secname;
		tmp = sssize - section->sh_name;
		secname = mvdata->secstrings + section->sh_name;
		seccheck(secname[0] != 0);
		if (section->sh_name > last)
			last = section->sh_name;
	}

	if (last > -1) {
		tmp = sssize - last;
		elfcheck(memchr(mvdata->secstrings + last, 0, tmp) != NULL);
	}

	/* look for various sections in the module */
	for (section = mvdata->sections + 1; section < secstop; section++) {
		switch (section->sh_type) {
		case SHT_SYMTAB:
			if (strcmp(mvdata->secstrings + section->sh_name,
				   ".symtab") == 0
			    ) {
				seccheck(mvdata->symbols == NULL);
				mvdata->symbols =
					mvdata->buffer + section->sh_offset;
				mvdata->nsyms =
					section->sh_size / sizeof(Elf_Sym);
				seccheck(section->sh_size > 0);
			}
			break;

		case SHT_STRTAB:
			if (strcmp(mvdata->secstrings + section->sh_name,
				   ".strtab") == 0
			    ) {
				seccheck(mvdata->strings == NULL);
				mvdata->strings =
					mvdata->buffer + section->sh_offset;
				sssize = mvdata->nstrings = section->sh_size;
				seccheck(section->sh_size > 0);
			}
			break;
		}
	}

	if (!mvdata->symbols) {
		printk("Couldn't locate module symbol table\n");
		goto format_error;
	}

	if (!mvdata->strings) {
		printk("Couldn't locate module strings table\n");
		goto format_error;
	}

	/* validate the symbol table */
	symstop = mvdata->symbols + mvdata->nsyms;

	symbol = mvdata->symbols;
	symcheck(ELF_ST_TYPE(symbol[0].st_info) == STT_NOTYPE);
	symcheck(symbol[0].st_shndx == SHN_UNDEF);
	symcheck(symbol[0].st_value == 0);
	symcheck(symbol[0].st_size == 0);

	last = -1;
	for (symbol++; symbol < symstop; symbol++) {
		symcheck(symbol->st_name < sssize);
		if (symbol->st_name > last)
			last = symbol->st_name;
		symcheck(symbol->st_shndx < mvdata->nsects ||
			 symbol->st_shndx >= SHN_LORESERVE);
	}

	if (last > -1) {
		tmp = sssize - last;
		elfcheck(memchr(mvdata->strings + last, 0, tmp) != NULL);
	}

	/* validate each relocation table as best we can */
	for (section = mvdata->sections + 1; section < secstop; section++) {
		section2 = mvdata->sections + section->sh_info;

		switch (section->sh_type) {
		case SHT_REL:
			rels = mvdata->buffer + section->sh_offset;
			relstop = mvdata->buffer + section->sh_offset + section->sh_size;

			for (rel = rels; rel < relstop; rel++) {
				relcheck(rel->r_offset < section2->sh_size);
				relcheck(ELF_R_SYM(rel->r_info) < mvdata->nsyms);
			}

			break;

		case SHT_RELA:
			relas = mvdata->buffer + section->sh_offset;
			relastop = mvdata->buffer + section->sh_offset + section->sh_size;

			for (rela = relas; rela < relastop; rela++) {
				relacheck(rela->r_offset < section2->sh_size);
				relacheck(ELF_R_SYM(rela->r_info) < mvdata->nsyms);
			}

			break;

		default:
			break;
		}
	}


	_debug("ELF okay\n");
	return 0;

 elfcheck_error:
	printk("Verify ELF error (assertion %d)\n", line);
	goto format_error;

 seccheck_error:
	printk("Verify ELF error [sec %ld] (assertion %d)\n",
	       (long)(section - mvdata->sections), line);
	goto format_error;

 symcheck_error:
	printk("Verify ELF error [sym %ld] (assertion %d)\n",
	       (long)(symbol - mvdata->symbols), line);
	goto format_error;

 relcheck_error:
	printk("Verify ELF error [sec %ld rel %ld] (assertion %d)\n",
	       (long)(section - mvdata->sections),
	       (long)(rel - rels), line);
	goto format_error;

 relacheck_error:
	printk("Verify ELF error [sec %ld rela %ld] (assertion %d)\n",
	       (long)(section - mvdata->sections),
	       (long)(rela - relas), line);
	goto format_error;

 format_error:
	return -ELIBBAD;

} /* end module_verify_elf() */