Read file in structure

I am trying to read the content of a file into a struct. The structure looks like this:

    typedef struct{
            unsigned char e_ident[EI_NIDENT] ;
            Elf32_Half e_type;
            Elf32_Half e_machine;
            Elf32_Word e_version;
            Elf32_Addr e_entry;
            Elf32_Off e_phoff;
            Elf32_Off e_shoff;
            Elf32_Word e_flags;
            Elf32_Half e_ehsize;
            Elf32_Half e_phentsize;
            Elf32_Half e_phnum;
            Elf32_Half e_shentsize;
            Elf32_Half e_shnum;
            Elf32_Half e_shstrndx;
    } Elf32_Ehdr;
extern Elf32_Ehdr elfH;

      

This is basically an ELF header file. So, anyway, I want to load the content of the file into this structure.

The function looks like this.

Elf32_Ehdr elfH;
int load(char* fname){
        FILE* file = fopen(fname,"r");

        if(NULL == file) return 0;

        fread(&elfH, 1, 52, file);

        fclose(file);
        return 1;
}

      

It doesn't seem to be working properly. ElfH's content is not as expected. What could be the problem? If i

+3


source to share


5 answers


You need to add "b"

to the file access mode string ( "rb"

) fopen

to read the binary data. The size of a hardcoded elf may also not be as good an idea as it might be if the size of your elf header structure doesn't quite fit 62. sizeof(Elf32_Ehdr)

is probably the best way to do this ...



0


source


This is the code I used to read the header from the ELF executable.



FILE* fp = fopen(fname, "rb");
if(fp == NULL)
{
    printf("failed to load\n");
    exit(1);
}

Elf32_Ehdr hdr;
if (1 != fread(&hdr, sizeof(hdr), 1, fp))
{
    printf("failed to read elf header\n");
    exit(1);
}
// If program doesn't exit, header was read and can be worked with down here.

      

0


source


Perhaps your problem is that the data in the file is not written in the same endianness that your program expected from you.

You can check if this is the case if you compare the expected and actual values ​​in hexadecimal format.
For example. if you expect 0x12345678 but actually get 0x78563412, this is definitely a content question.

See the answer to this problem for a solution .

0


source


C gives fairly limited guarantees about how content is struct

packed and aligned in memory. It probably depends on both the compiler and the platform if your struct members Elf32_Ehdr

are actually contiguous. Since they have different sizes, I doubt it (if you print sizeof(Elf32_Ehdr)

and compare it to the actual size on the title disc, you can see the differences).

If you know that the header you are reading is 52 bytes long (as your code suggests) then you need fread

so much and then do something like (in outline)

#define HDR_SIZE 52
typedef unsigned char byte;
Elf32_Ehdr hdr;

byte buf[HDR_SIZE];

fread(buf, HDR_SIZE, 1, fp);

hdr.e_version = *(Elf32_Word*)&buf[1];
...

      

(I am assuming it Elf32_Half

is a nibble, so it e_version

is in bytes 1 and 2 (0-offset) of the buffer).

I haven't tested this, but I hope you get the idea.

Then there are bytex questions you might be concerned about ...

0


source


As dragosht pointed out, you might need to open the binary of the file if you are on some Windows machine.

There is also the possibility that you will run into alignment issues. Structures can have additional fields added to help maintain correct address alignment for performance reasons.

If you are reading a file that was written from the same structure in binary mode, I would not expect to see this issue.

0


source







All Articles