2
votes

I am doing cross compile debugging. My build server CPU is amd64. My device CPU is MIPS. When I am trying to do debug the elf file compiled by myself. The gdb can only show ld.so.1

(gdb) info sharedlibrary
 From        To          Syms Read   Shared Object Library
 0x7704f9c0  0x7706c490  Yes (*)     /lib/ld.so.1
 (*): Shared library is missing debugging information.
 (gdb) q

I checked the /proc/xxxx/maps file. It showed that the shared libraries are loaded.

root@TRA:/proc/13679# cat maps 
...... 
76549000-76d48000 rwxp 00000000 00:00 0 [stack:13682]                           
76d48000-76d4a000 r-xp 00000000 00:0c 5268 /usr/lib/strongswan/plugins/libstrongswan-addrblock.so                    
76d4a000-76d59000 ---p 00002000 00:0c 5268 /usr/lib/strongswan/plugins/libstrongswan-addrblock.so            
......

If I debug the file which is installed from Debian Package server, then GDB can show all the shared libraries.

(gdb) info sharedlibrary
 From        To          Syms Read   Shared Object Library
 0x77341bc0  0x77342c80  Yes (*)     /lib/mips-linux-gnu/libdl.so.2
 0x771d77e0  0x772ff6f0  Yes (*)     /lib/mips-linux-gnu/libc.so.6
 0x773549c0  0x77371490  Yes (*)     /lib/ld.so.1
 (*): Shared library is missing debugging information.
 (gdb)

GDB version is: GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1


My questions is: Why the GDB command 'info sharedlibrary' can't show all the libraries? How can I fix it?


(EDIT)

(Does every executable file need the library ld.so? It is missing.)

The output of command "mips-linux-gnu-readelf -d src/charon/.libs/charon"

Dynamic section at offset 0x1fc contains 33 entries:
   Tag        Type                         Name/Value
  0x00000001 (NEEDED)                     Shared library: [libstrongswan.so.0]
  0x00000001 (NEEDED)                     Shared library: [libhydra.so.0]
  0x00000001 (NEEDED)                     Shared library: [libcharon.so.0]
  0x00000001 (NEEDED)                     Shared library: [libm.so.6]
  0x00000001 (NEEDED)                     Shared library: [libpthread.so.0]
  0x00000001 (NEEDED)                     Shared library: [libdl.so.2]
  0x00000001 (NEEDED)                     Shared library: [libc.so.6]
  0x0000001d (RUNPATH)                    Library runpath: [/usr/lib/strongswan]
  0x0000000c (INIT)                       0xd00
  0x0000000d (FINI)                       0x2eb0
  0x00000004 (HASH)                       0x32c
  0x00000005 (STRTAB)                     0x904
  0x00000006 (SYMTAB)                     0x4d4
  0x0000000a (STRSZ)                      787 (bytes)
  0x0000000b (SYMENT)                     16 (bytes)
  0x70000035 (MIPS_RLD_MAP_REL)           0x134dc
  0x00000015 (DEBUG)                      0x0
  0x00000003 (PLTGOT)                     0x13760
  0x00000011 (REL)                        0xcf0
  0x00000012 (RELSZ)                      16 (bytes)
  0x00000013 (RELENT)                     8 (bytes)
  0x70000001 (MIPS_RLD_VERSION)           1
  0x70000005 (MIPS_FLAGS)                 NOTPOT
  0x70000006 (MIPS_BASE_ADDRESS)          0x0
  0x7000000a (MIPS_LOCAL_GOTNO)           18
  0x70000011 (MIPS_SYMTABNO)              67
  0x70000012 (MIPS_UNREFEXTNO)            37
  0x70000013 (MIPS_GOTSYM)                0x11
  0x6ffffffb (FLAGS_1)                    Flags: PIE
  0x6ffffffe (VERNEED)                    0xca0
  0x6fffffff (VERNEEDNUM)                 2
  0x6ffffff0 (VERSYM)                     0xc18
  0x00000000 (NULL)                       0x0

EDIT

