gdb 8.2无法识别macOS Mojave 10.14上的可执行文件 [英] gdb 8.2 can't recognized executable file on macOS Mojave 10.14

查看:252
本文介绍了gdb 8.2无法识别macOS Mojave 10.14上的可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过brew install gdb获得了gdb.

源文件内容为:

#include <cstdio>
int main(){
    int a = 10;
    for(int i = 0; i< 10; i++){
        a += i;
    }
    printf("%d\n",a);
    return 0;
}

这是名为"demo"的可执行文件: https://pan.baidu.com/s/1wg-ffGCYzPGDI77pRxhyaw

Here is the executable file named 'demo': https://pan.baidu.com/s/1wg-ffGCYzPGDI77pRxhyaw

我这样编译源文件:

c++ -g -o demo demo.cpp

并运行gdb

gdb ./demo

但是,它不起作用.无法识别可执行文件.

But, it can't work. It can't recognized the executable file.

GNU gdb (GDB) 8.2
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin18.0.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
BFD: /Users/xxx/Codes/demo: unknown load command 0x32
BFD: /Users/xxx/Codes/demo: unknown load command 0x32
"/Users/xxx/Codes/demo": not in executable format: file format not recognized

我使用file demo,其输出是demo: Mach-O 64-bit executable x86_64

我使用file ./demo,其输出为./demo: Mach-O 64-bit executable x86_64

类型c++ -v,输出为:

Apple LLVM version 10.0.0 (clang-1000.10.44.2)
Target: x86_64-apple-darwin18.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

运行./demo,其输出为55 在gdb中输入show configuration,它显示:

run ./demo,its output is 55 type show configuration in gdb,it shows:

 This GDB was configured as follows:
 configure --host=x86_64-apple-darwin18.0.0 --target=x86_64-apple-darwin18.0.0
         --with-auto-load-dir=:${prefix}/share/auto-load
         --with-auto-load-safe-path=:${prefix}/share/auto-load
         --with-expat
         --with-gdb-datadir=/usr/local/Cellar/gdb/8.2/share/gdb (relocatable)
         --with-jit-reader-dir=/usr/local/Cellar/gdb/8.2/lib/gdb (relocatable)
         --without-libunwind-ia64
         --without-lzma
         --without-babeltrace
         --without-intel-pt
         --disable-libmcheck
         --without-mpfr
         --with-python=/System/Library/Frameworks/Python.framework/Versions/2.7
         --without-guile
         --with-separate-debug-dir=/usr/local/Cellar/gdb/8.2/lib/debug (relocatable)

谁可以帮助我?非常感谢!!!

Who can help me ? Thank you very much !!!

推荐答案

问题在于,随Apple LLVM version 10.0.0分发的clang-1000.11.45.2向名为LC_BUILD_VERSION的o-mach可执行文件添加了新的加载命令.

The problem is that clang-1000.11.45.2 distributed with Apple LLVM version 10.0.0 add a new load command to o-mach executables named LC_BUILD_VERSION.

$ otool -l test.o
...
Load command 1
       cmd LC_BUILD_VERSION
   cmdsize 24
  platform macos
       sdk n/a
     minos 10.14
    ntools 0
...

从苹果来源:

/*
 * The build_version_command contains the min OS version on which this
 * binary was built to run for its platform.  The list of known platforms and
 * tool values following it.
 */

因此,当前bfd(gdb用于操纵可执行程序的程序)无法解释此命令,并返回错误.

So currently bfd (program used by gdb to manipulate executables) is not able to interpret this command, and returns the error.

我找到的临时解决方案是直接编辑gdb提供的bfd来源. 我只测试了gdb-8.0.1.

The temporary solution I found is directly edit bfd sources provide with gdb. I've only test with gdb-8.0.1.

首先,从镜像下载gdb-8.0.1源.然后在4649行中将以下代码添加到gdb-8.0.1/bfd/mach-o.c:

First, download gdb-8.0.1 sources from mirrors. Then add to gdb-8.0.1/bfd/mach-o.c the following code at line 4649 :

case BFD_MACH_O_LC_BUILD_VERSION:
break;

最后添加int gdb-8.0.1/include/mach-o/loader.h:

  BFD_MACH_O_LC_BUILD_VERSION = 0x32

189行(不要忘记在BFD_MACH_O_LC_VERSION_MIN_WATCHOS = 0x30之后的188行末尾添加,).

at line 189 (don't forget to add a , at the end of the line 188 after BFD_MACH_O_LC_VERSION_MIN_WATCHOS = 0x30).

按照这些说明进行操作之后,您可以按照README中的指示进行经典的gdb编译:

After these instructions you can follow a classic gdb compilation as indicate inside the README :

run the ``configure'' script here, e.g.:

    ./configure 
    make

To install them (by default in /usr/local/bin, /usr/local/lib, etc),
then do:
    make install

别忘了按此处签名. 如果仍然出现(os/kern)故障(0x5)错误,只需运行sudo gdb.

Don't forget to sign gdb as explain here. If you still get the (os/kern) failure (0x5) error, just run sudo gdb.

这是一个临时解决方案,等待GNU团队直接在存储库中解决问题.

This is a temporary solution waiting for the GNU team fix the problem directly on the repo.

编辑

Binutils-gdb已更新,现在已在commit fc7b364 .

Binutils-gdb has been updated, these changes are now implemented in commit fc7b364.

希望这会有所帮助.

这篇关于gdb 8.2无法识别macOS Mojave 10.14上的可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