scripts/gen_tee_bin.py: convert symbol and section name to strings

Older versions of pyelftools return symbol and section names as byte
arrays rather than plain strings. As a result, symbols are not found:

 $ make
   ...
   GEN     out/arm/core/tee-header_v2.bin
 Cannot find symbol _start
 core/arch/arm/kernel/link.mk:191: recipe for target 'out/arm/core/tee-header_v2.bin' failed
 make[1]: *** [out/arm/core/tee-header_v2.bin] Error 1

(This error was observed when building on Ubuntu 16.04.)

Signed-off-by: Jerome Forissier <jerome@forissier.org>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/scripts/gen_tee_bin.py b/scripts/gen_tee_bin.py
index e4b4663..af7e948 100755
--- a/scripts/gen_tee_bin.py
+++ b/scripts/gen_tee_bin.py
@@ -62,6 +62,16 @@
     sys.exit(1)
 
 
+def get_name(obj):
+    # Symbol or section .name might be a byte array or a string, we want a
+    # string
+    try:
+        name = obj.name.decode()
+    except (UnicodeDecodeError, AttributeError):
+        name = obj.name
+    return name
+
+
 def get_symbol(elffile, name):
     global elffile_symbols
     global lsyms_def
@@ -72,15 +82,16 @@
                          if isinstance(s, SymbolTableSection)]
         for section in symbol_tables:
             for symbol in section.iter_symbols():
+                symbol_name = get_name(symbol)
                 if symbol['st_info']['bind'] == 'STB_GLOBAL':
-                    elffile_symbols[symbol.name] = symbol
+                    elffile_symbols[symbol_name] = symbol
                 elif symbol['st_info']['bind'] == 'STB_LOCAL':
-                    if symbol.name not in elffile_symbols.keys():
-                        elffile_symbols[symbol.name] = symbol
-                        if symbol.name not in lsyms_def.keys():
-                            lsyms_def[symbol.name] = 1
+                    if symbol_name not in elffile_symbols.keys():
+                        elffile_symbols[symbol_name] = symbol
+                        if symbol_name not in lsyms_def.keys():
+                            lsyms_def[symbol_name] = 1
                         else:
-                            lsyms_def[symbol.name] += 1
+                            lsyms_def[symbol_name] += 1
 
     if name in lsyms_def.keys() and lsyms_def[name] > 1:
         eprint("Multiple definitions of local symbol %s" % name)
@@ -97,9 +108,10 @@
     bin_data = bytearray()
 
     for section in elffile.iter_sections():
+        section_name = get_name(section)
         if (section['sh_type'] == 'SHT_NOBITS' or
                 not (section['sh_flags'] & SH_FLAGS.SHF_ALLOC) or
-                not dump_names.match(section.name)):
+                not dump_names.match(section_name)):
             continue
 
         if last_end == 0: