0

我想知道如何将二进制文件调用的外部函数与其链接的共享库相匹配。例如,我可以查看反汇编文件的 .plt 部分的函数,我可以使用 ldd 找到使用的库(或查看 ELF 动态部分);但是如何将每个函数与其库匹配?

4

1 回答 1

0

我按照 Lazio 的提示创建了一个小 Python 函数,它获取二进制文件名,并通过混合 ldd 和 nm,返回一个字典,其中包含外部函数及其共享库。也许这有点令人困惑,但它有效:) 这是代码

def get_dynamicOBJ(filename):
   p_nm = subprocess.Popen(["nm", "-D", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   result_nm = p_nm.stdout.readlines()
   p_ldd = subprocess.Popen(["ldd", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   result_ldd = p_ldd.stdout.readlines()
   dyn = {}

   for nm_out in result_nm:
       sym_entry = nm_out.split()
       if len(sym_entry) >= 2 and sym_entry[0 if len(sym_entry) == 2 else 1] == "U":
           sym = sym_entry[1 if len(sym_entry) == 2 else 2]
           for lld_out in result_ldd:
               lib_entry = lld_out.split()
               if "=>" in lld_out and len(lib_entry) > 3: # virtual library
                   lib = lib_entry[2]
                   ls_nm = subprocess.Popen(["nm", "-D", lib], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                   result_lsnm = ls_nm.stdout.readlines()
                   for ls_nm_out in result_lsnm:
                       lib_symbol = ls_nm_out.split()
                       if len(lib_symbol) >= 2 and lib_symbol[0 if len(lib_symbol) == 2 else 1] == "T":                            
                           if sym == lib_symbol[1 if len(lib_symbol) == 2 else 2]:
                               dyn[sym] = lib

   return dyn
于 2013-10-09T22:53:36.610 回答