]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Add new ld option: --print-memory-usage
authorTristan Gingold <gingold@adacore.com>
Fri, 5 Jun 2015 13:09:31 +0000 (15:09 +0200)
committerTristan Gingold <gingold@adacore.com>
Fri, 12 Jun 2015 09:24:36 +0000 (11:24 +0200)
ld/
* NEWS: Mention new option.
* lexsup.c (parse_args): Handle --print-memory-usage.
(ld_options): Add --print-memory-usage.
* ldmain.c (main): Call lang_print_memory_usage.
* ldlex.h (enum option_values): Add OPTION_PRINT_MEMORY_USAGE.
* ldlang.h: Add prototype of lang_print_memory_usage.
* ldlang.c (lang_print_memory_size, lang_print_memory_usage): New
functions.
* ld.texinfo (Options): Document --print-memory-usage.
* ld.h (args_type): Add print_memory_usage field.

ld/testsuite/
* ld-scripts/print-memory-usage-1.t,
* ld-scripts/print-memory-usage-1.s,
* ld-scripts/print-memory-usage-1.l,
* ld-scripts/print-memory-usage-2.t,
* ld-scripts/print-memory-usage-2.l,
* ld-scripts/print-memory-usage-3.s,
* ld-scripts/print-memory-usage-3.t,
* ld-scripts/print-memory-usage-3.l,
* ld-scripts/print-memory-usage.t: New tests.
* ld-scripts/print-memory-usage.exp: Run them.

20 files changed:
ld/ChangeLog
ld/NEWS
ld/ld.h
ld/ld.texinfo
ld/ldlang.c
ld/ldlang.h
ld/ldlex.h
ld/ldmain.c
ld/lexsup.c
ld/testsuite/ChangeLog
ld/testsuite/ld-scripts/print-memory-usage-1.l [new file with mode: 0644]
ld/testsuite/ld-scripts/print-memory-usage-1.s [new file with mode: 0644]
ld/testsuite/ld-scripts/print-memory-usage-1.t [new file with mode: 0644]
ld/testsuite/ld-scripts/print-memory-usage-2.l [new file with mode: 0644]
ld/testsuite/ld-scripts/print-memory-usage-2.t [new file with mode: 0644]
ld/testsuite/ld-scripts/print-memory-usage-3.l [new file with mode: 0644]
ld/testsuite/ld-scripts/print-memory-usage-3.s [new file with mode: 0644]
ld/testsuite/ld-scripts/print-memory-usage-3.t [new file with mode: 0644]
ld/testsuite/ld-scripts/print-memory-usage.exp [new file with mode: 0644]
ld/testsuite/ld-scripts/print-memory-usage.t [new file with mode: 0644]

index 7fb7413c1357509747f78a45a55d2ba518565ab9..a12e5c9106394bf0ad896a984f63a3377f3b1108 100644 (file)
@@ -1,3 +1,16 @@
+2015-06-12  Tristan Gingold  <gingold@adacore.com>
+
+       * NEWS: Mention new option.
+       * lexsup.c (parse_args): Handle --print-memory-usage.
+       (ld_options): Add --print-memory-usage.
+       * ldmain.c (main): Call lang_print_memory_usage.
+       * ldlex.h (enum option_values): Add OPTION_PRINT_MEMORY_USAGE.
+       * ldlang.h: Add prototype of lang_print_memory_usage.
+       * ldlang.c (lang_print_memory_size, lang_print_memory_usage): New
+       functions.
+       * ld.texinfo (Options): Document --print-memory-usage.
+       * ld.h (args_type): Add print_memory_usage field.
+
 2015-06-08  Nick Clifton  <nickc@redhat.com>
 
        * po/fr.po: Updated French Translation.
diff --git a/ld/NEWS b/ld/NEWS
index 4e84d5a5a6a2233f691abfe655036fdde7bcb7cc..140d3c95905030396d8da25174ec507cbb722220 100644 (file)
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -7,6 +7,8 @@
 
 * Add support for LLVM plugin.
 
+* Add --print-memory-usage option to report memory blocks usage.
+
 Changes in 2.25:
 
 * PE binaries now once again contain real timestamps by default.  To disable
