binutils-gdb/gdb/maint.h
Tom de Vries 545e49f5ee [gdb/tdep] Fix inferior plt calls in PIE for i386
Consider test-case test.c:
...
int main (void) {
  void *p = malloc (10);
  return 0;
}
...

When compiled to a non-PIE exec:
...
$ gcc -m32 test.c
...
the call sequence looks like:
...
 8048447:       83 ec 0c                sub    $0xc,%esp
 804844a:       6a 0a                   push   $0xa
 804844c:       e8 bf fe ff ff          call   8048310 <malloc@plt>
...
which calls to:
...
08048310 <malloc@plt>:
 8048310:       ff 25 0c a0 04 08       jmp    *0x804a00c
 8048316:       68 00 00 00 00          push   $0x0
 804831b:       e9 e0 ff ff ff          jmp    8048300 <.plt>
...
where the first insn at 0x8048310 initially jumps to the following address
0x8048316, read from the .got.plt @ 0x804a00c:
...
 804a000 0c9f0408 00000000 00000000 16830408  ................
 804a010 26830408                             &...
...

Likewise, when compiled as a PIE:
...
$ gcc -m32 -fPIE -pie test.c
...
we have this call sequence (with %ebx setup to point to the .got.plt):
...
0000055d <main>:
 579:   83 ec 0c                sub    $0xc,%esp
 57c:   6a 0a                   push   $0xa
 57e:   89 c3                   mov    %eax,%ebx
 580:   e8 6b fe ff ff          call   3f0 <malloc@plt>
...
which calls to:
...
000003f0 <malloc@plt>:
 3f0:   ff a3 0c 00 00 00       jmp    *0xc(%ebx)
 3f6:   68 00 00 00 00          push   $0x0
 3fb:   e9 e0 ff ff ff          jmp    3e0 <.plt>
...
where the insn at 0x3f0 initially jumps to following address 0x3f6, read from
the .got.plt at offset 0xc:
...
 2000 f41e0000 00000000 00000000 f6030000  ................
 2010 06040000                             ....
...

When instead doing an inferior call to malloc (with nosharedlib to force
malloc to resolve to malloc@plt rather than the functions in ld.so or libc.so)
with the non-PIE exec, we have the expected:
...
$ gdb -q -batch a.out -ex start -ex nosharedlib -ex "p /x (void *)malloc (10)"
Temporary breakpoint 1 at 0x8048444

Temporary breakpoint 1, 0x08048444 in main ()
$1 = 0x804b160
...

But with the PIE exec, we run into:
...
$ gdb -q -batch a.out -ex start -ex nosharedlib -ex "p /x (void *)malloc (10)"
Temporary breakpoint 1 at 0x56c

Temporary breakpoint 1, 0x5655556c in main ()

Program received signal SIGSEGV, Segmentation fault.
0x565553f0 in malloc@plt ()
...

The segfault happens because:
- the inferior call mechanism doesn't setup %ebx
- %ebx instead is 0
- the jump to "*0xc(%ebx)" reads from memory at 0xc

Fix this by setting up %ebx properly in i386_thiscall_push_dummy_call.

Fixes this failure with target board unix/-m32/-pie/-fPIE reported in
PR28467:
...
FAIL: gdb.base/nodebug.exp: p/c (int) array_index("abcdef",2)
...

Tested on x86_64-linux, with target board unix/-m32 and unix/-m32/-fPIE/-pie.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28467
2021-12-07 08:07:18 +01:00

70 lines
2.2 KiB
C++

/* Support for GDB maintenance commands.
Copyright (C) 2013-2021 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef MAINT_H
#define MAINT_H
#include "gdbsupport/run-time-clock.h"
#include <chrono>
extern void set_per_command_time (int);
extern void set_per_command_space (int);
/* Records a run time and space usage to be used as a base for
reporting elapsed time or change in space. */
class scoped_command_stats
{
public:
explicit scoped_command_stats (bool msg_type);
~scoped_command_stats ();
private:
DISABLE_COPY_AND_ASSIGN (scoped_command_stats);
/* Print the time, along with a string. */
void print_time (const char *msg);
/* Zero if the saved time is from the beginning of GDB execution.
One if from the beginning of an individual command execution. */
bool m_msg_type;
/* Track whether the stat was enabled at the start of the command
so that we can avoid printing anything if it gets turned on by
the current command. */
int m_time_enabled : 1;
int m_space_enabled : 1;
int m_symtab_enabled : 1;
run_time_clock::time_point m_start_cpu_time;
std::chrono::steady_clock::time_point m_start_wall_time;
long m_start_space;
/* Total number of symtabs (over all objfiles). */
int m_start_nr_symtabs;
/* A count of the compunits. */
int m_start_nr_compunit_symtabs;
/* Total number of blocks. */
int m_start_nr_blocks;
};
extern obj_section *maint_obj_section_from_bfd_section (bfd *abfd,
asection *asection,
objfile *ofile);
#endif /* MAINT_H */