示例#1
0
文件: sym.c 项目: danilaslau/frotznet
int __dl_patchup (lib_t *lib)
{
	int size;
	elf32_rel_t *rel;

	rel = (elf32_rel_t *) elf_find_section_data (lib->hdr, ".rel.text");
	size = elf_section_size (lib->hdr, ".rel.text") / sizeof (elf32_rel_t);
	if (__dl_patch_section (lib, rel, size))
		return -1;
	rel = (elf32_rel_t *) elf_find_section_data (lib->hdr, ".rel.data");
	size = elf_section_size (lib->hdr, ".rel.data") / sizeof (elf32_rel_t);
	if (__dl_patch_section (lib, rel, size))
		return -1;
	return 0;
}
int lua_elf_section_t_disassemble (lua_State * L)
{
    struct _elf_section * section;
    uint_t * address;
    uint_t   address_tmp;
    unsigned char * data;
    int disassemble_address = 0;
    
    section = lua_check_elf_section(L, 1);
    if (lua_isuserdata(L, 2)) {
        address = lua_check_uint_t(L, 2);
        disassemble_address = 1;
        lua_pop(L, 2);
    }
    else
        lua_pop(L, 1);
    
    if (disassemble_address) {
        uint_t_set(&address_tmp, address);
        uint_t_sub(&address_tmp, elf_section_addr(section));
        data = elf_section_data(section);
        lua_dis_instruction(L, &(data[uint_t_get(&address_tmp)]),
                            ELF_CLASS(section->elf));
    }
    else
        lua_dis_table(L,
                      elf_section_addr(section),
                      elf_section_data(section),
                      int_t_get(elf_section_size(section)), 
                      ELF_CLASS(section->elf));

    return 1;
}
int lua_elf_section_t_rop_table (lua_State * L)
{
    struct _elf_section * section;
    struct _rop_list * rop_list;
    struct _rop_list * rop_list_first;
    int                rop_depth;
    int                rop_i;
    uint_t             address;
    
    section = lua_check_elf_section(L, 1);
    rop_depth = luaL_checkinteger(L, 2);
    lua_pop(L, 2);
    
    rop_list_first = rop_ret_rops(elf_section_data(section),
                                  int_t_get(elf_section_size(section)),
                                  rop_depth,
                                  ELF_CLASS(section->elf));
    rop_list = rop_list_first;
    
    rop_i = 1;
    lua_newtable(L);
    while (rop_list != NULL) {
        rop_list_ins(rop_list);
        uint_t_set(&address, elf_section_addr(section));
        uint_t_add_int(&address, rop_list->offset);
        lua_dis_table(L, &address, rop_list->bytes,
                      rop_list->bytes_size, ELF_CLASS(section->elf));
        lua_pushinteger(L, (lua_Integer) rop_i);
        lua_insert(L, -2);
        lua_settable(L, -3);
        rop_i++;
        rop_list = rop_list->next;
    }
    
    rop_list_destroy(rop_list_first);
    
    return 1;
}
int lua_elf_symbol_t_disassemble (lua_State * L)
{
    struct lua_elf_symbol_t * symbol_t;
    struct _elf * elf;
    struct _elf_symbol symbol;
    struct _elf_section section;
    unsigned char * data;
    int section_i;
    int symbol_i;
    uint_t offset;
    
    int end_address_set = 0;
    uint_t end_address;
    uint_t_8_set(&end_address, 0);
    
    symbol_t = lua_check_elf_symbol_t(L, 1);
    lua_pop(L, 1);
    
    elf = symbol_t->section_t->elf_t->elf;
    // find function sym at next highest address, save it's start address in
    // end_address
    for (section_i = 0; section_i < int_t_get(elf_shnum(elf)); section_i++) {
        elf_section(elf, &section, section_i);
        if (int_t_get(elf_section_type(&section)) == SHT_SYMTAB) {
            for (symbol_i = 0; symbol_i < elf_section_num(&section); symbol_i++) {
                elf_section_symbol(&section, &symbol, symbol_i);
                if (    (elf_symbol_type(&symbol) == STT_FUNC)
                     && (uint_t_cmp(elf_symbol_value(&(symbol_t->symbol)),
                                    elf_symbol_value(&symbol)) < 0)
                     && (uint_t_cmp(elf_symbol_shndx(&symbol),
                                    elf_symbol_shndx(&(symbol_t->symbol)))
                         == 0)
                     && (    (uint_t_cmp(elf_symbol_value(&symbol),
                                         &end_address) < 0)
                          || (end_address_set == 0)
                        )
                   ) {
                    uint_t_set(&end_address, elf_symbol_value(&symbol));
                    end_address_set = 1;
                }
            }
        }
    }
    
    // end address not set? set it to the end of the linked section
    if (end_address_set == 0) {
        elf_section(elf, &section, 
                    uint_t_get(elf_symbol_shndx(&(symbol_t->symbol))));
        uint_t_int_t(&end_address, elf_section_size(&section));
        uint_t_add(&end_address, elf_section_addr(&section));
    }
    
    
    // load the linked section
    elf_section(elf, &section, uint_t_get(elf_symbol_shndx(&(symbol_t->symbol))));
    data = elf_section_data(&section);
    uint_t_set(&offset, elf_symbol_value(&(symbol_t->symbol)));
    uint_t_sub(&offset, elf_section_addr(&section));
    
    // disassemble that function like it was 1980
    lua_dis_table(L,
                  elf_symbol_value(&(symbol_t->symbol)),
                  &(data[uint_t_get(&offset)]),
                  uint_t_get(&end_address)
                  - uint_t_get(elf_symbol_value(&(symbol_t->symbol))),
                  ELF_CLASS(elf));
                  
    return 1;
}
int lua_elf_section_t_address (lua_State * L)
{
    LUA_ELF_SECTION_T_ACCESSOR(lua_push_uint_t, elf_section_addr(section))
}


int lua_elf_section_t_exec (lua_State * L)
{
    LUA_ELF_SECTION_T_ACCESSOR(lua_pushboolean, elf_section_exec(section))
}


int lua_elf_section_t_size (lua_State * L)
{
    LUA_ELF_SECTION_T_ACCESSOR(lua_push_int_t, elf_section_size(section))
}


int lua_elf_section_t_offset (lua_State * L)
{
    LUA_ELF_SECTION_T_ACCESSOR(lua_push_uint_t, elf_section_offset(section))
}


int lua_elf_section_t_type (lua_State * L)
{
    LUA_ELF_SECTION_T_ACCESSOR(lua_pushstring,
        elf_section_type_strings[int_t_get(elf_section_type(section))])
}