diff --git a/ld/ld.h b/ld/ld.h
index f804f9c069699f4fd5140dab62ff844f89c48693..e6ebaae90f5f48c624e067f90f23b3fb1b6b9c13 100644 (file)
--- a/ld/ld.h
+++ b/ld/ld.h
@@ -161,6 +161,9 @@ typedef struct {
   /* If TRUE we'll just print the default output on stdout.  */
   bfd_boolean print_output_format;
 
+  /* If set, display the target memory usage (per memory region).  */
+  bfd_boolean print_memory_usage;
+
   /* Big or little endian as set on command line.  */
   enum endian_enum endian;
 
index 0c5f884b5f0f6b5ff1dbd03f88a85916e54fcea8..e12b64affb509cd4f9ea8840785b37e73860c950 100644 (file)
@@ -1472,6 +1472,21 @@ Print the name of the default output format (perhaps influenced by
 other command-line options).  This is the string that would appear
 in an @code{OUTPUT_FORMAT} linker script command (@pxref{File Commands}).
 
+@kindex --print-memory-usage
+@cindex memory usage
+@item --print-memory-usage
+Print used size, total size and used size of memory regions created with
+the @ref{MEMORY} command.  This is useful on embedded targets to have a
+quick view of amount of free memory.  The format of the output has one
+headline and one line per region.  It is both human readable and easily
+parsable by tools.  Here is an example of an output:
+
+@smallexample
+Memory region         Used Size  Region Size  %age Used
+             ROM:        256 KB         1 MB     25.00%
+             RAM:          32 B         2 GB      0.00%
+@end smallexample
+
 @cindex help
 @cindex usage
 @kindex --help
index c96c21fd4ee0dd2e513408d3049560478bf1a632..b816d698fc334551095a78a6d19adc595a3ea26f 100644 (file)
@@ -8133,3 +8133,41 @@ lang_ld_feature (char *str)
       p = q;
     }
 }
+
+/* Pretty print memory amount.  */
+
+static void
+lang_print_memory_size (bfd_vma sz)
+{
+  if ((sz & 0x3fffffff) == 0)
+    printf ("%10" BFD_VMA_FMT "u GB", sz >> 30);
+  else if ((sz & 0xfffff) == 0)
+    printf ("%10" BFD_VMA_FMT "u MB", sz >> 20);
+  else if ((sz & 0x3ff) == 0)
+    printf ("%10" BFD_VMA_FMT "u KB", sz >> 10);
+  else
+    printf (" %10" BFD_VMA_FMT "u B", sz);
+}
+
+/* Implement --print-memory-usage: disply per region memory usage.  */
+
+void
+lang_print_memory_usage (void)
+{
+  lang_memory_region_type *r;
+
+  printf ("Memory region         Used Size  Region Size  %%age Used\n");
+  for (r = lang_memory_region_list; r->next != NULL; r = r->next)
+    {
+      bfd_vma used_length = r->current - r->origin;
+      double percent;
+
+      printf ("%16s: ",r->name_list.name);
+      lang_print_memory_size (used_length);
+      lang_print_memory_size ((bfd_vma) r->length);
+
+      percent = used_length * 100.0 / r->length;
+
+      printf ("    %6.2f%%\n", percent);
+    }
+}
index 7cb7610e477a51330a64e89f4c0b4e2b8e58e29d..475cf71c21d827b1601adda59d5178fd974a9112 100644 (file)
@@ -688,4 +688,7 @@ ldlang_override_segment_assignment
 extern void
 lang_ld_feature (char *);
 
+extern void
+lang_print_memory_usage (void);
+
 #endif
index f174c28823b9f097e34b680ea6f3d8aaa85b15c1..7885de0282339c300420e2935c3b7e0ea413473b 100644 (file)
@@ -142,6 +142,7 @@ enum option_values
   OPTION_IGNORE_UNRESOLVED_SYMBOL,
   OPTION_PUSH_STATE,
   OPTION_POP_STATE,
+  OPTION_PRINT_MEMORY_USAGE,
 };
 
 /* The initial parser states.  */
