【问题标题】:gdb 8.2 can't recognized executable file on macOS Mojave 10.14gdb 8.2 在 macOS Mojave 10.14 上无法识别可执行文件
【发布时间】:2018-09-27 05:13:31
【问题描述】:

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

我这样编译源文件:

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,它的输出是55 在 gdb 中输入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)

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

【问题讨论】:

  • 你在用什么gdb?你是如何得到的?你有没有从sourceware.org/gdb/download下载它的源代码并编译它?如果是,您是如何配置的?如果否,则在gdb 中显示show configuration 的输出。同样对于您的c++(是GCCClang,....)?显示c++ -v 的输出。你能在同一个终端运行./demo 吗? file ./demo 的输出是什么?
  • 可能显示demo.cpp 的来源(或将其设为很小的minimal reproducible example)。首先尝试使用 hello-world 之类的示例
  • 这听起来很像sourceware.org/bugzilla/show_bug.cgi?id=13157,只是在 8.2 中已修复。另请注意,有一些 macOS 修复程序仅在 git master 上 - 至少从 High Sierra 开始需要它们。
  • 另外,我认为在 gdb 上工作的任何人都没有尝试过 Mojave。提交一个 gdb 错误会很棒。更好的是在失败的地方附加一个“hello world”类型的可执行文件。
  • gdb 8.0 和 8.2 都试过了,同样的问题

标签: macos gdb


【解决方案1】:

问题是clang-1000.11.45.2Apple LLVM version 10.0.0 一起分发,向名为LC_BUILD_VERSION 的o-mach 可执行文件添加了一个新的加载命令。

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

来自苹果source