Debuging GDB: The gdb query ‘qXfer:libraries-svr4:read’ returned empty library list.

 Breakpoint 7, svr4_current_sos_via_xfer_libraries (list=0x7fff8be59ad0, annex=<optimized out>)
 at /gdb/gdb-7.11.1/gdb/solib-svr4.c:1301
 1301  result = svr4_  parse_libraries (svr4_library_document, list);
 1: svr4_library_document = 0x15cd9c0 "<library-list-svr4 version=\"1.0\"/>"
 (gdb)

For Debian packages which are not compiled by me, the gdb query ‘qXfer:libraries-svr4:read’ returned full shared library list.

How does gdbserver construct the reply of this query ‘qXfer:libraries-svr4:read’?


EDIT

One more clue:

The pkgs installed from debian Jessie distribute is not PIE code. The code I compiled is PIE code.

root@TRA:/proc/14956# readelf -r /usr/lib/strongswan/charon

Relocation section '.rel.dyn' at offset 0xcf0 contains 2 entries:
Offset     Info    Type            Sym.Value  Sym. Name
00000000  00000000 R_MIPS_NONE
00013870  00000003 R_MIPS_REL32

root@TRA:/proc/14956# readelf -r /usr/bin/id

There are no relocations in this file.
root@TRA:/proc/14956#

EDIT

After debugging gdbserver, I found one strange info. The DT_DEBUG entry of the running proc is 0. After loader relocate the code, the DT_DEBUG should not be 0.(?) Does the system not support PIE code? I am using Debian Jessie MIPS system.

gdbserver source code:

if (dyn->d_tag == DT_DEBUG && map == -1)
   map = dyn->d_un.d_val;

gdbserver dbg print

(gdb) p *dyn
$19 = {d_tag = 21, d_un = {d_val = 0, d_ptr = 0}}
(gdb)

EDIT

Get some information from this link: https://sourceware.org/ml/binutils/2015-06/msg00166.html

I installed gdbserver from Debian Jessie mips-pkg server. But it seems not support PIE. Where can I install the mips-gdbserver which can support PIE?

Or how can I disable the gcc compiler generate PIE code? I tried these flags (-fno-pie -fPIC) in cross-compile, but it still generate PIE code.

libtool: link: mips-linux-gnu-gcc -mfp32 -fno-pie -fPIC 
      -ggdb -O0 -Wall -Wno-format -Wno-format-security 
      -Wno-pointer-sign -I/cross-mips/usr/include -I/cross-mips/usr/include/libnl3 
      -I/cross-mips/usr/include/mips-linux-gnu 
      -I/work/strongswan/src/util 
      -include /work/strongswan/config.h 
      -o .libs/charon charon.o  -L/cross-mips/lib/mips-linux-gnu 
      - L/cross-mips -L/cross-mips/usr/lib/mips-linux-gnu 
      ../../src/libstrongswan/.libs/libstrongswan.so 
      -lm -lpthread -ldl -Wl,-rpath -Wl,/usr/lib/strongswan

Check the generated code:

 mips-linux-gnu-readelf -r src/charon/.libs/charon

 Relocation section '.rel.dyn' at offset 0xcf0 contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
 00000000  00000000 R_MIPS_NONE
 00013870  00000003 R_MIPS_REL32

Solution Unfortunately the reason is my compiler gcc-6 is brocken. I used 'gcc version 6.3.0 20170516 (Debian 6.3.0-18)'. It is configured with '--enable-default-pie'. And there is no way to disable PIE. And this PIE breaks static library links. I have to change my compiler to gcc5.

1
Where exactly did you stop your binary when you issued info shared command?Employed Russian
After command 'run' few seconds, I issued 'Ctrl-C' interrupt the program and I issued 'info shared'.PokerFace

1 Answers

1
votes

From the info you provided, it seems that there are two likely causes:

  1. Either you fully strip your binary, and gdbserver requires some symbol, or

  2. You are building a PIE binary, and gdbserver on your system doesn't support such binaries.

(It's also possible that it's the combination of 1 and 2 that causes the problem.)

Since you know that the distribution binaries work, your best bet is probably to understand the differences between them and your binary, and minimizing such differences until gdbserver starts working.