binman: Handle writing ELF symbols in the Entry class

This feature is used by several etypes and we plan to add more that use
it. Make symbol writing a feature of the base class to reduce the code
duplication.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/tools/binman/entry.py b/tools/binman/entry.py
index 63ec5ce..bdf53dd 100644
--- a/tools/binman/entry.py
+++ b/tools/binman/entry.py
@@ -12,6 +12,7 @@
 import time
 
 from binman import bintool
+from binman import elf
 from dtoc import fdt_util
 from patman import tools
 from patman.tools import to_hex, to_hex_size
@@ -86,10 +87,15 @@
         fake_fname: Fake filename, if one was created, else None
         required_props (dict of str): Properties which must be present. This can
             be added to by subclasses
+        elf_fname (str): Filename of the ELF file, if this entry holds an ELF
+            file, or is a binary file produced from an ELF file
+        auto_write_symbols (bool): True to write ELF symbols into this entry's
+            contents
     """
     fake_dir = None
 
-    def __init__(self, section, etype, node, name_prefix=''):
+    def __init__(self, section, etype, node, name_prefix='',
+                 auto_write_symbols=False):
         # Put this here to allow entry-docs and help to work without libfdt
         global state
         from binman import state
@@ -125,6 +131,8 @@
         self.fake_fname = None
         self.required_props = []
         self.comp_bintool = None
+        self.elf_fname = None
+        self.auto_write_symbols = auto_write_symbols
 
     @staticmethod
     def FindEntryClass(etype, expanded):
@@ -647,7 +655,8 @@
         Args:
           section: Section containing the entry
         """
-        pass
+        if self.auto_write_symbols:
+            elf.LookupAndWriteSymbols(self.elf_fname, self, section.GetImage())
 
     def CheckEntries(self):
         """Check that the entry offsets are correct
diff --git a/tools/binman/etype/blob.py b/tools/binman/etype/blob.py
index ceaefb0..a50a806 100644
--- a/tools/binman/etype/blob.py
+++ b/tools/binman/etype/blob.py
@@ -31,8 +31,9 @@
     the node (if enabled with -u) which provides the uncompressed size of the
     data.
     """
-    def __init__(self, section, etype, node):
-        super().__init__(section, etype, node)
+    def __init__(self, section, etype, node, auto_write_symbols=False):
+        super().__init__(section, etype, node,
+                         auto_write_symbols=auto_write_symbols)
         self._filename = fdt_util.GetString(self._node, 'filename', self.etype)
 
     def ObtainContents(self, fake_size=0):
diff --git a/tools/binman/etype/u_boot_spl.py b/tools/binman/etype/u_boot_spl.py
index 6f79bf5..d1aa3b4 100644
--- a/tools/binman/etype/u_boot_spl.py
+++ b/tools/binman/etype/u_boot_spl.py
@@ -5,7 +5,6 @@
 # Entry-type module for spl/u-boot-spl.bin
 #
 
-from binman import elf
 from binman.entry import Entry
 from binman.etype.blob import Entry_blob
 
@@ -35,11 +34,9 @@
     unless --no-expanded is used or the node has a 'no-expanded' property.
     """
     def __init__(self, section, etype, node):
-        super().__init__(section, etype, node)
+        super().__init__(section, etype, node, auto_write_symbols=True)
         self.elf_fname = 'spl/u-boot-spl'
+        self.auto_write_symbols = True
 
     def GetDefaultFilename(self):
         return 'spl/u-boot-spl.bin'
-
-    def WriteSymbols(self, section):
-        elf.LookupAndWriteSymbols(self.elf_fname, self, section.GetImage())
diff --git a/tools/binman/etype/u_boot_spl_nodtb.py b/tools/binman/etype/u_boot_spl_nodtb.py
index 316b381..50a126d 100644
--- a/tools/binman/etype/u_boot_spl_nodtb.py
+++ b/tools/binman/etype/u_boot_spl_nodtb.py
@@ -5,7 +5,6 @@
 # Entry-type module for 'u-boot-spl-nodtb.bin'
 #
 
-from binman import elf
 from binman.entry import Entry
 from binman.etype.blob import Entry_blob
 
@@ -32,11 +31,8 @@
     binman uses that to look up symbols to write into the SPL binary.
     """
     def __init__(self, section, etype, node):
-        super().__init__(section, etype, node)
+        super().__init__(section, etype, node, auto_write_symbols=True)
         self.elf_fname = 'spl/u-boot-spl'
 
     def GetDefaultFilename(self):
         return 'spl/u-boot-spl-nodtb.bin'
