How to reverse the objcopy's strip with only-keep-debug?
Asked Answered
E

2

25

In modern linux almost all objects are stripped and splitted in two parts (two files). First is executable itself and second is debug symbols, stripped out from original ELF. Such files are created with

objcopy --only-keep-debug original.elf binary.dbg
mv original.elf binary
objcopy --strip-debug binary

How can I merge binary and binary.dbg into ELF file with debugging info? I want to recreate unstripped, original binary. It can be not byte-to-byte equal to the original, but it must to have a debug symbols inside.

PS Yes, I know about gnu.debuglink section, but it doesn't work for some debuggers (etnus) and disassemblers (objdump can't restore symbols info)

Ebeneser answered 24/3, 2010 at 16:9 Comment(0)
R
13

For ELF, the elfutils package contains a tool called eu-unstrip that does the job. In the context of your example:

eu-unstrip binary binary.dbg

binary.dbg now has both the binary and debug symbols. I'd include a reference to documentation if I could find any...

Reprint answered 28/7, 2016 at 19:59 Comment(3)
There is the doc: linuxfromscratch.org/blfs/view/svn/general/elfutils.html "eu-unstrip combine stripped files with separate symbols and debug information." and this is homepage fedorahosted.org/elfutils and this is source git.fedorahosted.org/cgit/elfutils.git/tree/src/unstrip.c "Combine stripped files with separate symbols and debug information. Copyright (C) 2007-2012, 2014, 2015 Red Hat, Inc...Roland McGrath". If I have "program.bin" as normal stripped ELF, and "program.dbg" with only debug info, how should I start eu-unstrip and which file will be changed?Ebeneser
Based on my usage, it merges the binary into the debug symbols. If you have program.dbg and program.bin, then eu-unstrip program.bin program.dbg merges the bin into the dbg file. Getting the params reversed yields errors.Reprint
I dont have access to elfutils but only binutils. Is it possible to do it using objcopy --add-section or similar ?Counterstamp
L
1

You can use eu-unstrip utility which merges debug symbols with executables.

eu-unstrip -f <executablefilename> <symbolefilename.debug> -o <newoutputfilename>
Liechtenstein answered 17/1, 2023 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.