scripts/gen_offset_header/gen_offset_header.c
/**
 * @brief load the ELF header
 *
 * @param fd file descriptor of file from which to read
 * @returns 0 on success, -1 on failure
 */
@brief load the ELF header
@param fd file descriptor of file from which to read
@returns 0 on success, -1 on failure
@brief load the ELF header @param fd file descriptor of file from which to read @returns 0 on success - 1 on failure
static int ehdrLoad(int fd)
{
	unsigned  ix = 0x12345678;  
	size_t    nBytes;           
	if (lseek(fd, 0, SEEK_SET) == -1) {
		fprintf(stderr, "Unable to seek\n");
		return -1;
	}
	nBytes = read(fd, &ehdr, sizeof(ehdr));
	if (nBytes != sizeof(ehdr))
	{
		fprintf(stderr, "Failed to read ELF header\n");
		return -1;
	}
	if (strncmp((char *)ehdr.e_ident, ELFMAG, 4) != 0)
	{
		fprintf(stderr, "Input object module not ELF format\n");
		return -1;
	}
	if (ehdr.e_ident[EI_CLASS] != ELFCLASS32)
	{
		fprintf(stderr, "ELF64 class not supported\n");
		return -1;
	}
	if (((*(char*)&ix == 0x78) && (ehdr.e_ident[EI_DATA] == ELFDATA2MSB)) ||
			((*(char*)&ix == 0x12) && (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)))
	{
		swabRequired = 1;
		DBG_PRINT("Swab required\n");
	}
	swabElfHdr(&ehdr);   
	DBG_PRINT("Elf header Magic = %s\n", ehdr.e_ident);
	DBG_PRINT("Elf header e_type = %d\n", ehdr.e_type);
	DBG_PRINT("Elf header e_shstrndx = %d\n", ehdr.e_shstrndx);
	DBG_PRINT("Elf header e_shnum = %d\n", ehdr.e_shnum);
	return 0;
}
static int ehdrLoad ( int fd ) { unsigned ix = 0x12345678 ; size_t nBytes ; if ( lseek ( fd , 0 , SEEK_SET ) == -1 ) { fprintf ( stderr , " \n " ) ; return -1 ; } nBytes = read ( fd , & ehdr , sizeof ( ehdr ) ) ; if ( nBytes != sizeof ( ehdr ) ) { fprintf ( stderr , " \n " ) ; return -1 ; } if ( strncmp ( ( char * ) ehdr . e_ident , ELFMAG , 4 ) != 0 ) { fprintf ( stderr , " \n " ) ; return -1 ; } if ( ehdr . e_ident [ EI_CLASS ] != ELFCLASS32 ) { fprintf ( stderr , " \n " ) ; return -1 ; } if ( ( ( * ( char * ) & ix == 0x78 ) && ( ehdr . e_ident [ EI_DATA ] == ELFDATA2MSB ) ) || ( ( * ( char * ) & ix == 0x12 ) && ( ehdr . e_ident [ EI_DATA ] == ELFDATA2LSB ) ) ) { swabRequired = 1 ; DBG_PRINT ( " \n " ) ; } swabElfHdr ( & ehdr ) ; DBG_PRINT ( " \n " , ehdr . e_ident ) ; DBG_PRINT ( " \n " , ehdr . e_type ) ; DBG_PRINT ( " \n " , ehdr . e_shstrndx ) ; DBG_PRINT ( " \n " , ehdr . e_shnum ) ; return 0 ; }
@brief load the ELF header
 @param fd file descriptor of file from which to read
 @returns 0 on success, -1 on failure
@brief load the ELF header @param fd file descriptor of file from which to read @returns 0 on success - 1 on failure
/* used to auto-detect endian-ness */ /* number of bytes read from file */ /* perform some rudimentary ELF file validation */ /* 64-bit ELF module not supported (for now) */ /*
	 * Dynamically determine the endianess of the host (in the absence of
	 * a compile time macro ala _BYTE_ORDER).  The ELF structures will require
	 * byte swapping if the host and target have different byte ordering.
	 */ /* swap bytes (if required) */ /* debugging: dump some important ELF header fields */