1

I have an ELF executable file named app2 which is compiled under Linux, and it can not be debugged by GDB on a Mac. But there is no problem by using LLDB.

Peterx:Documents Peter$ file app2
app2: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, not stripped

and the gdb information:

Peterx:Documents Peter$ gdb app2
GNU gdb (GDB) 7.7
Copyright (C) 2014 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-darwin13.1.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"...
"/Users/Peter/Documents/app2": not in executable format: File format not recognized

Peterx:Documents Peter$ ./app2
-bash: ./app2: Permission denied

Set a breakpoint and then delete it.

Peterx:Documents Peter$ lldb app2
Current executable set to 'app2' (x86_64).
(lldb) b main
Breakpoint 1: where = app2`main + 8 at app2.c:4, address = 0x00000000004004fc
(lldb) br del 1
1 breakpoints deleted; 0 breakpoint locations disabled.
(lldb) quit
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter Pop
  • 11
  • 1
  • 3
  • 1
    Where do you use LLDB, on Mac or on Linux? ELF files are not portable between different OSes, so you can't run the ELF file from Linux (`app2`) on MAC OS X (it is different kind of unix). You need to recompile it for MAC OS X. – osgx Apr 26 '14 at 19:07
  • I created the (app2) on Linux and then used the GDB and LLDB on MAC OS.I wonder why it can run by LLDB but fail by GDB. – Peter Pop Apr 27 '14 at 14:37
  • I believe that OSX's LLDB should not execute linux application (e.g. http://stackoverflow.com/a/9439548/196561). Can you start your app2 on OSX without debugger? Can you show debugging session with lldb? – osgx Apr 27 '14 at 16:31
  • Sure. And you are right that it can not run on OSX. – Peter Pop Apr 28 '14 at 14:29
  • 1
    Do you still want to answer to this question, given you cannot execute ELF binaries under OSX? – trojanfoe Apr 28 '14 at 14:52

2 Answers2

2

Linux and Mac OS X (OS X) have different executable formats (ELF in linux, Mach-O in OSX) and use different ABI. So you can't run executable file from linux under OSX or executable file from OSX under linux. The only variant is to use simulators like qemu, virtualbox, or parallels, and install right OS into virtual machine.

But there is no problem by using LLDB.

Actually there are problems with LLDB. First problem: LLDB does open ELF binaries (it can parse ELF format) without check of ELF header. Your ELF is from Linux, it has incompatible OS_ABI field in header. I think LLDB should fail or print warning early. This can be reported to LLDB bug tracker.

Second problem: there is actually no debugging in your log:

Peterx:Documents Peter$ lldb app2
Current executable set to 'app2' (x86_64).
(lldb) b main
Breakpoint 1: where = app2`main + 8 at app2.c:4, address = 0x00000000004004fc
(lldb) br del 1
1 breakpoints deleted; 0 breakpoint locations disabled.
(lldb) quit

Only symbols are read from ELF, but there is still no run of app2. LLDB will not run app2, and this log is not using LLDB as real debugger, but using it as nm or objdump to get symbol addresses, read debug info and disassemble code.

Your gdb is configured with only Mach-O binary support, and your version just can't parse ELF binary. If you will configure gdb with ELF reading support, it may load and parse ELF from linux. Possibly it will show you sybmols from file, parse debuginfo and disassemble functions. But it still can't change ABI and will not run the app2.

osgx
  • 90,338
  • 53
  • 357
  • 513
  • +1 As an aside, Jonathan Levin, the author of *Mac OS X and IOS Internals: To the Apple's Core* has speculated whether Apple will start using ELF in an upcoming version of OSX/iOS. I cannot see the advantage of changing, myself, but interesting nevertheless. – trojanfoe Apr 28 '14 at 15:05
  • trojanfoe, LLDB can just support ELF to work on Linux. And I think it can follow generic principles of LLVM to have all platforms and formats supported in default build; while `gdb` is more like `gcc` - there are dozens of platforms and formats, and they can't be enabled at same time. The gcc and gdb ideology is to build specific version of tool for each platform (or pair of platforms in case of cross-complation/cross-debugging). – osgx Apr 28 '14 at 15:08
  • @PeterPop, if you problem is solved and no more answers needed, you can "accept" my answer by clicking "v"-shaped symbol left from it. – osgx Apr 28 '14 at 16:23
  • osgx has it. gdb's architecture support is done with preprocessor macros and similar compile time tricks, so you can't build a single gdb binary that supports multiple architectures. lldb uses a plugin mechanism to support different architectures and OS'es, so it can be built to support many architectures in one binary. Both debuggers can do cross architecture debugging (read an ELF file on MacOS, then connect to a Linux machine and remotely debug the binary running there.) But for gdb you would have to build the right gdb to do this. – Jim Ingham Sep 20 '18 at 20:54
2

In addition to what folks have said above, note that you actually could use lldb on a mac to "cross debug" to a linux machine. For instance, on the Linux box you could use gdbserver to start up or attach to your app, and then connect to that debugserver using lldb's gdb-remote command to actually debug it. This might be handy if you wanted to do two machine debugging, but only had one Mac & one Linux box.

So it is not a bug that lldb will read in and set breakpoints on your ELF file, that's intended behavior. Of course, "run" is not going to do anything useful, since the binary won't run natively.

Finally, there is a port of lldb for Linux and some of the BSD's which a number of folks are working on.

Jim Ingham
  • 25,260
  • 2
  • 55
  • 63