コード例 #1
0
ファイル: elf.c プロジェクト: Anmol2307/PranaliOS
struct elf_file_t *elf_open(char *path)
{
	struct elf_file_t *elf;

	/* Create structure */
	elf = calloc(1, sizeof(struct elf_file_t));
	if (!elf)
		fatal("elf_open: out of memory\n");
///	printf("\n length = %d\n",strlen(path));
///	printf("\n tryng to open %s\n",path);

	/* Open file */
	elf->f = fopen(path, "rb");
///	printf("\n error no is %d",errno);
	if (!elf->f)
		fatal("%s: cannot open executable file", path);
	if (strlen(path) >= sizeof(elf->path))
		fatal("%s: executable file path too long", path);
	strcpy(elf->path, path);

	/* Get file size */
	fseek(elf->f, 0, SEEK_END);
	elf->size = ftell(elf->f);
	fseek(elf->f, 0, SEEK_SET);

	/* Read header, section headers, and program headers. */
	elf_debug("\n%s: reading ELF file\n", path);
	elf_read_ehdr(elf);
	elf_read_shdr(elf);
	elf_read_phdr(elf);
	
	/* Return elf file structure */
	return elf;
}
コード例 #2
0
ファイル: elf_util.c プロジェクト: linux-next/linux-next
/**
 * elf_read_from_buffer - read ELF file and sets up ELF header and ELF info
 * @buf:	Buffer to read ELF file from.
 * @len:	Size of @buf.
 * @ehdr:	Pointer to existing struct which will be populated.
 * @elf_info:	Pointer to existing struct which will be populated.
 *
 * This function allows reading ELF files with different byte order than
 * the kernel, byte-swapping the fields as needed.
 *
 * Return:
 * On success returns 0, and the caller should call elf_free_info(elf_info) to
 * free the memory allocated for the section and program headers.
 */
int elf_read_from_buffer(const char *buf, size_t len, struct elfhdr *ehdr,
			 struct elf_info *elf_info)
{
	int ret;

	ret = elf_read_ehdr(buf, len, ehdr);
	if (ret)
		return ret;

	elf_info->buffer = buf;
	elf_info->ehdr = ehdr;
	if (ehdr->e_phoff > 0 && ehdr->e_phnum > 0) {
		ret = elf_read_phdrs(buf, len, elf_info);
		if (ret)
			return ret;
	}
	if (ehdr->e_shoff > 0 && ehdr->e_shnum > 0) {
		ret = elf_read_shdrs(buf, len, elf_info);
		if (ret) {
			kfree(elf_info->proghdrs);
			return ret;
		}
	}

	return 0;
}
コード例 #3
0
ファイル: ELFManager.cpp プロジェクト: fast01/baiduhook
int ELFManager::elf_open(const char *filename)
{
	//只读模式打开
	_fd = open(filename, O_RDONLY);
	if (_fd < 0) {
		perror("open fail!");
		return -1;
	}
	_filename = filename;

	int ret = elf_read(0, _ident, sizeof(_ident));
	if (ret < 0) {
		fprintf(stderr, "read e_ident error!\n");
		return -1;
	}
	_is_32_bit = (_ident[EI_CLASS] != ELFCLASS64);

	ret = elf_read_ehdr(_ehdr);
	if(ret != 0) {
		return -1;
	}
	return _fd;
}
コード例 #4
0
int main(int argc, char *argv[])
{
	if (argc < 2) {
		usage();
		goto exit_failure;
	}

	FILE *fp = fopen(argv[1], "rb");

	Elf32_Ehdr ehdr;
	if (!elf_read_ehdr(fp, &ehdr)) {
		fprintf(stderr, "ERROR: loading ELF\n");
		goto exit_close;
	}

	Elf32_Phdr *phdr;
	if (!elf_load_phdr(fp, &ehdr, &phdr)) {
		fprintf(stderr, "ERROR: loading ELF Program Header\n");
		goto exit_close;
	}

	Elf32_Shdr *shdr;
	if (!elf_load_shdr(fp, &ehdr, &shdr)) {
		fprintf(stderr, "ERROR: loading ELF Section Header\n");
		goto exit_free_phdr;
	}


	// Load entire String Table Section
	char *shstrtab = elf_load_shstrtab(fp, &ehdr, shdr);
	int fstubs_found = 0;
	int vstubs_found = 0;
	int fstubs_idx = 0, vstubs_idx = 0;
	int i;
	for (i = 0; i < ehdr.e_shnum && (!fstubs_found || !vstubs_found); i++) {
		const char *shstr = shstrtab + shdr[i].sh_name;
		if (!fstubs_found) {
			if (strcmp(FSTUBS_SECTION, shstr) == 0) {
				fstubs_found = 1;
				fstubs_idx = i;
			}
		}
		if (!vstubs_found) {
			if (strcmp(VSTUBS_SECTION, shstr) == 0) {
				vstubs_found = 1;
				vstubs_idx = i;
			}
		}
	}

	if (!fstubs_found && !vstubs_found) {
		fprintf(stderr, "ERROR: the requiered sections "
			FSTUBS_SECTION " and " VSTUBS_SECTION
			" were not found.\n");
		goto exit_free_shstrtab;
	} else if (!fstubs_found) {
		printf("WARNING: section: " FSTUBS_SECTION
			" was not found\n");
	} else if (!vstubs_found) {
		printf("WARNING: section: " VSTUBS_SECTION
			" was not found\n");
	}

	unsigned char *fbuf = malloc(shdr[fstubs_idx].sh_size);
	unsigned char *vbuf = malloc(shdr[vstubs_idx].sh_size);

	fseek(fp, shdr[fstubs_idx].sh_offset, SEEK_SET);
	fread(fbuf, 1, shdr[fstubs_idx].sh_size, fp);

	fseek(fp, shdr[vstubs_idx].sh_offset, SEEK_SET);
	fread(vbuf, 1, shdr[vstubs_idx].sh_size, fp);

	printf("fstubs size: 0x%08X\n", shdr[fstubs_idx].sh_size);
	printf("vstubs size: 0x%08X\n", shdr[vstubs_idx].sh_size);

	for (i = 0; i < shdr[fstubs_idx].sh_size; i += 4*4) {
		printf("fstub: Lib NID: 0x%08X\n"
		       "       Mod NID: 0x%08X\n"
		       "       Fun NID: 0x%08X\n\n",
		       *(unsigned int *)(fbuf + i + 0),
		       *(unsigned int *)(fbuf + i + 4),
		       *(unsigned int *)(fbuf + i + 8));
	}
	printf("\n");
	for (i = 0; i < shdr[vstubs_idx].sh_size; i += 4*4) {
		printf("vstub: Lib NID: 0x%08X\n"
		       "       Mod NID: 0x%08X\n"
		       "       Var NID: 0x%08X\n\n",
		       *(unsigned int *)(vbuf + i + 0),
		       *(unsigned int *)(vbuf + i + 4),
		       *(unsigned int *)(vbuf + i + 8));
	}

	free(fbuf);
	free(vbuf);

	free(shstrtab);

	elf_free_shdr(&shdr);
	elf_free_phdr(&phdr);
	fclose(fp);
	return EXIT_SUCCESS;

exit_free_shstrtab:
	free(shstrtab);
exit_free_shdr:
	elf_free_shdr(&shdr);
exit_free_phdr:
	elf_free_phdr(&phdr);
exit_close:
	fclose(fp);
exit_failure:
	return EXIT_FAILURE;
}