首页 > 解决方案 > gdb 8.2 在 macOS Mojave 10.14 上无法识别可执行文件

问题描述

我通过brew install 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

我像这样编译源文件:

c++ -g -o demo demo.cpp

并运行 gdb

gdb ./demo

但是,它行不通。它无法识别可执行文件。

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,它的输出是gdb中的55 类型,它显示:show configuration

 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)

谁能帮我 ?非常感谢你 !!!

标签: macosgdb

解决方案


问题是clang-1000.11.45.2分布式Apple LLVM version 10.0.0添加了一个新的加载命令到名为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 用来操作可执行文件的程序)无法解释这个命令并返回错误。

作为临时解决方案,您可以编辑bfdgdb.

首先,从镜像gdb-8.0.1下载源。然后在 line 处添加以下代码:gdb-8.0.1/bfd/mach-o.c4649

case BFD_MACH_O_LC_BUILD_VERSION:
break;

最后gdb-8.0.1/include/mach-o/loader.h在行内189

  BFD_MACH_O_LC_BUILD_VERSION = 0x32

不要忘记,在 188 行的末尾添加 a BFD_MACH_O_LC_VERSION_MIN_WATCHOS = 0x30)。

然后按照 README 中的说明处理经典gdb编译:

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

不要忘记按照这里gdb的说明签名。如果您仍然收到 (os/kern) failure (0x5) 错误,只需运行.sudo gdb

这是一个临时解决方案,以便等待 GNU 团队的修复。

编辑

Binutils-gdb已更新,这些更改现在在提交fc7b364中实现。

希望它会有所帮助。


推荐阅读