Exemplo n.º 1
0
/* Examine a pre-existing object file and extract its ELF header.
 */
static void readreferenceheader(char const *filename)
{
    FILE *file;

    file = fopen(filename, "rb");
    if (!file)
	fail("%s: %s", filename, strerror(errno));
    if (!elfrw_read_Ehdr(file, &refehdr))
	fail("%s: %s", filename, strerror(errno));
    fclose(file);
    output64 = refehdr.e_ident[EI_CLASS] == ELFCLASS64;
}
Exemplo n.º 2
0
/* Read in the ELF header proper, and verify that its contents conform
 * to what the program can decipher.
 */
static int readelfhdr(void)
{
    if (elfrw_read_Ehdr(thefile, &elffhdr) != 1) {
	if (ferror(thefile))
	    return err(NULL);
	else
	    return err("%s: not an ELF file.", thefilename);
    }
    if (!checkelfident(elffhdr.e_ident))
	return FALSE;

    switch (elffhdr.e_type) {
      case ET_REL:
      case ET_EXEC:
      case ET_DYN:
      case ET_CORE:
	break;
      default:
	return err("%s: unknown ELF file type (type = %u).",
		   thefilename, elffhdr.e_type);
    }
    if (elffhdr.e_ehsize != sizeof(Elf32_Ehdr) &&
			elffhdr.e_ehsize != sizeof(Elf64_Ehdr))
	return err("%s: warning: unrecognized ELF header size: %d.",
		   thefilename, elffhdr.e_ehsize);
    if (elffhdr.e_version != EV_CURRENT)
	return err("%s: unrecognized ELF header version: %u.",
		   thefilename, (unsigned int)elffhdr.e_version);

    if (elffhdr.e_phoff != 0) {
	if (elffhdr.e_phentsize != sizeof(Elf32_Phdr) &&
			elffhdr.e_phentsize != sizeof(Elf64_Phdr))
	    err("%s: unrecognized program header entry size: %u.",
		thefilename, elffhdr.e_phentsize);
	else
	    proghdrs = 1;
    }
    if (elffhdr.e_shoff != 0) {
	if (elffhdr.e_shentsize != sizeof(Elf32_Shdr) &&
			elffhdr.e_shentsize != sizeof(Elf64_Shdr))
	    err("%s: unrecognized section header entry size: %u.",
		thefilename, elffhdr.e_shentsize);
	else
	    secthdrs = 1;
    }
    return TRUE;
}