From: paul-naert <paul_naert@hotmail.fr>
To: gdb-patches@sourceware.org
Cc: paul-naert <paul_naert@hotmail.fr>
Subject: [RFC][PATCH 6/7] Convert short jumps to long ones in amd64_relocate_instruction
Date: Thu, 14 May 2020 10:08:48 -0400 [thread overview]
Message-ID: <20200514140849.31845-7-paul_naert@hotmail.fr> (raw)
In-Reply-To: <20200514140849.31845-1-paul_naert@hotmail.fr>
When relocating a short jump, the range is often
too short to correctly point to the original position from the displaced one.
We replace short jump instructions by 5 bytes jumps, meaning we now have a
32bit possible offset instead of 8.
---
gdb/amd64-tdep.c | 33 ++++++++++++++++++++++++++++++---
1 file changed, 30 insertions(+), 3 deletions(-)
diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c
index ca9b909..a1604a8 100644
--- a/gdb/amd64-tdep.c
+++ b/gdb/amd64-tdep.c
@@ -1835,6 +1835,8 @@ amd64_relocate_instruction (struct gdbarch *gdbarch,
gdb_byte *buf = (gdb_byte *) xmalloc (len + fixup_sentinel_space);
struct amd64_insn insn_details;
int offset = 0;
+ int jmp_arg_len = 4;
+ int opcode_len_diff = 0;
LONGEST rel32, newrel;
gdb_byte *insn;
int insn_length;
@@ -1924,19 +1926,44 @@ amd64_relocate_instruction (struct gdbarch *gdbarch,
offset = rip_relative_offset (&insn_details);
if (!offset)
{
+ /* Replace short jumps by long ones */
+ if (insn[0] == 0xeb)
+ {
+ jmp_arg_len = 1;
+ insn[0] = 0xe9;
+ insn_length = 5;
+ }
/* Adjust jumps with 32-bit relative addresses. Calls are
already handled above. */
if (insn[0] == 0xe9)
offset = 1;
+
+ /* Replace short conditional jumps by long ones */
+ if ((insn[0] & 0xf0) == 0x70)
+ {
+ jmp_arg_len = 1;
+ insn[2] = insn[1];
+ insn[1] = insn[0] + 0x10;
+ insn[0] = 0x0f;
+ insn_length = 6;
+ opcode_len_diff = 1;
+ }
/* Adjust conditional jumps. */
- else if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
+ if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
offset = 2;
}
if (offset)
{
- rel32 = extract_signed_integer (insn_details.raw_insn + offset, 4, byte_order);
- newrel = (oldloc - *to) + rel32;
+ rel32 = extract_signed_integer (insn_details.raw_insn + offset, jmp_arg_len, byte_order);
+ /* The length of the opcode and offset of the jump may be different. */
+ newrel = (oldloc - *to) + rel32 - (4 + opcode_len_diff - jmp_arg_len);
+ if (newrel < INT32_MIN || newrel > INT32_MAX)
+ {
+ /* Overflowing the 32 bit jump */
+ if (debug_displaced)
+ error (_ ("Overflowing of int32 for jump instruction relocation"));
+ }
store_signed_integer (insn_details.raw_insn + offset, 4, byte_order, newrel);
if (debug_displaced)
fprintf_unfiltered (gdb_stdlog,
--
2.7.4
next prev parent reply other threads:[~2020-05-14 14:10 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-14 14:08 [RFC][PATCH 0/7] Patching new code in a binary paul-naert
2020-05-14 14:08 ` [RFC][PATCH 1/7] Add addr argument to infcall_mmap paul-naert
2020-05-14 14:08 ` [RFC][PATCH 2/7] Change memory protection for compile arguments to RW paul-naert
2020-05-22 19:18 ` Tom Tromey
2020-05-14 14:08 ` [RFC][PATCH 3/7] Make create_sals_from_location_default non static paul-naert
2020-05-14 14:08 ` [RFC][PATCH 4/7] Add address argument to compile_to_object paul-naert
2020-05-14 14:08 ` [RFC][PATCH 5/7] Add the option to defer register storage for compiled object loading paul-naert
2020-05-22 19:39 ` Tom Tromey
2020-05-14 14:08 ` paul-naert [this message]
2020-05-22 19:41 ` [RFC][PATCH 6/7] Convert short jumps to long ones in amd64_relocate_instruction Tom Tromey
2020-05-14 14:08 ` [RFC][PATCH 7/7] Add the patch command to dynamically inject code in the inferior paul-naert
2020-05-22 19:50 ` Tom Tromey
2020-05-22 19:57 ` [RFC][PATCH 0/7] Patching new code in a binary Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200514140849.31845-7-paul_naert@hotmail.fr \
--to=paul_naert@hotmail.fr \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).