0

I'm trying to run a very simple linked file in GDB.

I did the following actions:

(gdb) break _start
Breakpoint 1 at 0x4000b0
(gdb) start
Function "main" not defined.
**Make breakpoint pending on future shared library load? (y or [n]) ?**

However the label _start isn't in any shared library, but simply in the assembly code that generated the executable. Why does GDB ask this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Koinos
  • 151
  • 3
  • 14

1 Answers1

5

The GDB start command sets a breakpoint at the start of main, and then runs your program. Presumably your program doesn't have a main symbol.

Read the error message carefully, it says Function "main" not defined. The breakpoint at _start is already set; if that was the problem you'd have seen the prompt after that command, and wouldn't have seen a numeric address. (Try it with b xyz or b printf.)


You're looking for the run command, which just runs the program without looking for any symbols in it.

Use help start and help run in GDB to see what they do.

Or the online docs: https://sourceware.org/gdb/onlinedocs/gdb/Starting.html

  • run: just start the program, no new breakpoints added.
  • start: set a one-time breakpoint in main before run. Useful for compiled C/C++, or hand-written asm that defines a main and uses the standard CRT startup files. This is what you tried to use.
  • starti: stop at the first asm instruction of the process. Especially useful for a PIE executable without symbols, where the entry-point numeric address isn't known until after ASLR chooses it, if you run it with ASLR enabled. (The GDB default behaviour is to disable ASLR).

starti is a relatively recent feature; before that one common hack was to use b *0, which leads to an error right after the process starts, before any instructions execute. Stopping at the first machine code instruction in GDB

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • (I'm sorry if i ask this, but the online resources for Assembly seem very few) Let's suppose that after typing "start" i made a breakpoint pending on future shared library load. If i load a shared library, when does the compiler go to check if the main label is present ? Does the library get simply copy-pasted at the beginning of the source file that loaded it ? – Koinos Nov 07 '18 at 07:04
  • @Koinos: This isn't an assembly issue, it's a shared-library / linking issue. But anyway, the compiler isn't doing any checking of anything when you're debugging with GDB. If your executable itself didn't contain a `main` function, but was linked against a shared library that did, then it would make sense. `main` is not a normal use-case for this, but it's the same GDB prompt as for setting breakpoints on other functions, like `printf`. – Peter Cordes Nov 07 '18 at 07:15