Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

gdb 8.2 can't recognized executable file on macOS Mojave 10.14

I get gdb by brew install gdb.

The source file content is:

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

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

I compile the source file like this:

c++ -g -o demo demo.cpp

And run 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

I use file demo,its ouput is demo: Mach-O 64-bit executable x86_64

I use file ./demo,its output is ./demo: Mach-O 64-bit executable x86_64

Type c++ -v, output is :

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

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 !!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that clang-1000.11.45.2 distributed with Apple LLVM version 10.0.0 adds 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
...

From the apple 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.
 */

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

As a temporary solution, you can edit the bfd sources code provides with gdb.

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;

Finally inside gdb-8.0.1/include/mach-o/loader.h at line 189:

  BFD_MACH_O_LC_BUILD_VERSION = 0x32

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

Then process a classic gdb compilation following instructions from 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

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

This is a temporary solution in order to wait for a fix from GNU team.

EDIT

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

Hope It will be helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...