diff --git a/arch/arm/src/Makefile b/arch/arm/src/Makefile index 4850080312..45cd42bac3 100644 --- a/arch/arm/src/Makefile +++ b/arch/arm/src/Makefile @@ -181,7 +181,7 @@ board$(DELIM)libboard$(LIBEXT): $(Q) $(MAKE) -C board libboard$(LIBEXT) EXTRAFLAGS="$(EXTRAFLAGS)" define LINK_ALLSYMS - $(Q) $(TOPDIR)/tools/mkallsyms.py $(NUTTX) allsyms.tmp + $(Q) $(TOPDIR)/tools/mkallsyms.py $(NUTTX) allsyms.tmp --orderbyname $(CONFIG_SYMTAB_ORDEREDBYNAME) $(Q) $(call COMPILE, allsyms.tmp, allsyms$(OBJEXT), -x c) $(Q) $(LD) --entry=__start $(LDFLAGS) $(LIBPATHS) $(EXTRA_LIBPATHS) \ -o $(NUTTX) $(HEAD_OBJ) allsyms$(OBJEXT) $(EXTRA_OBJS) \ diff --git a/arch/arm64/src/Makefile b/arch/arm64/src/Makefile index fc1b4c9ffa..3be44f834c 100644 --- a/arch/arm64/src/Makefile +++ b/arch/arm64/src/Makefile @@ -157,7 +157,7 @@ board$(DELIM)libboard$(LIBEXT): $(Q) $(MAKE) -C board libboard$(LIBEXT) EXTRAFLAGS="$(EXTRAFLAGS)" define LINK_ALLSYMS - $(Q) $(TOPDIR)/tools/mkallsyms.py $(NUTTX) allsyms.tmp + $(Q) $(TOPDIR)/tools/mkallsyms.py $(NUTTX) allsyms.tmp --orderbyname $(CONFIG_SYMTAB_ORDEREDBYNAME) $(Q) $(call COMPILE, allsyms.tmp, allsyms$(OBJEXT), -x c) $(Q) $(LD) --entry=__start $(LDFLAGS) $(LIBPATHS) $(EXTRA_LIBPATHS) \ -o $(NUTTX) $(HEAD_OBJ) allsyms$(OBJEXT) $(EXTRA_OBJS) \ diff --git a/arch/risc-v/src/Makefile b/arch/risc-v/src/Makefile index 0b23888be0..11914e50c8 100644 --- a/arch/risc-v/src/Makefile +++ b/arch/risc-v/src/Makefile @@ -162,7 +162,7 @@ board/libboard$(LIBEXT): $(Q) $(MAKE) -C board libboard$(LIBEXT) EXTRAFLAGS="$(EXTRAFLAGS)" define LINK_ALLSYMS - $(Q) $(TOPDIR)/tools/mkallsyms.py $(NUTTX) allsyms.tmp + $(Q) $(TOPDIR)/tools/mkallsyms.py $(NUTTX) allsyms.tmp --orderbyname $(CONFIG_SYMTAB_ORDEREDBYNAME) $(Q) $(call COMPILE, allsyms.tmp, allsyms$(OBJEXT), -x c) $(Q) $(LD) --entry=__start $(LDFLAGS) $(LIBPATHS) $(EXTRA_LIBPATHS) \ -o $(NUTTX) $(HEAD_OBJ) allsyms$(OBJEXT) $(EXTRA_OBJS) \ diff --git a/arch/sim/src/Makefile b/arch/sim/src/Makefile index a1328ad90c..073bfd2983 100644 --- a/arch/sim/src/Makefile +++ b/arch/sim/src/Makefile @@ -354,7 +354,7 @@ nuttx-names.dat: nuttx-names.in define LINK_ALLSYMS $(if $(CONFIG_HOST_MACOS), \ $(Q) $(TOPDIR)/tools/mkallsyms.sh noconst $(NUTTX) $(CROSSDEV) > allsyms.tmp, \ - $(Q) $(TOPDIR)/tools/mkallsyms.py $(NUTTX) allsyms.tmp) + $(Q) $(TOPDIR)/tools/mkallsyms.py $(NUTTX) allsyms.tmp --orderbyname $(CONFIG_SYMTAB_ORDEREDBYNAME)) $(Q) $(call COMPILE, allsyms.tmp, allsyms$(OBJEXT), -x c) $(if $(CONFIG_HAVE_CXX),\ $(Q) "$(CXX)" $(CFLAGS) $(LDFLAGS) -o $(NUTTX) \ diff --git a/arch/xtensa/src/Makefile b/arch/xtensa/src/Makefile index 2a6f450609..f4d7c3a802 100644 --- a/arch/xtensa/src/Makefile +++ b/arch/xtensa/src/Makefile @@ -158,7 +158,7 @@ board/libboard$(LIBEXT): $(Q) $(MAKE) -C board libboard$(LIBEXT) EXTRAFLAGS="$(EXTRAFLAGS)" define LINK_ALLSYMS - $(Q) $(TOPDIR)/tools/mkallsyms.py $(NUTTX) allsyms.tmp + $(Q) $(TOPDIR)/tools/mkallsyms.py $(NUTTX) allsyms.tmp --orderbyname $(CONFIG_SYMTAB_ORDEREDBYNAME) $(Q) $(call COMPILE, allsyms.tmp, allsyms$(OBJEXT), -x c) $(Q) $(LD) --entry=__start $(LDFLAGS) $(LIBPATHS) $(EXTRA_LIBPATHS) \ -o $(NUTTX) $(STARTUP_OBJS) allsyms$(OBJEXT) $(EXTRA_OBJS) \ diff --git a/tools/mkallsyms.py b/tools/mkallsyms.py index 46721d50ec..75488991d5 100755 --- a/tools/mkallsyms.py +++ b/tools/mkallsyms.py @@ -19,6 +19,7 @@ # ############################################################################ +import argparse import errno import os import re @@ -33,6 +34,7 @@ try: except ModuleNotFoundError: print("Please execute the following command to install dependencies:") print("pip install pyelftools cxxfilt") + os._exit(errno.EINVAL) class SymbolTables(object): @@ -97,7 +99,7 @@ class SymbolTables(object): return section - def parse_symbol(self): + def parse_symbol(self, orderbyname=False): if self.elffile is None: return symtable = self.get_symtable() @@ -109,47 +111,49 @@ class SymbolTables(object): except cxxfilt.InvalidName: symbol_name = symbol.name self.symbol_list.append((symbol["st_value"] & ~0x01, func_name)) - self.symbol_list = sorted(self.symbol_list, key=lambda item: item[0]) + if orderbyname: + self.symbol_list = sorted(self.symbol_list, key=lambda item: item[1]) + else: + self.symbol_list = sorted(self.symbol_list, key=lambda item: item[0]) def emitline(self, s=""): self.output.write(str(s) + "\n") def usage(): - print("Usage: mkallsyms.py [noconst] [output file]") + print( + "Usage: mkallsyms.py [noconst] [output file] [order symbols by name]" + ) os._exit(errno.ENOENT) -def parse_args(argv): - index = 1 - argc = len(argv) - outfile = None - elffile = None - - if argc > index and argv[index] == "--version": - print("mkallsyms.py: based on pyelftools %s" % __version__) - os.exit(0) - - if argc > index and argv[index] == "noconst": - noconst = True - index += 1 - else: - noconst = False - - if argc > index: - elffile = argv[index] - index += 1 - - if argc > index: - outfile = open(argv[index], "w") - else: - outfile = sys.stdout - - return noconst, elffile, outfile - - if __name__ == "__main__": - noconst, elffile, outfile = parse_args(sys.argv) - readelf = SymbolTables(elffile, outfile) - readelf.parse_symbol() - readelf.print_symbol_tables(noconst) + parser = argparse.ArgumentParser( + description="Process ELF binary to extract symbols." + ) + parser.add_argument("elffile", help="Path to the ELF binary file.") + parser.add_argument( + "outfile", + nargs="?", + type=argparse.FileType("w"), + default=sys.stdout, + help="Output file to write symbols to (default: stdout).", + ) + parser.add_argument("--noconst", action="store_true", help="Exclude const symbols.") + parser.add_argument( + "--version", + action="version", + version="mkallsyms.py: based on pyelftools %s" % __version__, + ) + parser.add_argument( + "--orderbyname", + nargs="?", + const=False, + default=False, + help='Order symbols by name (specify "y" to enable, default: False).', + ) + args = parser.parse_args() + + readelf = SymbolTables(args.elffile, args.outfile) + readelf.parse_symbol(args.orderbyname) + readelf.print_symbol_tables(args.noconst)