mem_usage.py: stop parsing file when _end_of_ram is reached

Since commit 5966660c02b3 ("core: move relocation to embedded data
region"), when ASLR is enabled, some sections are stored at the end of
tee.elf for use by gen_tee_bin.py which then discards them. As a
result, they are not present in the final binary and should not be
reported by the memory usage script.

By ignoring any section past the _end_of_ram symbol, we avoid reporting
those discarded sections as well as a hole before them.

 Before:

 $ make -s CFG_CORE_ASLR=y
 $ scripts/mem_usage.py out/arm-plat-vexpress/core/tee.elf
  RAM Usage        0E100000 - 0E301E04 size 00201E04 2056 KiB 514 pages
 .text            0E100000 - 0E12FA68 size 0002FA68 190 KiB
 *hole*           0E12FA68 - 0E130000 size 00000598   1 KiB
 .rodata          0E130000 - 0E140598 size 00010598  65 KiB
 .gnu.hash        0E140598 - 0E1405B0 size 00000018   0 KiB
 .got             0E1405B0 - 0E1406F8 size 00000148   0 KiB
 .ARM.exidx       0E1406F8 - 0E142B78 size 00002480   9 KiB
 .ARM.extab       0E142B78 - 0E143274 size 000006FC   1 KiB
 *hole*           0E143274 - 0E144000 size 00000D8C   3 KiB
 .data            0E144000 - 0E145410 size 00001410   5 KiB
 .bss             0E145410 - 0E1509A8 size 0000B598  45 KiB
 .heap1           0E1509A8 - 0E164000 size 00013658  77 KiB
 .nozi            0E164000 - 0E176B80 size 00012B80  74 KiB
 *hole*           0E176B80 - 0E300000 size 00189480 1573 KiB
 .dynamic         0E300000 - 0E300098 size 00000098   0 KiB
 .hash            0E300098 - 0E3000B0 size 00000018   0 KiB
 .dynsym          0E3000B0 - 0E3000E0 size 00000030   0 KiB
 .dynstr          0E3000E0 - 0E3000E1 size 00000001   0 KiB
 *hole*           0E3000E1 - 0E3000E4 size 00000003   0 KiB
 .rel             0E3000E4 - 0E301E04 size 00001D20   7 KiB

 After:

 $ make -s CFG_CORE_ASLR=y
 $ scripts/mem_usage.py out/arm-plat-vexpress/core/tee.elf
 RAM Usage        0E100000 - 0E176B80 size 00076B80 475 KiB 119 pages
 .text            0E100000 - 0E12FA68 size 0002FA68 190 KiB
 *hole*           0E12FA68 - 0E130000 size 00000598   1 KiB
 .rodata          0E130000 - 0E140598 size 00010598  65 KiB
 .gnu.hash        0E140598 - 0E1405B0 size 00000018   0 KiB
 .got             0E1405B0 - 0E1406F8 size 00000148   0 KiB
 .ARM.exidx       0E1406F8 - 0E142B78 size 00002480   9 KiB
 .ARM.extab       0E142B78 - 0E143274 size 000006FC   1 KiB
 *hole*           0E143274 - 0E144000 size 00000D8C   3 KiB
 .data            0E144000 - 0E145410 size 00001410   5 KiB
 .bss             0E145410 - 0E1509A8 size 0000B598  45 KiB
 .heap1           0E1509A8 - 0E164000 size 00013658  77 KiB
 .nozi            0E164000 - 0E176B80 size 00012B80  74 KiB

Signed-off-by: Jerome Forissier <jerome@forissier.org>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/scripts/mem_usage.py b/scripts/mem_usage.py
index 2612545..fbf96a4 100755
--- a/scripts/mem_usage.py
+++ b/scripts/mem_usage.py
@@ -88,6 +88,16 @@
     args = get_args()
     env = os.environ.copy()
     env['LC_ALL'] = 'C'
+    readelf = subprocess.Popen(str.split(readelf_cmd()) + ['-s',
+                                                           args.tee_elf],
+                               stdout=subprocess.PIPE, env=env,
+                               universal_newlines=True)
+    for line in iter(readelf.stdout.readline, ''):
+        words = line.split()
+        if len(words) == 8 and words[7] == '_end_of_ram':
+            end_of_ram = int(words[1], 16)
+            break
+    readelf.terminate()
     readelf = subprocess.Popen(str.split(readelf_cmd()) + ['-S', '-W',
                                                            args.tee_elf],
                                stdout=subprocess.PIPE, env=env,
@@ -112,12 +122,16 @@
                     flags == 'AL'):
                 sects.append({'name': name, 'addr': addr,
                               'offs': offs, 'size': size})
+    first_addr = None
     for sect in sects:
         if sect['addr'] != 0:
-            first_addr = sect['addr']
-            break
-    last_addr = sects[-1]['addr']
-    last_size = sects[-1]['size']
+            addr = sect['addr']
+            if not first_addr:
+                first_addr = addr
+            if int(addr, 16) >= end_of_ram:
+                break
+            last_addr = addr
+            last_size = sect['size']
 
     ram_usage = int(last_addr, 16) + int(last_size, 16) - int(first_addr, 16)
     print_sect('RAM Usage', int(first_addr, 16), ram_usage, True, True)
@@ -129,6 +143,8 @@
         addr = int(sect['addr'], 16)
         size = int(sect['size'], 16)
 
+        if addr >= end_of_ram:
+            break
         if last_addr != 0 and addr != last_addr + last_size:
             print_sect('*hole*', last_addr + last_size,
                        addr - (last_addr + last_size))