本文整理汇总了C++中elf_kind函数的典型用法代码示例。如果您正苦于以下问题:C++ elf_kind函数的具体用法?C++ elf_kind怎么用?C++ elf_kind使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了elf_kind函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: binary_diff
int binary_diff(int fd1, int fd2, char *sections, int verbose) {
Elf *elf1, *elf2;
Elf_Kind kind1, kind2;
if (elf_version(EV_CURRENT) == EV_NONE) {
if (verbose) fprintf(stderr, "elfdiff: ELF Version mismatch\n");
return ED_ELFFAIL;
}
if ((elf1 = elf_begin(fd1, ELF_C_READ, NULL)) != NULL) {
kind1 = elf_kind(elf1);
elf_end(elf1);
} else {
if (verbose) fprintf(stderr, "elfdiff: Error reading ELF information from first input file.\n");
return ED_NOELF1;
}
if ((elf2 = elf_begin(fd2, ELF_C_READ, NULL)) != NULL) {
kind2 = elf_kind(elf2);
elf_end(elf2);
} else {
if (verbose) fprintf(stderr, "elfdiff: Error reading ELF information from second input file.\n");
return ED_NOELF2;
}
if ((kind1 == ELF_K_ELF) && (kind2 == ELF_K_ELF)){
return elf_diff (fd1, fd2, sections, verbose);
} else if ((kind1 == ELF_K_AR) && (kind2 == ELF_K_AR)) {
return ar_diff (fd1, fd2, sections, verbose);
} else {
if (verbose) fprintf(stderr, "elfdiff: The specified files are not of matching/supported types.\n");
return ED_ELFFAIL;
}
}
示例2: read_archive
static int
read_archive(int fd, Elf *elf, char *file, char *label, read_cb_f *func,
void *arg, int require_ctf)
{
Elf *melf;
Elf_Cmd cmd = ELF_C_READ;
Elf_Arhdr *arh;
int secnum = 1, found = 0;
while ((melf = elf_begin(fd, cmd, elf)) != NULL) {
int rc = 0;
if ((arh = elf_getarhdr(melf)) == NULL) {
elfterminate(file, "Can't get archive header for "
"member %d", secnum);
}
/* skip special sections - their names begin with "/" */
if (*arh->ar_name != '/') {
size_t memlen = strlen(file) + 1 +
strlen(arh->ar_name) + 1 + 1;
char *memname = xmalloc(memlen);
snprintf(memname, memlen, "%s(%s)", file, arh->ar_name);
switch (elf_kind(melf)) {
case ELF_K_AR:
rc = read_archive(fd, melf, memname, label,
func, arg, require_ctf);
break;
case ELF_K_ELF:
rc = read_file(melf, memname, label,
func, arg, require_ctf);
break;
default:
terminate("%s: Unknown elf kind %d\n",
memname, elf_kind(melf));
}
free(memname);
}
cmd = elf_next(melf);
(void) elf_end(melf);
secnum++;
if (rc < 0)
return (rc);
else
found += rc;
}
return (found);
}
示例3: process_elf
static void
process_elf(Elf *elf, char *file, int fd, int member)
{
Elf_Cmd cmd;
Elf *_elf;
switch (elf_kind(elf)) {
case ELF_K_ELF:
/*
* This is an ELF file, now attempt to find it's
* .comment section and to display it.
*/
print_symtab(elf, file);
break;
case ELF_K_AR:
/*
* Archives contain multiple ELF files, which can each
* in turn be examined with libelf.
*
* The below loop will iterate over each member of the
* archive and recursively call process_elf().
*/
cmd = ELF_C_READ;
while ((_elf = elf_begin(fd, cmd, elf)) != NULL) {
Elf_Arhdr *arhdr;
char buffer[1024];
arhdr = elf_getarhdr(_elf);
/*
* Build up file names based off of
* 'archivename(membername)'.
*/
(void) snprintf(buffer, 1024, "%s(%s)",
file, arhdr->ar_name);
/*
* Recursively process the ELF members.
*/
process_elf(_elf, buffer, fd, 1);
cmd = elf_next(_elf);
(void) elf_end(_elf);
}
break;
default:
if (!member)
(void) fprintf(stderr,
"%s: unexpected elf_kind(): 0x%x\n",
file, elf_kind(elf));
return;
}
}
示例4: open_elf
/* Open libelf FILE->fd and compute the load base of ELF as loaded in MOD.
When we return success, FILE->elf and FILE->bias are set up. */
static inline Dwfl_Error
open_elf (Dwfl_Module *mod, struct dwfl_file *file)
{
if (file->elf == NULL)
{
/* If there was a pre-primed file name left that the callback left
behind, try to open that file name. */
if (file->fd < 0 && file->name != NULL)
file->fd = TEMP_FAILURE_RETRY (open64 (file->name, O_RDONLY));
if (file->fd < 0)
return CBFAIL;
file->elf = elf_begin (file->fd, ELF_C_READ_MMAP_PRIVATE, NULL);
}
if (unlikely (elf_kind (file->elf) != ELF_K_ELF))
{
close (file->fd);
file->fd = -1;
return DWFL_E_BADELF;
}
GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (file->elf, &ehdr_mem);
if (ehdr == NULL)
{
elf_error:
close (file->fd);
file->fd = -1;
return DWFL_E (LIBELF, elf_errno ());
}
/* The addresses in an ET_EXEC file are absolute. The lowest p_vaddr of
the main file can differ from that of the debug file due to prelink.
But that doesn't not change addresses that symbols, debuginfo, or
sh_addr of any program sections refer to. */
file->bias = 0;
if (mod->e_type != ET_EXEC)
for (uint_fast16_t i = 0; i < ehdr->e_phnum; ++i)
{
GElf_Phdr ph_mem;
GElf_Phdr *ph = gelf_getphdr (file->elf, i, &ph_mem);
if (ph == NULL)
goto elf_error;
if (ph->p_type == PT_LOAD)
{
file->bias = ((mod->low_addr & -ph->p_align)
- (ph->p_vaddr & -ph->p_align));
break;
}
}
mod->e_type = ehdr->e_type;
/* Relocatable Linux kernels are ET_EXEC but act like ET_DYN. */
if (mod->e_type == ET_EXEC && file->bias != 0)
mod->e_type = ET_DYN;
return DWFL_E_NOERROR;
}
示例5: ELF_initial
int ELF_initial(char *input_file){
if(elf_version(EV_CURRENT) == EV_NONE){
fprintf(stderr, "ELF library initialization failed: %s.\n",
elf_errmsg(-1));
exit(1);
}
if((fd = open(input_file, O_RDONLY, 0)) < 0){
fprintf(stderr, "Open \"%s\" failed.\n", input_file);
exit(1);
}
if((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL){
fprintf(stderr, "elf_begin() failed: %s.\n", elf_errmsg(-1));
exit(1);
}
if(elf_kind(e) != ELF_K_ELF){
fprintf(stderr, "\"%s\" is not an ELF object.\n", input_file);
exit(1);
}
if(gelf_getehdr(e, &ehdr) == NULL){
fprintf(stderr, "getehdr() failed: %s.\n",
elf_errmsg(-1));
exit(1);
}
if(ehdr.e_flags != 2){
fprintf(stderr, "\"%s\" is not an execution file.\n", input_file);
exit(1);
}
phdr_index = 0;
// printf("Entry point: 0x%jx\n", ehdr.e_entry);
// printf("Flags: %d\n", ehdr.e_flags);
return 0;
}
示例6: 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 "";
}
示例7: get_elf
static Elf *
get_elf (const gchar *file,
gint *fd)
{
Elf *elf;
if (elf_version (EV_CURRENT) == EV_NONE )
return NULL;
*fd = g_open (file, O_RDONLY, 0);
if (*fd < 0)
return NULL;
elf = elf_begin (*fd, ELF_C_READ, NULL);
if (elf == NULL)
{
g_close (*fd, NULL);
*fd = -1;
return NULL;
}
if (elf_kind (elf) != ELF_K_ELF)
{
g_close (*fd, NULL);
*fd = -1;
return NULL;
}
return elf;
}
示例8: open_elf_file
static inline Dwfl_Error
open_elf_file (Elf **elf, int *fd, char **name)
{
if (*elf == NULL)
{
/* CBFAIL uses errno if it's set, so clear it first in case we don't
set it with an open failure below. */
errno = 0;
/* If there was a pre-primed file name left that the callback left
behind, try to open that file name. */
if (*fd < 0 && *name != NULL)
*fd = TEMP_FAILURE_RETRY (open (*name, O_RDONLY));
if (*fd < 0)
return CBFAIL;
return __libdw_open_file (fd, elf, true, false);
}
else if (unlikely (elf_kind (*elf) != ELF_K_ELF))
{
elf_end (*elf);
*elf = NULL;
close (*fd);
*fd = -1;
return DWFL_E_BADELF;
}
/* Elf file already open and looks fine. */
return DWFL_E_NOERROR;
}
示例9: extract_functions_internal
/* Below code adapted from libelf tutorial example */
static u32 extract_functions_internal (const char *str, func_entry *func_list) {
Elf *e;
Elf_Kind ek;
Elf_Scn *scn;
Elf_Data *edata;
u32 fd, i, symbol_count;
GElf_Sym sym;
GElf_Shdr shdr;
u32 func_count = 0;
if(elf_version(EV_CURRENT) == EV_NONE) {
printf("Error initializing ELF: %s\n", elf_errmsg(-1));
return -1;
}
if ((fd = open(str, O_RDONLY, 0)) < 0) {
printf("Unable to open %s\n", str);
return -1;
}
if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
printf("elf_begin failed: %s\n", elf_errmsg(-1));
}
ek = elf_kind(e);
if(ek != ELF_K_ELF) {
printf("not an ELF object");
} else {
scn = NULL;
edata = NULL;
while((scn = elf_nextscn(e, scn)) != NULL) {
gelf_getshdr(scn, &shdr);
if(shdr.sh_type == SHT_SYMTAB) {
edata = elf_getdata(scn, edata);
symbol_count = shdr.sh_size / shdr.sh_entsize;
for(i = 0; i < symbol_count; i++) {
gelf_getsym(edata, i, &sym);
if(ELF32_ST_TYPE(sym.st_info) != STT_FUNC) {
check_for_end_marker(elf_strptr(e, shdr.sh_link, sym.st_name), sym.st_value);
continue;
}
if(sym.st_size == 0) continue;
func_list[func_count].offset = sym.st_value;
func_list[func_count++].func_name = strdup(elf_strptr(e, shdr.sh_link, sym.st_name));
if(func_count >= MAXFNS) {
printf("Func limit (%"PRId32") reached, please increase MAXFNS & rebuild\n", MAXFNS);
raise(SIGABRT);
}
// printf("%08x %08x\t%s\n", sym.st_value, sym.st_size, elf_strptr(e, shdr.sh_link, sym.st_name));
}
}
}
}
elf_end(e);
close(fd);
return func_count;
}
示例10: test_one
static void
test_one(const char *f, Elf *elf, int flags)
{
char *b = NULL;
GElf_Ehdr ehdr;
if (elf_kind(elf) != ELF_K_ELF) {
if (flags & P_OTHER)
goto out_print;
return;
}
if (gelf_getehdr(elf, &ehdr) == NULL)
return;
if ((flags & P_BUILDID)) {
size_t sz;
uint8_t *data = get_buildid(elf, &sz);
if (data) {
b = alloca(sz * 2 + 1);
data2hex(data, sz, b);
goto out_print;
}
return;
}
if ((flags & P_REL) && (ehdr.e_type == ET_REL))
goto out_print;
if ((flags & P_EXEC) && (ehdr.e_type == ET_EXEC))
goto out_print;
if ((flags & P_DEBUG) && has_debuginfo(elf))
goto out_print;
/* arguably should print if P_OTHER, but, nah. */
if (ehdr.e_type != ET_DYN)
return;
if (has_dt_debug(elf, &ehdr)) {
if (flags & P_EXEC) {
goto out_print; /* treat PIEs as executables */
}
} else if (flags & P_DSO) {
goto out_print;
}
return;
out_print:
write(1, f, strlen(f));
if (b) {
write(1, " ", 1);
write(1, b, strlen(b));
}
write(1, (flags & P_NEWLINE) ? "\n" : "", 1);
}
示例11: is_elf_file
gboolean
is_elf_file (const char *path,
gboolean *is_shared,
gboolean *is_stripped)
{
g_autofree char *filename = g_path_get_basename (path);
struct stat stbuf;
if (lstat (path, &stbuf) == -1)
return FALSE;
if (!S_ISREG (stbuf.st_mode))
return FALSE;
/* Self-extracting .zip files can be ELF-executables, but shouldn't be
treated like them - for example, stripping them breaks their
operation */
if (g_str_has_suffix (filename, ".zip"))
return FALSE;
if ((strstr (filename, ".so.") != NULL ||
g_str_has_suffix (filename, ".so")) ||
(stbuf.st_mode & 0111) != 0)
{
glnx_fd_close int fd = -1;
fd = open (path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
if (fd >= 0)
{
Elf *elf;
GElf_Ehdr ehdr;
gboolean res = FALSE;
if (elf_version (EV_CURRENT) == EV_NONE )
return FALSE;
elf = elf_begin (fd, ELF_C_READ, NULL);
if (elf == NULL)
return FALSE;
if (elf_kind (elf) == ELF_K_ELF &&
gelf_getehdr (elf, &ehdr))
{
if (is_shared)
*is_shared = ehdr.e_type == ET_DYN;
if (is_stripped)
*is_stripped = !elf_has_symtab (elf);
res = TRUE;
}
elf_end (elf);
return res;
}
}
return FALSE;
}
示例12: info
/**
* perform checks on the initial ELF binary and create an ELF descriptor
* to perform direct manipulations within the file.
*/
int
ElfInject::prepareElfBin()
{
info("prepare ELF binary: %s", bin.name.c_str());
/* open binary file */
bin.fd = open(bin.name.c_str(), O_RDWR, S_IRWXU);
if (bin.fd < 0) {
error("cannot open %s", bin.name.c_str());
return -1;
}
/* fstat binary */
if (fstat(bin.fd, &bin.stats)) {
error("cannot fstat %s", bin.name.c_str());
return -1;
}
/* initialize ELF library */
if(elf_version(EV_CURRENT) == EV_NONE ) {
error("ELF library initialization failed");
return -1;
}
/* create elf descriptor */
if ((bin.elf = elf_begin(bin.fd, ELF_C_RDWR_MMAP, NULL)) == NULL) {
error("cannot initialize elf");
return -1;
}
/* check, whether the file is an ELF-file */
if (elf_kind(bin.elf) != ELF_K_ELF) {
error("%s is not an ELF file", bin.name.c_str());
return -1;
}
debug("° correct ELF type");
if (gelf_getclass(bin.elf) != ELFCLASS32) {
error("%s is not a 32-bit binary", bin.name.c_str());
return -1;
}
debug("° compiled for 32-bit systems");
/* allocate space for binary */
// if ((bin.basePtr = (char*)malloc(bin.stats.st_size)) == NULL) {
// error("cannot allocate enough memory" << lend ;
// }
/* create elf descriptor for mem region */
// if ((bin.elfMem = elf_memory(bin.basePtr, bin.stats.st_size)) == NULL) {;
// error("cannot initialize elfMem");
// }
return 0;
}
示例13: print_ar_files
/*
* Process member files of an archive. This function provides
* a loop through an archive equivalent the processing of
* each_file for individual object files.
*/
static void
print_ar_files(int fd, Elf * elf_file, char *filename)
{
Elf_Arhdr *p_ar;
Elf *arf;
Elf_Cmd cmd;
Elf_Kind file_type;
cmd = ELF_C_READ;
archive_name = filename;
while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
p_ar = elf_getarhdr(arf);
if (p_ar == NULL) {
(void) fprintf(stderr, "%s: %s: %s\n",
prog_name, filename, elf_errmsg(-1));
return;
}
if (p_ar->ar_name[0] == '/') {
cmd = elf_next(arf);
(void) elf_end(arf);
continue;
}
if (!h_flag & !P_flag) {
if (p_flag)
(void) printf("\n\n%s[%s]:\n",
filename, p_ar->ar_name);
else {
if (A_flag != 0)
(void) printf("\n\n%s%s[%s]:\n",
A_header, filename, p_ar->ar_name);
else
(void) printf("\n\n%s[%s]:\n",
filename, p_ar->ar_name);
}
}
file_type = elf_kind(arf);
if (file_type == ELF_K_ELF) {
process(arf, p_ar->ar_name);
} else {
(void) fprintf(stderr, gettext(
"%s: %s: invalid file type\n"),
prog_name, p_ar->ar_name);
cmd = elf_next(arf);
(void) elf_end(arf);
errflag++;
continue;
}
cmd = elf_next(arf);
(void) elf_end(arf);
} /* end while */
}
示例14: init_libelf
static void init_libelf(int *fd, char const* bin)
{
if ((*fd = open(bin, O_RDONLY, 0)) == -1)
exit_error("open() fail");
if (elf_version(EV_CURRENT) == EV_NONE)
exit_error("elf_version() fail");
if ((e = elf_begin(*fd, ELF_C_READ, NULL)) == NULL)
exit_error("elf_begin() fail");
if (elf_kind(e) != ELF_K_ELF)
exit_error("file to execute is not an ELF file");
fd_bak = *fd;
}
示例15: what_kind
static Dwfl_Error
what_kind (int fd, Elf **elfp, Elf_Kind *kind, bool *close_fd)
{
Dwfl_Error error = DWFL_E_NOERROR;
*kind = elf_kind (*elfp);
if (unlikely (*kind == ELF_K_NONE))
{
if (unlikely (*elfp == NULL))
error = DWFL_E_LIBELF;
else
{
error = decompress (fd, elfp);
if (error == DWFL_E_NOERROR)
{
*close_fd = true;
*kind = elf_kind (*elfp);
}
}
}
return error;
}