-
-    def WriteSymbols(self, section):
-        elf.LookupAndWriteSymbols(self.elf_fname, self, section.GetImage())
diff --git a/tools/binman/etype/u_boot_tpl.py b/tools/binman/etype/u_boot_tpl.py
index 0c575df..1883a2b 100644
--- a/tools/binman/etype/u_boot_tpl.py
+++ b/tools/binman/etype/u_boot_tpl.py
@@ -5,7 +5,6 @@
 # Entry-type module for tpl/u-boot-tpl.bin
 #
 
-from binman import elf
 from binman.entry import Entry
 from binman.etype.blob import Entry_blob
 
@@ -35,11 +34,8 @@
     unless --no-expanded is used or the node has a 'no-expanded' property.
     """
     def __init__(self, section, etype, node):
-        super().__init__(section, etype, node)
+        super().__init__(section, etype, node, auto_write_symbols=True)
         self.elf_fname = 'tpl/u-boot-tpl'
 
     def GetDefaultFilename(self):
         return 'tpl/u-boot-tpl.bin'
-
-    def WriteSymbols(self, section):
-        elf.LookupAndWriteSymbols(self.elf_fname, self, section.GetImage())
diff --git a/tools/binman/etype/u_boot_tpl_nodtb.py b/tools/binman/etype/u_boot_tpl_nodtb.py
index 98f3853..7e08e58 100644
--- a/tools/binman/etype/u_boot_tpl_nodtb.py
+++ b/tools/binman/etype/u_boot_tpl_nodtb.py
@@ -5,7 +5,6 @@
 # Entry-type module for 'u-boot-tpl-nodtb.bin'
 #
 
-from binman import elf
 from binman.entry import Entry
 from binman.etype.blob import Entry_blob
 
@@ -32,11 +31,8 @@
     binman uses that to look up symbols to write into the TPL binary.
     """
     def __init__(self, section, etype, node):
-        super().__init__(section, etype, node)
+        super().__init__(section, etype, node, auto_write_symbols=True)
         self.elf_fname = 'tpl/u-boot-tpl'
 
     def GetDefaultFilename(self):
         return 'tpl/u-boot-tpl-nodtb.bin'
-
-    def WriteSymbols(self, section):
-        elf.LookupAndWriteSymbols(self.elf_fname, self, section.GetImage())
diff --git a/tools/binman/etype/u_boot_vpl.py b/tools/binman/etype/u_boot_vpl.py
index 9daaca4..62e5969 100644
--- a/tools/binman/etype/u_boot_vpl.py
+++ b/tools/binman/etype/u_boot_vpl.py
@@ -5,7 +5,6 @@
 # Entry-type module for vpl/u-boot-vpl.bin
 #
 
-from binman import elf
 from binman.entry import Entry
 from binman.etype.blob import Entry_blob
 
@@ -32,11 +31,8 @@
     binman uses that to look up symbols to write into the VPL binary.
     """
     def __init__(self, section, etype, node):
-        super().__init__(section, etype, node)
+        super().__init__(section, etype, node, auto_write_symbols=True)
         self.elf_fname = 'vpl/u-boot-vpl'
 
     def GetDefaultFilename(self):
         return 'vpl/u-boot-vpl.bin'
-
-    def WriteSymbols(self, section):
-        elf.LookupAndWriteSymbols(self.elf_fname, self, section.GetImage())
diff --git a/tools/binman/etype/u_boot_vpl_nodtb.py b/tools/binman/etype/u_boot_vpl_nodtb.py
index 25c966c..db3d8a9 100644
--- a/tools/binman/etype/u_boot_vpl_nodtb.py
+++ b/tools/binman/etype/u_boot_vpl_nodtb.py
@@ -5,7 +5,6 @@
 # Entry-type module for 'u-boot-vpl-nodtb.bin'
 #
 
-from binman import elf
 from binman.entry import Entry
 from binman.etype.blob import Entry_blob
 
@@ -32,11 +31,8 @@
     binman uses that to look up symbols to write into the VPL binary.
     """
     def __init__(self, section, etype, node):
-        super().__init__(section, etype, node)
+        super().__init__(section, etype, node, auto_write_symbols=True)
         self.elf_fname = 'vpl/u-boot-vpl'
 
     def GetDefaultFilename(self):
         return 'vpl/u-boot-vpl-nodtb.bin'
-
-    def WriteSymbols(self, section):
-        elf.LookupAndWriteSymbols(self.elf_fname, self, section.GetImage())