0

When executing a simple C program, is it possible to know at what physical address (or range of addresses) different parts of my C program (e.g. data,text, stack,heap etc) are mapped by the operating system e.g. Linux. Is it possible to achieve this using some compiler flags with gcc etc.

Also is it possible to define a static mapping for my program (in user or kernel space) such that every time I execute the program the physical addresses assigned to the program does not change.

Any help is this regard is more than welcome.

Thank You

  • You are doing something wrong if you have to ask... – Eugene Sh. Aug 03 '17 at 16:37
  • The answers are "No", "no", and "no". And for anything sensible, you don't need it. Therefore this is most certainly an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) -- please describe what problem you **think** you would solve this way. –  Aug 03 '17 at 17:41
  • 2
    Comipiler flags won't help. To get a mapping, read the file `/proc/self/maps` [it is ascii]. But, these are only virtual addresses and _not_ physical. It is **not** possible to force a mapping to a _physical_ address. And, the physical address can change dynamically, moment to moment. So, even if you could _query_ the _physical_ address, it could change before you even had the chance to use it [without getting the kernel to lock it in]. Please describe _why_ you want to try to do this (i.e. what problem are you trying to solve) – Craig Estey Aug 03 '17 at 17:44
  • @FelixPalmen Thanks I know that is is difficult and may be impossible. But I am a research student and I have seen this type of work being done in the state-of-the-art on a Linux kernel. – Syed Aftab Rashid Aug 03 '17 at 18:58
  • 1
    @CraigEstey Thank you for your response. what I want to do is something termed as "task profiling". What you do is that you run a task and see what are the hot or mostly accessed memory regions used by the task. Now by using this information you can use something like " Cache locking" etc to prefetch and lock the the mostly used contents of a task. This way what we can see is an improvement in the execution times of programs. This is the theory behind what I want to achieve however as you already pointed out practically things are not as simple. – Syed Aftab Rashid Aug 03 '17 at 19:04
  • 2
    You can do this using only _virtual_ addresses. They are more meaningful for your purposes. Using `mmap` against a mapped file for data. Then, you might use `madvise` on that region. To _preload_ a cache line into the cache [_locking_ is not possible], x86 has some `mmx` instructions (e.g. `prefetchnta`), but, in my experience, they produce poorer performance than the cpu's cache fetch/flush logic. x86 has HW performance counters you can query [with some kernel assist] that detail cache hit/miss, etc (e.g. x86 `pebs`). Set up _measurement_ before you try to tune. – Craig Estey Aug 03 '17 at 19:33
  • Every time you load your program the virtual addresses will be the same if you disable ASLR. The physical addresses will always be different, – stark Aug 03 '17 at 20:50

0 Answers0