index a7b72bd63f89f23a1107b64f8d1d9826a5251f33..04920343e1a99fff99fe1ee530b5cb0e99cc24f5 100644 (file)
@@ -440,6 +440,8 @@ main (int argc, char **argv)
     output_cref (config.map_file != NULL ? config.map_file : stdout);
   if (nocrossref_list != NULL)
     check_nocrossrefs ();
+  if (command_line.print_memory_usage)
+    lang_print_memory_usage ();
 #if 0
   {
     struct bfd_link_hash_entry * h;
index b618241f0d89bffc3d6cc235cd59215f6d9d03f6..b884d9a77f84224728955a806ffe9605eb86ac33 100644 (file)
@@ -523,6 +523,8 @@ static const struct ld_option ld_options[] =
   { {"pop-state", no_argument, NULL, OPTION_POP_STATE},
     '\0', NULL, N_("Pop state of flags governing input file handling"),
     TWO_DASHES },
+  { {"print-memory-usage", no_argument, NULL, OPTION_PRINT_MEMORY_USAGE},
+    '\0', NULL, N_("Report target memory usage"), TWO_DASHES },
 };
 
 #define OPTION_COUNT ARRAY_SIZE (ld_options)
@@ -1490,6 +1492,10 @@ parse_args (unsigned argc, char **argv)
              free (oldp);
            }
          break;
+
+       case OPTION_PRINT_MEMORY_USAGE:
+         command_line.print_memory_usage = TRUE;
+         break;
        }
     }
 
index ab94a7810450f6e93f74cdfbd663db8ae9f52bbd..de4adbb9093aecb66b533aaa7e2bc7b9e67d42a5 100644 (file)
@@ -1,3 +1,16 @@
+2015-06-12  Tristan Gingold  <gingold@adacore.com>
+
+       * ld-scripts/print-memory-usage-1.t,
+       * ld-scripts/print-memory-usage-1.s,
+       * ld-scripts/print-memory-usage-1.l,
+       * ld-scripts/print-memory-usage-2.t,
+       * ld-scripts/print-memory-usage-2.l,
+       * ld-scripts/print-memory-usage-3.s,
+       * ld-scripts/print-memory-usage-3.t,
+       * ld-scripts/print-memory-usage-3.l,
+       * ld-scripts/print-memory-usage.t: New tests.
+       * ld-scripts/print-memory-usage.exp: Run them.
+
 2015-06-05  Nick Clifton  <nickc@redhat.com>
 
        * ld-elf/compressed1d.d: Fix typo: iq200 -> iq2000.
