1

I want to know to the .text start and size of my c++ application. I have been reading related topics about this (link) but I am not able to do what I want.

Analyzing readelf output of my sample program I got this:

Section Headers:
[Nr] Name              Type             Address           Offset
Size              EntSize          Flags  Link  Info  Align

[14] .text             PROGBITS         0000000000400830  00000830
0000000000000252  0000000000000000  AX       0     0     16

So I understand that .text section of my program start at 0x400830 address.

But I cant access to this address from my program:

printf("My process ID : %d\n", getpid());
printf("Executable Start address: 0x%lx\n", (unsigned long)&__executable_start);
printf("Text Start Address: 0x%lx\n", (unsigned long)&__etext);

But the output is:

My process ID : 4029
Executable Start address: 0x400000
Text Start Address: 0x400a8d

As you can see the start address is not the same. How I can access the start address of the .text section. I need to know the size or the end address... It this possible?

Xabi E
  • 251
  • 2
  • 3
  • 15

1 Answers1

0

I want to know to the .text start and size of my c++ application.

If you told us why you want to know that, you would likely have gotten a better answer.

Note that an executable doesn't need to have a .text section at all: section table may be stripped, and the executable will still run just fine. For such a binary, your question is not answerable.

To know the exact offset and size of .text for an ELF binary, one simply has to read the section table (just as readelf does). It's not hard.

Without doing that, you can get quite close by using heuristics / approximations.

You've already found __executable_start and __etext. A tighter bound could be obtained by using &__start -- usually that is the very first symbol in the .text section.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362