当前位置: 首页>>代码示例>>C++>>正文


C++ elf_strptr函数代码示例

本文整理汇总了C++中elf_strptr函数的典型用法代码示例。如果您正苦于以下问题:C++ elf_strptr函数的具体用法?C++ elf_strptr怎么用?C++ elf_strptr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了elf_strptr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: elf_version

static elf_info_s *elf_symbols(int fd)
{
    /* Open Elf */
    elf_version(EV_CURRENT);
    elf_info_s *elf = calloc(1, sizeof (elf_info_s));
    elf->elf = elf_begin(fd, ELF_C_READ, NULL);
    gelf_getehdr(elf->elf, &elf->hdr);

    /* Iterate through the sections */
    Elf_Scn *scn = NULL;
    while ((scn = elf_nextscn(elf->elf, scn)) != 0) {
        GElf_Shdr shdr;
        const char *name;
        /* get section header */
        gelf_getshdr(scn, &shdr);
        /* get section name */
        name = elf_strptr(elf->elf, elf->hdr.e_shstrndx, shdr.sh_name);
        //LOG(INFO, "Iter on %s", name);

        if (shdr.sh_type == SHT_DYNSYM)
            handle_dynsym_section(elf, scn, shdr);
        else if (shdr.sh_type == SHT_DYNAMIC)
            handle_dynamic_section(elf, scn, shdr);
        else if ((shdr.sh_type == SHT_PROGBITS || shdr.sh_type == SHT_NOBITS)
                && !strcmp(name, ".plt"))
            handle_plt_section(elf, shdr);
        else if (!strcmp(name, ".got.plt"))
            handle_gotplt_section(elf, shdr);
    }
    return elf;
}
开发者ID:schischi,项目名称:my_ltrace,代码行数:31,代码来源:elfd.c

示例2: resolve_globals

static void resolve_globals( void )
{
    OBJECT_FILE *file;
    global_t *global_sym;
    Elf32_Sym *symbol;
    int i;
    int compare;
    char *sym_name;

    file = objects;
    while (file)
    {
        symbol = file->symbols + file->first_global;
        for ( i = file->first_global ; i < file->symbol_count ; ++i )
        {
            sym_name = elf_strptr( file->elf, file->string_index,
                                   symbol->st_name);
            if (symbol->st_shndx == SHN_UNDEF)
            {
                global_sym = (global_t *)bbtree_preinsert( &globals, sym_name,
                                                           &compare );
                if (compare == 0)
                {
                    symbol->st_value = global_sym->symbol->st_value;
                    /* set to any section other than SHN_UNDEF */
                    symbol->st_shndx = 1;
                }
                /* not an error until a symbol is referenced */
            }
            ++symbol;
        }
        file = file->next_object;
    }
}
开发者ID:wodz,项目名称:open21xx,代码行数:34,代码来源:execfile.c

示例3: get_embedded_repo

std::string get_embedded_repo() {
  GElf_Shdr shdr;
  size_t shstrndx;
  char *name;
  Elf_Scn *scn;

  if (elf_version(EV_CURRENT) == EV_NONE) return "";

  int fd = open("/proc/self/exe", O_RDONLY, 0);
  if (fd < 0) return "";
  Elf* e = elf_begin(fd, ELF_C_READ, nullptr);

  if (!e ||
      elf_kind(e) != ELF_K_ELF ||
      !elf_getshstrndx(e, &shstrndx)) {
    return "";
  }
  scn = nullptr;
  while ((scn = elf_nextscn(e, scn)) != nullptr) {
    if (gelf_getshdr(scn, &shdr) != &shdr ||
        !(name = elf_strptr(e, shstrndx , shdr.sh_name))) {
      return "";
    }
    if (!strcmp("repo", name)) {
      GElf_Shdr ghdr;
      if (gelf_getshdr(scn, &ghdr) != &ghdr) return "";
      char buf[512];
      sprintf(buf, "/proc/self/exe:%lu:%lu", ghdr.sh_offset, ghdr.sh_size);
      sqlite3_embedded_initialize(nullptr, true);
      return buf;
    }
  }
  return "";
}
开发者ID:ChrisOHu,项目名称:hiphop-php,代码行数:34,代码来源:embedded_repo.cpp

