简体   繁体   English

如何撤消strip - 即将符号添加回剥离的二进制文件

[英]How to undo strip - i.e. add symbols back to stripped binary

I have a stripped binary and symbol-file. 我有一个剥离的二进制和符号文件。 Is it possible to add the symbols back to binary and create an unstripped binary. 是否可以将符号添加回二进制文件并创建未提取的二进制文件。

My use-case is using this binary w/ valgrind. 我的用例是使用这个带w / valgrind的二进制文件。

For those tools that do not support separate files for debug information, you can glue the debug sections back to the original binary . 对于那些不支持调试信息的单独文件的工具,可以将调试部分粘合回原始二进制文件

You can do something along these lines, for example: 您可以沿着这些方向做某些事情,例如:

  • First build a small program that efficiently extracts an arbitrary chunk from a file 首先构建一个小程序,从文件中有效地提取任意块

    (note that dd will not do this efficiently as we'd have to use bs=1 to support an arbitrary offset and length, and objcopy -O binary does not copy sections that are not ALLOC, LOAD ) (请注意, dd不会有效地执行此操作,因为我们必须使用bs=1来支持任意偏移量长度,而objcopy -O binary不会复制不是ALLOC, LOAD ※的部分

     cat <<EOF | gcc -xc -o ./mydd - #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> #include <macros.h> char buf[1024*1024]; int main(int argc, char** argv) { char *fin, *fout; int fdin, fdout; off_t off; size_t len; ssize_t rd; int status; if (argc != 5) { fprintf(stderr, "Usage: %s fin skip count fout\\n", argv[0]); return 1; } fin = argv[1]; off = strtoul(argv[2], NULL, 0); len = strtoul(argv[3], NULL, 0); fout = argv[4]; fdin = -1; fdout = -1; if ((fdin = open(fin, O_RDONLY)) < 0) { status = errno; perror(fin); } else if ((fdout = open(fout, O_WRONLY|O_TRUNC|O_CREAT, 0660)) < 0) { status = errno; perror(fout); } else if (lseek(fdin, off, SEEK_SET) == (off_t)-1) { status = errno; perror("Seeking input"); } else { while (len > 0 && (rd = read(fdin, buf, min(len, sizeof(buf)))) > 0) { if (write(fdout, buf, rd) != rd) { /*don't bother with partial writes or EINTR/EAGAIN*/ status = errno; perror(fin); break; } len -= rd; } if (rd < 0) { status = errno; perror(fin); } } if (fdin >= 0) close(fdin); if (fdout >= 0) close(fdout); return status; } EOF 
  • Finally, extract the .debug sections and glue them to the stripped binary. 最后,提取.debug部分并将它们粘贴到剥离的二进制文件中。

     objcopy ` objdump -h program.dbg | awk '$2~/^\\.debug/' | while read idx name size vma lma off algn ; do echo "$name" >&2 echo " --add-section=$name=$name.raw" ./mydd program.dbg 0x$off 0x$size $name".raw" done ` program program_with_dbg 

Valgrind支持单独的调试文件 ,因此您应该在这里使用答案 ,并且valgrind应该与外部化调试文件一起正常工作。

elfutils comes with the tool eu-unstrip which can be used to merge symbol files with executables. elfutils附带了工具eu-unstrip ,可用于将符号文件与可执行文件合并。 The result can then be used in place of the stripped version. 然后可以使用结果代替剥离版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果尚未剥离应用程序版本,如何使用GDB附加到剥离的应用程序获取调试符号? - How do I get debugging symbols using GDB attach to stripped application if i have not stripped application version? 如何验证已从二进制文件中删除了无效代码? - how can I verify that dead code was stripped from the binary? 如何仅从依赖库中删除符号? - How do I strip symbols only from dependent libraries? 如何判断一个进程的state(即是否为僵尸) - How to determine the state of a process (i.e. if it is a zombie) 如何从目标文件中删除符号,然后将其归档为.a - how to strip symbols from an object file and then archive as .a dlsym 如何从剥离的二进制库中成功导入 function? - How can dlsym successfully import function from stripped binary library? 如何找出哪个进程正在消耗“等待 CPU”(即 I/O 阻塞) - How to find out which process is consuming “wait CPU” (i.e. I/O blocked) 如何计算linux中两个二进制文件(即两个可执行文件)之间的差异 - how to compute differences between two binaries (i.e., two executables) in linux 如何在shell或Perl脚本中发出“模块加载”(即非交互式) - How to issue “module load” in a shell or Perl script (i.e., non-interactively) "如何在 C 中正确计时写入磁盘(即文件)的速度" - How to correctly time speed of writing to a disk (i.e. file) in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM