I'm trying to read an ELF file to acces to the one segment it has. So far I've read the header of the file and it shows that the offset of the segment section starts 52 bytes from the beggining of the file (header.e_phoff) so I've tried using fseek(fitxer, header.e_phoff, SEEK_SET ); but then when I read the segment and print it it shows that the offset of the header is 1024 and the pyshical addres is 32768 when they should be 0x000400 and 0x00008000 respectively.
FILE* fitxer = fopen(string,"rb");
if(fitxer)
{
fread(&header, 1, sizeof(header), fitxer);
fseek(fitxer, header.e_phoff, SEEK_SET );
fread(&segment, 1, sizeof(segment), fitxer);
}
fclose(fitxer);
printf("offset: ");
printf("\n%d",segment.p_offset);
printf("\n");
printf("pyshical address: ");
printf("%d",segment.p_paddr);
printf("\n");
What am I doing wrong?
$ arm-none-eabi-readelf -l HOLA.elf
Elf file type is EXEC (Executable file)
Entry point 0x8000
There are 1 program headers, starting at offset 52
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x000400 0x00008000 0x00008000 0x0019f 0x0019f R E 0x400
Section to Segment mapping:
Segment Sections...
00 .text .rodata
```
Everything is ok: 1024 in decimal notation is equal to 0x400 in hexadecimal notation, and 0x8000 is equal to 32768.
Anyway, if you need to have results in hexadecimal notation, use %x
format instead of %d
:
printf("\n%x",segment.p_offset);
printf("\n");
printf("pyshical address: ");
printf("%x",segment.p_paddr);