示例4: find_symbol

int find_symbol(char *fn, struct lsym *lsym, unsigned long baseaddr)
{
	struct lsym *ls;
	int num = 0;
	for (ls = lsym; ls->name; ls++)
		num++;

	elf_version(EV_CURRENT);
	int fd = open(fn, O_RDONLY);
	if (fd < 0)
		return -1;

	long ret = -1;
	Elf *elf = elf_begin(fd, ELF_C_READ, NULL);
	if (elf == NULL)
		goto out;

	GElf_Ehdr header;
	if (!gelf_getehdr(elf, &header))
		goto out_elf;

	Elf_Scn *section = NULL;
	int found = 0;
	while (found < num && (section = elf_nextscn(elf, section)) != 0) {
		GElf_Shdr shdr, *sh;
		sh = gelf_getshdr(section, &shdr);

		if (sh->sh_type == SHT_SYMTAB || sh->sh_type == SHT_DYNSYM) {
			Elf_Data *data = elf_getdata(section, NULL);
			GElf_Sym *sym, symbol;
			int j;

			unsigned numsym = sh->sh_size / sh->sh_entsize;
			for (j = 0; j < numsym; j++) {
				sym = gelf_getsymshndx(data, NULL, j, &symbol, NULL);
				char *symname = elf_strptr(elf, shdr.sh_link, sym->st_name);
				for (ls = lsym; ls->name; ls++) {
					if (!strcmp(symname, ls->name)) {
						Elf_Scn *oscn = elf_getscn(elf, sym->st_shndx);
						GElf_Shdr oshdr, *osh;
						osh = gelf_getshdr(oscn, &oshdr);
						ls->addr = (sym->st_value - osh->sh_addr) + osh->sh_offset + baseaddr;
						found++;
						if (found == num)
							break;
					}
				}
			}
		}
	}
	if (found == num)
		ret = 0;

out_elf:
	elf_end(elf);

out:
	close(fd);
	return ret;
}
开发者ID:gcracker,项目名称:tsx-tools,代码行数:60,代码来源:sym.c

示例5: get_scn_hdr

scn_hdr_t* get_scn_hdr(int idx)
{
	Elf_Scn* scn = g.scns[idx];
	GElf_Shdr shdr = g.shdrs[idx];

	Elf_Data* data;
	size_t n;
	
	// if ( gelf_getshdr(scn , &shdr) != &shdr ) {
	// 	errx ( EXIT_FAILURE,
	// 	       " getshdr( shstrndx ) failed : %s.",
	// 	       elf_errmsg ( -1));
	// 	return NULL;
	// }

	// data = NULL; size_t buf_sz = 0;
	// while ((data=elf_getdata(scn, data)) != NULL) {
	// 	printf("+%d", data->d_size);
	// 	buf_sz += data->d_size;
	// }

	uint8_t* buf = (uint8_t*) malloc(shdr.sh_size);
	if (buf == NULL) {		
		errx (EXIT_FAILURE, "malloc failed");
		return NULL;
	}

	data = NULL; n = 0;
	while (n < shdr.sh_size &&
	       (data = elf_getdata(scn, data)) != NULL) {
		uint8_t* p = (uint8_t*)data->d_buf;
		size_t dsize = data->d_size;
		if(p != NULL)
			memcpy(buf+n, p, dsize);
		else //FIXME: seems some sections are empty in file
			memset(buf+n, 0, dsize);
		n += dsize;
	}

	// get the name string
	char* name;
	if ((name = elf_strptr(g.e, g.shstrndx, shdr.sh_name)) == NULL)
	  errx(EXIT_FAILURE , " elf_strptr()  failed : %s.",
	       elf_errmsg (-1));
	

	scn_hdr_t* scn_hdr_p = (scn_hdr_t*)malloc(sizeof(scn_hdr_t));
	if (scn_hdr_p == NULL) {
		errx (EXIT_FAILURE , "scn_hdr_t malloc failed");
		return NULL;
	}

	scn_hdr_p->sh_idx = idx;
	scn_hdr_p->name = name;
	scn_hdr_p->sh_addr = shdr.sh_addr;
	scn_hdr_p->sh_size = n;
	scn_hdr_p->data = buf;

	return scn_hdr_p;
}
开发者ID:grasshopyx,项目名称:luaelf,代码行数:60,代码来源:libelfconn.c

