0

I want to access the data segment of a c program. I found a working solution for Linux: Where are the symbols etext ,edata and end defined?

But the solution proposed for MacOS is not working for me. I have the following code:

#include <stdio.h>
#include <stdlib.h>
#include <mach-o/getsect.h>

int i;

int main(int argc, char *argv[])
{

    printf("%p\n", &i);
    printf("    program text (etext)      %p\n", (void*)get_etext());
    printf("    initialized data (edata)  %10p\n", (void*)get_edata());
    printf("    uninitialized data (end)  %p\n", (void*)get_end());

    exit(EXIT_SUCCESS);
}

Which outputs:

0x10c35b038
    program text (etext)      0x100000ee6
    initialized data (edata)         0x0
    uninitialized data (end)  0x100003000

The address of i is not between etext and end. And edata seems to be NULL.

Community
  • 1
  • 1
Lukas92
  • 363
  • 2
  • 11
  • OSX manual says `Use of these routines is very strongly discouraged.`, read it for more information. – Jean-Baptiste Yunès Feb 28 '17 at 12:09
  • Is there an alternative that can be used ? – Lukas92 Feb 28 '17 at 12:53
  • If you have the binary, you can run otool (the Mac equivalent of Linux objdump) on it and it will show all the sections with their virtual address ranges. – Ajay Brahmakshatriya Feb 28 '17 at 13:19
  • Thank's for your answer, but I need to have these information at runtime. – Lukas92 Feb 28 '17 at 14:12
  • This one may helps you http://stackoverflow.com/questions/25286221/how-to-find-text-segment-range-in-ios – Jean-Baptiste Yunès Feb 28 '17 at 14:17
  • Data segment will usually follow __TEXT but that does not need to be true for every executable. Do you need the beginning and end of the segment? You could get address of any global variable (using c & operator) which will be defined in the data segment. Then try to find relative address to what you need. Inspecting the binary with a disassembler like Hopper could give you an idea how it's organised inside. – Kamil.S Feb 28 '17 at 20:06

0 Answers0