OpenWrt Forum Archive

Topic: Addind debugging symbols to a package

The content of this topic has been archived on 1 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I asked yesterday in the IRC channel how to add debugging symbols to a binary from a package I build. Jow offered two possibilities. He said that it can be done on the command line by adding something, and that it doesn't always work. If it doesn't work, it is possible to add

TARGET_CFLAGS+=-ggdb3 -O0

to the Makefile.

Since the second method sounded like a sure bet, I tried it first and sadly I didn't keep the first method command line parameter.

I tried adding the line to the Makefile at the global scope and in the define Package section but I think that both of them did not add debugging symbols. I ran objdump and readelf on the resulting binaries (extracted with "tar zxvf serval-dna.ipk") and these are the outputs:

% readelf -aw servald
ELF Header:
  Magic:   7f 45 4c 46 01 02 01 00 01 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, big endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       1
  Type:                              EXEC (Executable file)
  Machine:                           MIPS R3000
  Version:                           0x1
  Entry point address:               0x40a910
  Start of program headers:          52 (bytes into file)
  Start of section headers:          0 (bytes into file)
  Flags:                             0x70001005, noreorder, cpic, o32, mips32r2
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         8
  Size of section headers:           0 (bytes)
  Number of section headers:         0
  Section header string table index: 0

There are no sections in this file.

There are no sections to group in this file.

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x00400034 0x00400034 0x00100 0x00100 R E 0x4
  INTERP         0x000134 0x00400134 0x00400134 0x00014 0x00014 R   0x1
      [Requesting program interpreter: /lib/ld-uClibc.so.0]
  REGINFO        0x000148 0x00400148 0x00400148 0x00018 0x00018 R   0x4
  LOAD           0x000000 0x00400000 0x00400000 0x206754 0x206754 R E 0x10000
  LOAD           0x206754 0x00616754 0x00616754 0x0fc44 0x145414 RW  0x10000
  DYNAMIC        0x000160 0x00400160 0x00400160 0x00138 0x00138 RWE 0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RWE 0x4
  NULL           0x000000 0x00000000 0x00000000 0x00000 0x00000     0x4

Dynamic section at offset 0x160 contains 34 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libm.so.0]
 0x00000001 (NEEDED)                     Shared library: [libdl.so.0]
 0x00000001 (NEEDED)                     Shared library: [librt.so.0]
 0x00000001 (NEEDED)                     Shared library: [libgcc_s.so.1]
 0x00000001 (NEEDED)                     Shared library: [libpthread.so.0]
 0x00000001 (NEEDED)                     Shared library: [libc.so.0]
 0x0000000c (INIT)                       0x40a894
 0x0000000d (FINI)                       0x5cf200
 0x00000004 (HASH)                       0x400298
 0x00000005 (STRTAB)                     0x4058d0
 0x00000006 (SYMTAB)                     0x401a60
 0x0000000a (STRSZ)                      18260 (bytes)
 0x0000000b (SYMENT)                     16 (bytes)
 0x70000016 (MIPS_RLD_MAP)               0x625350
 0x00000015 (DEBUG)                      0x0
 0x00000003 (PLTGOT)                     0x625360
 0x00000011 (REL)                        0x40a834
 0x00000012 (RELSZ)                      88 (bytes)
 0x00000013 (RELENT)                     8 (bytes)
 0x70000001 (MIPS_RLD_VERSION)           1
 0x70000005 (MIPS_FLAGS)                 NOTPOT
 0x70000006 (MIPS_BASE_ADDRESS)          0x400000
 0x7000000a (MIPS_LOCAL_GOTNO)           858
 0x70000011 (MIPS_SYMTABNO)              999
 0x70000012 (MIPS_UNREFEXTNO)            42
 0x70000013 (MIPS_GOTSYM)                0x32e
 0x00000014 (PLTREL)                     REL
 0x00000017 (JMPREL)                     0x40a88c
 0x00000002 (PLTRELSZ)                   8 (bytes)
 0x70000032 (MIPS_PLTGOT)                0x625354
 0x6ffffffe (VERNEED)                    0x40a7f4
 0x6fffffff (VERNEEDNUM)                 1
 0x6ffffff0 (VERSYM)                     0x40a024
 0x00000000 (NULL)                       0x0