示例6: find_injected_secaddr

int
find_injected_secaddr(elf_data_t *elf, inject_data_t *inject)
{
  Elf_Scn *scn;
  GElf_Shdr shdr;
  uint64_t max_inject_addr = 0;
  char* s;
  size_t shstrndx;

  if(elf_getshdrstrndx(elf->e, &shstrndx) < 0) {
    return -1;
  }

  scn = NULL;
  while((scn = elf_nextscn(elf->e, scn))) {
    if(!gelf_getshdr(scn, &shdr)) {
      return -1;
    }
    s = elf_strptr(elf->e, shstrndx, shdr.sh_name);
    if(!s) {
      return -1;
    }

    if(!strcmp(s, DYNINST_NAME))
		fprintf(finfo,"dyninst end=%lx\n",shdr.sh_addr + shdr.sh_size);
	if (shdr.sh_addr + shdr.sh_offset > max_inject_addr)
		max_inject_addr = shdr.sh_addr + shdr.sh_size;
  }
  inject->secaddr = max_inject_addr + inject->len + 0x4000;
  return 0;
}
开发者ID:sirmc,项目名称:XnRmor,代码行数:31,代码来源:elfinject.c

示例7: elf_scn_getsymaddr_byname

/**
 * @brief Search a single, particular ELF symbol table for a named symbol
 *
 * @param elf The open ELF object
 * @param symtab The ELF symbol table section
 * @param name The symbol name to find
 *
 * @returns The address to which the symbol points, 0 otherwise.
 */
static GElf_Addr elf_scn_getsymaddr_byname(Elf *elf, Elf_Scn *symtab,
                                           const char * name) {
    GElf_Shdr shdr;
    Elf_Data *data;
    uint32_t syms, i;
    GElf_Sym sym;

    if (gelf_getshdr(symtab, &shdr) == NULL || shdr.sh_type != SHT_SYMTAB) {
        return 0;
    }

    data = elf_getdata(symtab, NULL);
    if (data == NULL) {
        return 0;
    }

    syms = shdr.sh_size / shdr.sh_entsize;

    for(i=0; i<syms; i++) {
        gelf_getsym(data, i, &sym);
        if (strcmp(elf_strptr(elf, shdr.sh_link, sym.st_name), name) == 0) {
            return sym.st_value;
        }
    }

    return 0;
}
开发者ID:girling-morgan,项目名称:bootrom-tools,代码行数:36,代码来源:tftf_in.c

示例8: readSymbols

static void readSymbols(Elf *e, Elf_Scn *scn, const GElf_Shdr &shdr,
                        unsigned low, unsigned high,
                        std::auto_ptr<CoreSymbolInfo> &SI)
{
  Elf_Data *data = elf_getdata(scn, NULL);
  if (data == NULL) {
    return;
  }
  unsigned count = shdr.sh_size / shdr.sh_entsize;

  CoreSymbolInfoBuilder builder;

  for (unsigned i = 0; i < count; i++) {
    GElf_Sym sym;
    if (gelf_getsym(data, i, &sym) == NULL) {
      continue;
    }
    if (sym.st_shndx == SHN_ABS)
      continue;
    if (sym.st_value < low || sym.st_value >= high)
      continue;
    builder.addSymbol(elf_strptr(e, shdr.sh_link, sym.st_name),
                      sym.st_value,
                      sym.st_info);
  }
  SI = builder.getSymbolInfo();
}
开发者ID:hoangt,项目名称:tool_axe,代码行数:27,代码来源:main.cpp

示例9: elf_getscn_byname

/**
 * @brief Search an elf for a named section
 *
 * @param elf The open elf object
 * @param name The section name to find
 *
 * @returns A pointer to the section if successful, NULL otherwise.
 */