/*
 * 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 用来操作可执行文件的程序)无法解释此命令并返回错误。

作为临时解决方案,您可以编辑bfd 提供的gdb 源代码。

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

case BFD_MACH_O_LC_BUILD_VERSION:
break;

最后在gdb-8.0.1/include/mach-o/loader.h189 行内:

  BFD_MACH_O_LC_BUILD_VERSION = 0x32

别忘了在BFD_MACH_O_LC_VERSION_MIN_WATCHOS = 0x30之后的第188行末尾添加,

然后按照自述文件中的说明处理经典的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 解释here。 如果您仍然收到 (os/kern) failure (0x5) 错误,只需运行 sudo gdb

这是等待 GNU 团队修复的临时解决方案。

编辑

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

希望对您有所帮助。

【讨论】:

  • 如何使用你编辑的提交?我已经下载了 gdb-8.2 源文件并尝试修改为已提交。但是有些文件我找不到。
  • 问题来自bfd 而不是直接来自gdbbfd 存在于binutils 包中。您可以通过 git git clone git://sourceware.org/git/binutils-gdb.git 获取binutils。按照readme编译安装binutils,我使用的是gdb 8.0.1,避免出现兼容性问题。
  • 有人通知brew这个更新吗?更新:是的,discourse.brew.sh/t/…
  • 我们可能想在 GitHub 上提出问题:github.com/Homebrew/brew/issues
  • ? Homebrew 已修复,无需额外步骤。
【解决方案2】:

我发布了一个似乎有效的临时冲泡配方,同时等待官方冲泡配方更新:

酿造安装https://raw.githubusercontent.com/timotheecour/homebrew-timutil/master/gdb_tim.rb

【讨论】:

    【解决方案3】:

    升级到 GDB 版本 8.3。另请参阅 Binutils 错误跟踪器中的 Issue 23728, binutils fail on macOS 10.14 (Mojave) due to unimpl

    来自bug report

    我找到了问题的根源。 binutils 不处理负载 命令 0x32 LC_BUILD_VERSION(实际上也不是 0x31 LC_NOTE)。他们是 在最近的 LLVM 版本中定义:见 https://github.com/llvm-mirror/llvm/blob/master/include/llvm/BinaryFormat/MachO.def#L77

    查看 objdump -private-headers 的输出有一个明确的 区别:

    @@ -56,16 +56,18 @@ attributes NO_TOC STRIP_STATIC_SYMS LIVE
      reserved1 0
      reserved2 0
     Load command 1
    -      cmd LC_VERSION_MIN_MACOSX
    -  cmdsize 16
    -  version 10.13
    -      sdk n/a
    +       cmd LC_BUILD_VERSION
    +   cmdsize 24
    +  platform macos
    +       sdk n/a
    +     minos 10.14
    +    ntools 0
     Load command 2
          cmd LC_SYMTAB
      cmdsize 24
    

    LC_VERSION_MIN_MACOSX 在 binutils 中实现,而 LC_BUILD_VERSION 不是。这显然是莫哈韦的新事物。

    【讨论】:

    • GDB 8.3 尚未发布。升级是指从最新源编译吗?
    【解决方案4】:

    我从堆栈溢出中得到了一个很好的解决方案,但我不知道它为什么会起作用。 这是link

    我是 macOS 的新手,我会执行以下操作:

    1. 在高山脉中协同设计 gdb 8.0.1
    2. Mojave 更新
    3. gdb 8.0.1 死于BFD: /Users/xxx/Codes/demo: unknown load command 0x32
    4. 更改为 gdb 8.2.1 并遇到钥匙串错误Unknown Error -2,147,414,007

      通过在Login 中获取证书然后将其导出并将其导入System 来解决此问题(如果无法导入,请从登录中删除)。

    5. 最后,由于某种错误它仍然无法正常工作并且它带有ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". Unable to find Mach task port for process-id 1510: (os/kern) failure (0x5). (please check gdb is codesigned - see taskgated(8)),根据how to undo codesign,仍然存在一些错误,答案告诉我brew reinstall gdb,但它仍然无法正常工作,我昨天收工了。
    6. 终于遇到that link,我很开心,现在可以调试了!

    希望我的解决方案能有所帮助。

    【讨论】:

      【解决方案5】:

      我让 gdb 在 Mojave 上工作:

      a) 获取最新的 gdb 源存档(在撰写本文时,ftp://sourceware.org/pub/gdb/snapshots/current/gdb-weekly-8.2.50.20190212.tar.xz) - 除其他外,它增加了识别 Mac 上的可执行文件的处理。

      b) 构建 gdb。我在 darwin-nat.c 中遇到了变量阴影的错误,所以我编辑了文件并重建了。

      c) 按照https://forward-in-code.blogspot.com/2018/11/mojave-vs-gdb.html中的步骤进行操作

      瞧。

      (来源:GDB on Mac/Mojave: During startup program terminated with signal ?, Unknown signal

      【讨论】:

        【解决方案6】:

        从 Homebrew 安装的 gdb 8.2 与 Mac mojave 不兼容。 我已经升级到 8.2.1。这个问题应该得到解决。

        【讨论】:

          【解决方案7】:

          上面 timotheecour 的回答确实对我有用:

          酿造安装https://raw.githubusercontent.com/timotheecour/homebrew-timutil/master/gdb_tim.rb

          然后我必须生成一个生成自签名证书,如 https://www.thomasvitale.com/how-to-setup-gdb-and-eclipse-to-debug-c-files-on-macos-sierra/

          【讨论】:

            【解决方案8】:

            我通过精简应用程序在 Mojave 上解决了这个问题。 GDB 不理解通用二进制文件。所以如果file myapp 告诉你 myapp 是一个通用二进制文件,试试这个:

            lipo -thin x86_64 -output myapp-x86_64 myapp
            

            然后

            gdb myapp-x86_64
            

            【讨论】:

            • 我也没有。更具体地说,我收到此消息:致命错误:/Library/Developer/CommandLineTools/usr/bin/lipo: input file (a.out) must be a fat file when the -thin option is specified
            猜你喜欢
            • 1970-01-01
            • 2019-03-02
            • 1970-01-01
            • 2019-06-15
            • 1970-01-01
            • 1970-01-01
            • 2019-06-22
            • 2016-01-09
            • 1970-01-01
            相关资源
            最近更新 更多