How do I find where a symbol is defined among static libraries

前端 未结 3 1701
无人共我
无人共我 2021-02-01 23:22

Suppose you work with a codebase comprising several tools and libraries and you want to port (or resurrect) some component within such codebase but any clue about where symbols

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-01 23:42

    Assuming a linux box, the nm tool, listing names in library files, comes to the rescue.

    It can be used to do an extensive search as follows: one can first find all the libraries available (assuming the project have been successfully compiled without the component you are adding) with a find, then such find can be enclosed in a loop where you call nm on all discovered libraries; the output you then grep for discarding "U" references (undefined symbols, aka where else the symbol is being used). On a single bash line that gives:

    for lib in $(find base_path -name \*.a) ; do echo $lib ; nm $lib | grep my_symbol | grep -v " U "   ; done
    

    where:

    • base_path is the root of your codebase
    • my_symbol is the symbol you are looking for

    The echo generates a list of all libraries found, which is not so clean since it outputs names of libs not holding the symbol, but it was the fastest way I found to have a direct reference to the library so when you see a:

    base_path/component/libA.a
    0000000000000080 D my_symbol
    

    You have found your usual suspect.

提交回复
热议问题