1

I am a bit lost in the following piece of code. What does it mean? Define the the Elf pointer start position?

#define ELFHDR ((struct Elf *) 0x10000)


After reading some answers below, I realize my question is not clear enough. I am not sure what this code mean?

((struct Elf *) 0x10000)

The Macro is used in the function:

readseg((uint32_t) ELFHDR, SECTSIZE*8, 0);

Joshua
  • 689
  • 7
  • 16

2 Answers2

1

It is defining a macro ELFHDR to be a pointer to address 0x10000. Clearly the author of the code knows the ELF header is there, but that's not what sets it. It is still set in the linker scripts.

Since this is set right (and typically it is right...), accessing ELFHDR results in reading the executable's own ELF header.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • 1
    nit: it is not defining `ELFHDR` to be a pointer. It is defining `ELFHDR` to be a macro which expands to the literal text `((struct Elf *) 0x10000)`. – William Pursell Apr 21 '22 at 15:13
  • @WilliamPursell: And the literal text is a pointer. I prefer for OP to understand. – Joshua Apr 21 '22 at 15:15
  • Does `((struct Elf *) 0x10000)` mean that for any Elf instance, it will start with virtual address `0x10000`? – Joshua Apr 21 '22 at 15:18
  • @Joshua: No, it means the programmer knows that this particular one does. I have constructed ELF binaries with the header at `0x1000`. – Joshua Apr 21 '22 at 15:22
0

After I read this thread, I realize there are two ways to define a pointer:

int *a

or

int* a

This article explain it very well.

So the macro means that it defines a pointer with the ELF struct type which starts its virtual memory address at 0x10000.

Joshua
  • 689
  • 7
  • 16