There are no relocations in this file.

There are no unwind sections in this file.

Histogram for bucket list length (total of 521 buckets):
 Length  Number     % of total  Coverage
      0  75         ( 14.4%)
      1  156        ( 29.9%)     15.6%
      2  131        ( 25.1%)     41.9%
      3  93         ( 17.9%)     69.8%
      4  43         (  8.3%)     87.1%
      5  12         (  2.3%)     93.1%
      6  9          (  1.7%)     98.5%
      7  1          (  0.2%)     99.2%
      8  1          (  0.2%)    100.0%

No version information found in this file.
readelf: Warning: Virtual address 0x625360 not located in any PT_LOAD segment.
readelf: Error: Unable to read in 0x104c bytes of GOT
% objdump -g ./servald

./servald:     file format elf32-big

Here is the complete makefile (It's a bit different than what you wrote Jow, after the developers of serval saw your makefile and patch they changed it to work without the patch).

include $(TOPDIR)/rules.mk

PKG_NAME:=serval-dna
PKG_VERSION:=2012-10-25
PKG_RELEASE=$(PKG_SOURCE_VERSION)

PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/servalproject/serval-dna.git
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)
PKG_SOURCE_VERSION:=be3b529504450680065008aab95e2ed3f1a5a3d5
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz

PKG_FIXUP:=autoreconf

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)
PKG_BUILD_DEPENDS:=

include $(INCLUDE_DIR)/package.mk

define Package/serval-dna
        SECTION:=utils
        CATEGORY:=Utilities
        TITLE:=serval-dna
        DEPENDS:=+libpthread +librt
endef

define Package/serval-dna/description
  Serval Daemon
endef

define Package/serval-dna/install
        $(INSTALL_DIR) $(1)/usr/bin
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/servald $(1)/usr/bin
endef

$(eval $(call BuildPackage,serval-dna))

So the questions are:
1. What are the command line arguments of make that I lost and can add debugging symbols?
2. Where in the makefile should I add the line suggested in order for the debugging symbols to be added?
3. How to check if debugging symbols were actually added?

Thank you!

make package/serval-dna/{clean,compile} V=99 TARGET_OPTIMIZATION="-ggdb3 -O0" STRIP="/bin/true"

Thank you!
As usual, your solution works, I think.
I think becuause with the command line argument, when trying to compile, the compiler looks for non present shared objects:

cp: cannot stat '/home/jonathan/OpenWrt/build/trunk/staging_dir/toolchain-mips_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libuClibc-*.so': No such file or directory
cp: cannot stat '/home/jonathan/OpenWrt/build/trunk/staging_dir/toolchain-mips_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libcrypt-*.so': No such file or directory
cp: cannot stat '/home/jonathan/OpenWrt/build/trunk/staging_dir/toolchain-mips_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libm-*.so': No such file or directory
cp: cannot stat '/home/jonathan/OpenWrt/build/trunk/staging_dir/toolchain-mips_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libpthread-*.so': No such file or directory

I tried to find them in menuconfig but I could only find pthreads which was configured as a module (M), I changed it to (*) and ran "make" and then tried to build servald again but it didn't help. About the others, I didn't find them under Libraries or development. Are they separate packages which can be enabled or are they disabled features of uClibc?

Can you post the full output of a make V=99?

Yes, thank you.

 % make package/serval-dna/{clean,compile} V=99 TARGET_OPTIMIZATION="-ggdb3 -O0" STRIP="/bin/true"