static Elf_Scn * elf_getscn_byname(Elf *elf, const char * name) {
    Elf_Scn *scn = NULL;
    size_t shstrndx;
    GElf_Shdr shdr;
    char *section_name;

    if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
        fprintf(stderr, "elf_getshdrstrndx() failed: %s\n", elf_errmsg(-1));
    } else {
        /**
         * Iterate over the sections in the ELF descriptor to get the indices
         * of the .text and .data descriptors
         */
        while ((scn = elf_nextscn(elf, scn)) != NULL) {
            if (gelf_getshdr(scn, &shdr) != &shdr) {
                fprintf(stderr, "getshdr() failed: %s\n",
                    elf_errmsg(-1));
            } else {
                section_name = elf_strptr(elf, shstrndx, shdr.sh_name);
                if (section_name == NULL) {
                    fprintf(stderr, "elf_strptr() failed: %s\n",
                        elf_errmsg(-1));
                } else if (strcmp(section_name, name) == 0) {
                    break;
                }
            }
        }
    }

    return scn;
}
开发者ID:girling-morgan,项目名称:bootrom-tools,代码行数:39,代码来源:tftf_in.c

示例10: while

static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
				    GElf_Shdr *shp, const char *name,
				    size_t *idx)
{
	Elf_Scn *sec = NULL;
	size_t cnt = 1;

	/* Elf is corrupted/truncated, avoid calling elf_strptr. */
	if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
		return NULL;

	while ((sec = elf_nextscn(elf, sec)) != NULL) {
		char *str;

		gelf_getshdr(sec, shp);
		str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
		if (!strcmp(name, str)) {
			if (idx)
				*idx = cnt;
			break;
		}
		++cnt;
	}

	return sec;
}
开发者ID:alexey-pelykh,项目名称:linux,代码行数:26,代码来源:symbol-elf.c

示例11: rewrite_section_name

int
rewrite_section_name(elf_data_t *elf, inject_data_t *inject, char **err)
{
  Elf_Scn *scn;
  GElf_Shdr shdr;
  char *s;
  size_t shstrndx, stroff, strbase;

  if(strlen(inject->secname) > strlen(ABITAG_NAME)) {
    (*err) = "section name too long";
    return -1;
  }

  if(elf_getshdrstrndx(elf->e, &shstrndx) < 0) {
    (*err) = "failed to get string table section index";
    return -1;
  }

  stroff = 0;
  strbase = 0;
  scn = NULL;
  while((scn = elf_nextscn(elf->e, scn))) {
    if(!gelf_getshdr(scn, &shdr)) {
      (*err) = "failed to get section header";
      return -1;
    }

    s = elf_strptr(elf->e, shstrndx, shdr.sh_name);
    if(!s) {
      (*err) = "failed to get section name";
      return -1;
    }

    if(!strcmp(s, ABITAG_NAME)) {
      stroff = shdr.sh_name;   /* offset into shstrtab */
    } else if(!strcmp(s, SHSTRTAB_NAME)) {
      strbase = shdr.sh_offset; /* offset to start of shstrtab */
    }
  }

  if(stroff == 0) {
    (*err) = "cannot find shstrtab entry for injected section";
    return -1;
  } else if(strbase == 0) {
    (*err) = "cannot find shstrtab";
    return -1;
  }

  inject->shstroff = strbase + stroff;

  verbose("renaming rewritten section to \"%s\"", inject->secname);
  verbose("writing section string table to file");

  if(write_secname(elf, inject, err) < 0) {
    return -1;
  }

  return 0;
}
开发者ID:sirmc,项目名称:XnRmor,代码行数:59,代码来源:elfinject.c

示例12: drsym_obj_symbol_name

const char *
drsym_obj_symbol_name(void *mod_in, uint idx)
{
    elf_info_t *mod = (elf_info_t *) mod_in;
    if (mod == NULL || idx >= mod->num_syms || mod->syms == NULL)
        return NULL;
    return elf_strptr(mod->elf, mod->strtab_idx, mod->syms[idx].st_name);
}
开发者ID:unbaiat,项目名称:robocheck,代码行数:8,代码来源:drsyms_elf.c

示例13: find_elf_section_by_name

