parseelf: Add comment about getting number of symbols from DT_HASH

Also, make the format endianess-aware.

BUG=chromium:668568
TEST=sudo /mnt/host/source/chromite/bin/test_image --board cyan \
          chromiumos_base_image_R57-9019.0.0-rc1.bin

Change-Id: I86358b985a44eb7e04e2f27786a05abd20cf8c7e
Previous-Reviewed-on: https://chromium-review.googlesource.com/414848
(cherry picked from commit 5d61a1ffd31f058a71e93f83a3300711d064b637)
Reviewed-on: https://chromium-review.googlesource.com/414712
Reviewed-by: Nicolas Boichat <drinkcat@chromium.org>
Commit-Queue: Nicolas Boichat <drinkcat@chromium.org>
Tested-by: Nicolas Boichat <drinkcat@chromium.org>
Trybot-Ready: Nicolas Boichat <drinkcat@chromium.org>
diff --git a/lib/parseelf.py b/lib/parseelf.py
index 8180204..1d5afe4 100644
--- a/lib/parseelf.py
+++ b/lib/parseelf.py
@@ -66,8 +66,14 @@
     symtab_offset = next(elf.address_offsets(symtab_ptr))
 
     if dthash_ptr:
+      # DT_SYMTAB provides no information on the number of symbols table
+      # entries. Instead, we use DT_HASH's nchain value, which according to the
+      # spec, "should equal the number of symbol table entries".
+      # nchain is the second 32-bit integer at the address pointed by DT_HASH,
+      # both for ELF and ELF64 formats.
+      fmt = "<I" if elf.little_endian else ">I"
       elf.stream.seek(dthash_ptr + 4)
-      nsymbols = struct.unpack('i', elf.stream.read(4))[0]
+      nsymbols = struct.unpack(fmt, elf.stream.read(4))[0]
     else:
       # If DT_HASH is not defined, assume that symtab ends right before strtab.
       # This is the same assumption that glibc makes in dl-addr.c.