How to list library dependencies of a non-native binary?

后端 未结 9 802
孤城傲影
孤城傲影 2020-12-12 16:03

When developing for native platform, I can use ldd to list all the shared libraries (.so files) a binary executable I build will try to load upon start-up. But when cross-co

相关标签:
9条回答
  • 2020-12-12 16:53

    this helped me

    objdump -p /path/to/program | grep NEEDED 
    
    0 讨论(0)
  • 2020-12-12 17:00

    You can do bash -x ldd /bin/ls to understand what ldd is doing. The ldd script is not that "cryptic". It basically runs

    LD_TRACE_LOADED_OBJECTS=1 /lib64/ld-linux-x86-64.so.2 /bin/ls
    

    so it uses the dynamic loader of the system (because the result of ldd depends upon your actual environment and system!). But you could examine with objdump -x /bin/ls the dynamic section of an executable, e.g.

    % objdump -x /bin/ls
      /bin/ls:     file format elf64-x86-64
      /bin/ls
      architecture: i386:x86-64, flags 0x00000112:
      EXEC_P, HAS_SYMS, D_PAGED
      start address 0x00000000004046d4
    
      Program Header:
          PHDR off    0x0000000000000040 vaddr 0x0000000000400040 paddr 0x0000000000400040 align 2**3
               filesz 0x00000000000001c0 memsz 0x00000000000001c0 flags r-x
        INTERP off    0x0000000000000200 vaddr 0x0000000000400200 paddr 0x0000000000400200 align 2**0
               filesz 0x000000000000001c memsz 0x000000000000001c flags r--
          LOAD off    0x0000000000000000 vaddr 0x0000000000400000 paddr 0x0000000000400000 align 2**21
               filesz 0x0000000000019ef4 memsz 0x0000000000019ef4 flags r-x
          LOAD off    0x000000000001a000 vaddr 0x000000000061a000 paddr 0x000000000061a000 align 2**21
               filesz 0x000000000000077c memsz 0x0000000000001500 flags rw-
       DYNAMIC off    0x000000000001a028 vaddr 0x000000000061a028 paddr 0x000000000061a028 align 2**3
               filesz 0x00000000000001d0 memsz 0x00000000000001d0 flags rw-
          NOTE off    0x000000000000021c vaddr 0x000000000040021c paddr 0x000000000040021c align 2**2
               filesz 0x0000000000000044 memsz 0x0000000000000044 flags r--
      EH_FRAME off    0x0000000000017768 vaddr 0x0000000000417768 paddr 0x0000000000417768 align 2**2
               filesz 0x00000000000006fc memsz 0x00000000000006fc flags r--
         STACK off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**3
               filesz 0x0000000000000000 memsz 0x0000000000000000 flags rw-
    
      Dynamic Section:
        NEEDED               libselinux.so.1
        NEEDED               librt.so.1
        NEEDED               libacl.so.1
        NEEDED               libc.so.6
        INIT                 0x0000000000402148
        FINI                 0x00000000004125f8
        HASH                 0x0000000000400260
        GNU_HASH             0x00000000004005c0
        STRTAB               0x0000000000401100
        SYMTAB               0x0000000000400620
        STRSZ                0x00000000000004d7
        SYMENT               0x0000000000000018
        DEBUG                0x0000000000000000
        PLTGOT               0x000000000061a208
        PLTRELSZ             0x0000000000000990
        PLTREL               0x0000000000000007
        JMPREL               0x00000000004017b8
        RELA                 0x0000000000401740
        RELASZ               0x0000000000000078
        RELAENT              0x0000000000000018
        VERNEED              0x00000000004016c0
        VERNEEDNUM           0x0000000000000003
        VERSYM               0x00000000004015d8
    
      Version References:
        required from librt.so.1:
          0x09691a75 0x00 05 GLIBC_2.2.5
        required from libacl.so.1:
          0x05822452 0x00 06 ACL_1.2
          0x05822450 0x00 04 ACL_1.0
        required from libc.so.6:
          0x09691a75 0x00 03 GLIBC_2.2.5
          0x0d696913 0x00 02 GLIBC_2.3
    

    Again, the actual dependency depends upon the system where your binary is run (e.g. because I could have an LD_LIBRARY_PATH with my own libc.so.6 somewhere, which would be a bad idea).

    So you need a cross variant of objdump

    0 讨论(0)
  • 2020-12-12 17:00

    By design ldd can only been executed on target. However, it is possible to mimic ldd behavior using readelf. A script called `xldd' was developed in crosstool-ng project. An independent version of this script is available here:

    https://gist.github.com/jerome-pouiller/c403786c1394f53f44a3b61214489e6f

    0 讨论(0)
提交回复
热议问题