/**
 * Finds a section by its name in an ELF file.
 * @param elf
 *   A libelf handle representing the file.
 * @param section_name
 *   Name of section to be found.
 * @param data_dest
 *   Save the resulting elf data pointer here.  Cannot be NULL.
 * @param shdr_dest
 *   Save the section header here. Cannot be NULL.
 * @param error_message
 *   Will be filled by an error message if the function fails (returns
 *   zero).  Caller is responsible for calling free() on the string
 *   pointer.  If function succeeds, the pointer is not touched by the
 *   function.
 * @returns
 *   Zero on error, index of the section on success.
 */
static unsigned
find_elf_section_by_name(Elf *elf,
                         const char *section_name,
                         Elf_Data **data_dest,
                         GElf_Shdr *shdr_dest,
                         char **error_message)
{
    /* Find the string table index */
    size_t shdr_string_index;
    if (0 != elf_getshdrstrndx(elf, &shdr_string_index))
    {
        *error_message = sr_asprintf("elf_getshdrstrndx failed");
        return 0;
    }

    unsigned section_index = 0;
    Elf_Scn *section = NULL;
    while ((section = elf_nextscn(elf, section)) != NULL)
    {
        /* Starting index is 1. */
        ++section_index;

        GElf_Shdr shdr;
        if (gelf_getshdr(section, &shdr) != &shdr)
        {
            *error_message = sr_asprintf("gelf_getshdr failed");
            return 0;
        }

        const char *current_section_name = elf_strptr(elf,
                                                      shdr_string_index,
                                                      shdr.sh_name);

        if (!current_section_name)
        {
            *error_message = sr_asprintf("elf_strptr failed");
            return 0;
        }

        if (0 == strcmp(current_section_name, section_name))
        {
            /* We found the right section!  Save the data. */
            *data_dest = elf_getdata(section, NULL);
            if (!*data_dest)
            {
                *error_message = sr_asprintf("elf_getdata failed");
                return 0;
            }

            /* Save the section header. */
            *shdr_dest = shdr;
            return section_index;
        }
    }

    *error_message = sr_asprintf("Section %s not found", section_name);
    return 0;
}
开发者ID:abrt,项目名称:satyr,代码行数:76,代码来源:elves.c

示例14: find_section_base

static void
find_section_base(const char *exe, Elf *e, const char *section)
{
	Dwarf_Addr off;
	Elf_Scn *scn;
	GElf_Ehdr eh;
	GElf_Shdr sh;
	size_t shstrndx;
	int elferr;
	const char *name;

	if (gelf_getehdr(e, &eh) != &eh) {
		warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
		return;
	}

	if (!elf_getshstrndx(e, &shstrndx)) {
		warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
		return;
	}

	(void) elf_errno();
	off = 0;
	scn = NULL;
	while ((scn = elf_nextscn(e, scn)) != NULL) {
		if (gelf_getshdr(scn, &sh) == NULL) {
			warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
			continue;
		}
		if ((name = elf_strptr(e, shstrndx, sh.sh_name)) == NULL)
			goto next;
		if (!strcmp(section, name)) {
			if (eh.e_type == ET_EXEC || eh.e_type == ET_DYN) {
				/*
				 * For executables, section base is the virtual
				 * address of the specified section.
				 */
				section_base = sh.sh_addr;
			} else if (eh.e_type == ET_REL) {
				/*
				 * For relocatables, section base is the
				 * relative offset of the specified section
				 * to the start of the first section.
				 */
				section_base = off;
			} else
				warnx("unknown e_type %u", eh.e_type);
			return;
		}
	next:
		off += sh.sh_size;
	}
	elferr = elf_errno();
	if (elferr != 0)
		warnx("elf_nextscn failed: %s", elf_errmsg(elferr));

	errx(EXIT_FAILURE, "%s: cannot find section %s", exe, section);
}
开发者ID:jhofstee,项目名称:freebsd,代码行数:58,代码来源:addr2line.c

示例15: elf_getshdrstrndx

const char *get_scn_name(vita_elf_t *ve, Elf_Scn *scn)
{
	size_t shstrndx;
	GElf_Shdr shdr;

	elf_getshdrstrndx(ve->elf, &shstrndx);
	gelf_getshdr(scn, &shdr);
	return elf_strptr(ve->elf, shstrndx, shdr.sh_name);
}
开发者ID:nterry,项目名称:vita-toolchain,代码行数:9,代码来源:vita-elf-create.c


注:本文中的elf_strptr函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。