make[1]: Entering directory `/home/jonathan/OpenWrt/build/trunk'
make[2]: Entering directory `/home/jonathan/OpenWrt/build/trunk/package/serval-dna'
rm -f /home/jonathan/OpenWrt/build/trunk/staging_dir/target-mips_uClibc-0.9.33.2/stamp/.serval-dna_installed
rm -f /home/jonathan/OpenWrt/build/trunk/bin/ar71xx/packages/serval-dna_*
rm -f /home/jonathan/OpenWrt/build/trunk/staging_dir/target-mips_uClibc-0.9.33.2/packages/serval-dna.list /home/jonathan/OpenWrt/build/trunk/staging_dir/host/packages/serval-dna.list
rm -rf /home/jonathan/OpenWrt/build/trunk/build_dir/target-mips_uClibc-0.9.33.2/serval-dna-2012-10-25
make[2]: Leaving directory `/home/jonathan/OpenWrt/build/trunk/package/serval-dna'
make[1]: Leaving directory `/home/jonathan/OpenWrt/build/trunk'
make[1]: Entering directory `/home/jonathan/OpenWrt/build/trunk'
make[2]: Entering directory `/home/jonathan/OpenWrt/build/trunk/package/toolchain'
cp -fpR /home/jonathan/OpenWrt/build/trunk/staging_dir/toolchain-mips_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libuClibc-*.so /home/jonathan/OpenWrt/build/trunk/staging_dir/toolchain-mips_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libcrypt-*.so /home/jonathan/OpenWrt/build/trunk/staging_dir/toolchain-mips_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libm-*.so /home/jonathan/OpenWrt/build/trunk/staging_dir/toolchain-mips_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libpthread-*.so /home/jonathan/OpenWrt/build/trunk/build_dir/target-mips_uClibc-0.9.33.2/toolchain/
cp: cannot stat '/home/jonathan/OpenWrt/build/trunk/staging_dir/toolchain-mips_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libuClibc-*.so': No such file or directory
cp: cannot stat '/home/jonathan/OpenWrt/build/trunk/staging_dir/toolchain-mips_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libcrypt-*.so': No such file or directory
cp: cannot stat '/home/jonathan/OpenWrt/build/trunk/staging_dir/toolchain-mips_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libm-*.so': No such file or directory
cp: cannot stat '/home/jonathan/OpenWrt/build/trunk/staging_dir/toolchain-mips_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libpthread-*.so': No such file or directory
make[2]: *** [/home/jonathan/OpenWrt/build/trunk/build_dir/target-mips_uClibc-0.9.33.2/toolchain/.built] Error 1
make[2]: Leaving directory `/home/jonathan/OpenWrt/build/trunk/package/toolchain'
make[1]: *** [package/toolchain/compile] Error 2
make[1]: Leaving directory `/home/jonathan/OpenWrt/build/trunk'
make: *** [package/serval-dna/compile] Error 2

I don't see how TARGET_OPTIMIZATION or STRIP could lead to the error you're getting - thought I confess to a less than complete knowledge of the OpenWrt build system.

Can you try without those two options? (bare make V=99 essentially)

Without it it builds just fine. The reason I need the debugging symbols is because there is a segfault in the application and I want to track where it happens.
I'm not on the build machine right now, but would you like me to post the full output of the successful build when I can, or maybe it won't help?

I'm not sure - I'm starting to suspect the STRIP. But basically try a few different combinations.. without TARGET_OPTIMIZATION or STRIP, with only TARGET_OPTIMIZATION, with only STRIP, etc.

But it seems like TARGET_CFLAGS in the Makefile is a better way to do this (more selective to just the package). Maybe you can get that working.

I tried different combinations. The STRIP is fine, the TARGET_OPTIMIZATION causes it.
The addition of STRIP did help a bit because now I can see which function causes a segfault but if I could add -g it would have been great.

I tried adding TARGET_CFLAGS in the makefile but it didn't do anything, I probably did it wrong.
Can you show me where and what to add to the makefile in order to get the effect of adding -g and disabling strip?
The full makefile is in the first message of the thread.

Thanks!

I'm looking at include/package-default.mk and TARGET_CFLAGS should be passed to the package's ./configure with CFLAGS="$(TARGET_CFLAGS) $(EXTRA_CFLAGS)".

Can you add 'TARGET_CFLAGS += -ggdb' to the Makefile, just after PKG_BUILD_DEPENDS, and post a 'make package/serval-dna/{clean,compile} V=99' ?

The prevention of strip was enough in this case to solve the problem we had (segfault).
I'll try your suggestion when I need to build the software again.

Thank you!

The discussion might have continued from here.