Linux – /proc/pid/maps addresses (and converting them)

linuxmemoryproc

When looking at the memory maps in /proc/pid/maps, we can see different length addresses:

00400000-0042e000 r-xp 00000000 fb:01 268953                             /bin/nano
0062e000-0062f000 r--p 0002e000 fb:01 268953                             /bin/nano
0062f000-00630000 rw-p 0002f000 fb:01 268953                             /bin/nano
0081e000-00906000 rw-p 00000000 00:00 0                                  [heap]
7f8313e5c000-7f8314109000 rw-p 00000000 fb:01 2399989                    /usr/share/misc/magic.mgc
7f8314109000-7f83142ce000 r--p 00000000 fb:01 2759354                    /usr/lib64/locale/locale-archive
7f83142ce000-7f83142d1000 r-xp 00000000 fb:01 1457046                    /lib64/libdl-2.17.so
7f83142d1000-7f83144d0000 ---p 00003000 fb:01 1457046                    /lib64/libdl-2.17.so

We have addresses with 8 digit lengths like:

  • 00400000-0042e000

And ones with 12 digit lengths (the last 3 digits are always 0):

  • 7f8313e5c000-7f8314109000

Why are those addresses formatted this way, and can I convert them to 8 digit lengths?

Best Answer

First, you can not convert the addresses to have just 8 digits. Memory addresses can and will have much larger values than could be represented with just 8 digits.

The reason why memory addresses are represented in /proc/pid/maps as they are is on the line 283 in fs/proc/task_mmu.c (or task_nommu.c) in a recent kernel source tree:

283         seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
284                         start,
285                         end,
286                         flags & VM_READ ? 'r' : '-',
287                         flags & VM_WRITE ? 'w' : '-',
288                         flags & VM_EXEC ? 'x' : '-',
289                         flags & VM_MAYSHARE ? 's' : 'p',
290                         pgoff,
291                         MAJOR(dev), MINOR(dev), ino);

What this boils down to is that in any memory address which has a hex string representation shorter than 8 digits, will get padded with leading zeros. Any value larger than that will be represented as it is, not truncated to 8 digits. That's just the way how printk()'s printf-style formatting works.

Now what to make out of all this? Probably you should take a minute to think about why would you want to truncate memory addresses to 8 digits. What do you think is the benefit of doing so?

Related Question