diff --git a/ld/testsuite/ld-scripts/print-memory-usage-1.l b/ld/testsuite/ld-scripts/print-memory-usage-1.l
new file mode 100644 (file)
index 0000000..34bc895
--- /dev/null
@@ -0,0 +1,3 @@
+Memory region         Used Size  Region Size  %age Used
+             ROM:          32 B         1 KB      3.12%
+             RAM:          32 B       256 KB      0.01%
diff --git a/ld/testsuite/ld-scripts/print-memory-usage-1.s b/ld/testsuite/ld-scripts/print-memory-usage-1.s
new file mode 100644 (file)
index 0000000..ccb94bf
--- /dev/null
@@ -0,0 +1,8 @@
+       .text
+       .globl text_symbol
+text_symbol:
+       .space  32
+       .data
+       .globl data_symbol
+data_symbol:
+       .space  32
diff --git a/ld/testsuite/ld-scripts/print-memory-usage-1.t b/ld/testsuite/ld-scripts/print-memory-usage-1.t
new file mode 100644 (file)
index 0000000..97065b3
--- /dev/null
@@ -0,0 +1,5 @@
+MEMORY
+{
+  ROM (RX) : ORIGIN = 0x1000, LENGTH = 1K
+  RAM (W)  : ORIGIN = 0x100000, LENGTH = 256K
+}
diff --git a/ld/testsuite/ld-scripts/print-memory-usage-2.l b/ld/testsuite/ld-scripts/print-memory-usage-2.l
new file mode 100644 (file)
index 0000000..8797120
--- /dev/null
@@ -0,0 +1 @@
+Memory region         Used Size  Region Size  %age Used
diff --git a/ld/testsuite/ld-scripts/print-memory-usage-2.t b/ld/testsuite/ld-scripts/print-memory-usage-2.t
new file mode 100644 (file)
index 0000000..11ce1ba
--- /dev/null
@@ -0,0 +1,14 @@
+SECTIONS
+{
+  .text 0x1000 :
+  {
+    *(.text)
+    *(.pr)
+  }
+
+  .data :
+  {
+    *(.data)
+    *(.rw)
+  }
+}
diff --git a/ld/testsuite/ld-scripts/print-memory-usage-3.l b/ld/testsuite/ld-scripts/print-memory-usage-3.l
new file mode 100644 (file)
index 0000000..1044101
--- /dev/null
@@ -0,0 +1,3 @@
+Memory region         Used Size  Region Size  %age Used
+             ROM:        256 KB         1 MB     25.00%
+             RAM:          32 B         2 GB      0.00%
diff --git a/ld/testsuite/ld-scripts/print-memory-usage-3.s b/ld/testsuite/ld-scripts/print-memory-usage-3.s
new file mode 100644 (file)
index 0000000..917ff20
--- /dev/null
@@ -0,0 +1,8 @@
+       .text
+       .globl text_symbol
+text_symbol:
+       .space  256*1024
+       .data
+       .globl data_symbol
+data_symbol:
+       .space  32
diff --git a/ld/testsuite/ld-scripts/print-memory-usage-3.t b/ld/testsuite/ld-scripts/print-memory-usage-3.t
new file mode 100644 (file)
index 0000000..eff51b6
--- /dev/null
@@ -0,0 +1,5 @@
+MEMORY
+{
+  ROM (RX) : ORIGIN = 0x100000, LENGTH = 1M
+  RAM (W)  : ORIGIN = 0x1000000, LENGTH = 2048M
+}
diff --git a/ld/testsuite/ld-scripts/print-memory-usage.exp b/ld/testsuite/ld-scripts/print-memory-usage.exp
new file mode 100644 (file)
index 0000000..bbae75f
--- /dev/null
@@ -0,0 +1,69 @@
+# Test --print-memory-usage linker functionality
+# By Tristan Gingold, AdaCore
+#   Copyright (C) 2015 Free Software Foundation, Inc.
+#
+# This file is part of the GNU Binutils.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+# Mips adds MIPS.abiflags section.
+# Spu, pdp11 memory is too small.
+# Tic30 needs a special linker script.
+# Tic54x interpret space values in bits.
+# XCOFF has garbage collection
+if { [istarget mips*-*-*]
+     || [istarget spu*-*-*]
+     || [istarget pdp11*-*-*]
+     || [istarget tic30*-*-*]
+     || [istarget tic54x*-*-*]
+     || [istarget *-*-aix*]
+     || [istarget *-*-xcoff*] } {
+    return
+}
+
+run_ld_link_tests {
+    {
+       "print-memory-usage-1"
+       "-T print-memory-usage-1.t -T print-memory-usage.t --print-memory-usage"
+       ""
+       ""
+       { "print-memory-usage-1.s" }
+       { { ld "print-memory-usage-1.l" } }
+       "print-memory-usage-1"
+    }
+
+    {
+       "print-memory-usage-2"
+       "-T print-memory-usage-2.t --print-memory-usage"
+       ""
+       ""
+       { "print-memory-usage-1.s" }
+       { { ld "print-memory-usage-2.l" } }
+       "print-memory-usage-2"
+    }
+
+    {
+       "print-memory-usage-3"
+       "-T print-memory-usage-3.t -T print-memory-usage.t --print-memory-usage"
+       ""
+       ""
+       { "print-memory-usage-3.s" }
+       { { ld "print-memory-usage-3.l" } }
+       "print-memory-usage-3"
+    }
+
+}
+
diff --git a/ld/testsuite/ld-scripts/print-memory-usage.t b/ld/testsuite/ld-scripts/print-memory-usage.t
new file mode 100644 (file)
index 0000000..5ff057a
--- /dev/null
@@ -0,0 +1,14 @@
+SECTIONS
+{
+  .text :
+  {
+    *(.text)
+    *(.pr)
+  }
+
+  .data :
+  {
+    *(.data)
+    *(.rw)
+  }
+}