From patchwork Wed Jul 29 18:30:37 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sandra Loosemore X-Patchwork-Id: 7919 Received: (qmail 91191 invoked by alias); 29 Jul 2015 18:32:38 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 91170 invoked by uid 89); 29 Jul 2015 18:32:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 29 Jul 2015 18:32:35 +0000 Received: from svr-orw-fem-03.mgc.mentorg.com ([147.34.97.39]) by relay1.mentorg.com with esmtp id 1ZKW9Y-0001t4-Hn from Sandra_Loosemore@mentor.com ; Wed, 29 Jul 2015 11:32:32 -0700 Received: from [IPv6:::1] (147.34.91.1) by svr-orw-fem-03.mgc.mentorg.com (147.34.97.39) with Microsoft SMTP Server id 14.3.224.2; Wed, 29 Jul 2015 11:32:32 -0700 Message-ID: <55B91BCD.9060203@codesourcery.com> Date: Wed, 29 Jul 2015 12:30:37 -0600 From: Sandra Loosemore User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130329 Thunderbird/17.0.5 MIME-Version: 1.0 To: CC: Yao Qi Subject: [patch, nios2] update trap/break handling in prologue analyzer This patch updates the nios2 prologue analyzer to handle some recent-ish GCC code generation changes: * In GCC r223083, the instruction emitted when a stack overflow is detected and in other trap situations was changed from "break" to "trap". This patch tweaks the prologue analyzer to recognize both the old and new stack checking sequences. * In GCC r225787, the stack overflow checking logic was changed so that now there is only a single check being emitted instead of one after each stack adjustment. This doesn't require code changes to GDB, but I tidied up the comments that explain what can appear in the prologue. * It seemed to me to be an oversight that "trap" and "break" instructions not related to stack overflow checking don't indicate that we've advanced past the end of the prologue, as do control transfer instructions. GCC only generates these from calls to __builtin_trap() or in situations where it has detected some other trap situation with undefined behavior, like a null pointer dereference, which shouldn't appear in prologue code. OK to commit? -Sandra diff --git a/gdb/nios2-tdep.c b/gdb/nios2-tdep.c index 1968a88..1c5dcde 100644 --- a/gdb/nios2-tdep.c +++ b/gdb/nios2-tdep.c @@ -697,22 +697,29 @@ nios2_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc) stw sp, constant(rx) mov sp, rx - 5) A frame pointer save, which can be either a MOV or ADDI. + 4) A frame pointer save, which can be either a MOV or ADDI. - 6) A further stack pointer adjustment. This is normally included - adjustment in step 4 unless the total adjustment is too large + 5) A further stack pointer adjustment. This is normally included + adjustment in step 3 unless the total adjustment is too large to be done in one step. 7) A stack overflow check, which can take either of these forms: bgeu sp, rx, +8 - break 3 + trap 3 or bltu sp, rx, .Lstack_overflow ... .Lstack_overflow: - break 3 - If present, this is inserted after the stack pointer adjustments - for steps 3, 4, and 6. + trap 3 + + Older versions of GCC emitted "break 3" instead of "trap 3" here, + so we check for both cases. + + Older GCC versions emitted stack overflow checks after the SP + adjustments in both steps 3 and 4. Starting with GCC 6, there is + at most one overflow check, which is placed before the first + stack adjustment for R2 CDX and after the first stack adjustment + otherwise. The prologue instructions may be combined or interleaved with other instructions. @@ -995,14 +1002,15 @@ nios2_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR start_pc, else if (cond == branch_geu) { /* BGEU sp, rx, +8 - BREAK 3 + TRAP 3 (or BREAK 3) This instruction sequence is used in stack checking; we can ignore it. */ unsigned int next_insn; const struct nios2_opcode *next_op = nios2_fetch_insn (gdbarch, pc, &next_insn); if (next_op != NULL - && nios2_match_break (next_insn, op, mach, &uimm)) + && (nios2_match_trap (next_insn, op, mach, &uimm) + || nios2_match_break (next_insn, op, mach, &uimm))) pc += next_op->size; else break; @@ -1010,13 +1018,14 @@ nios2_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR start_pc, else if (cond == branch_ltu) { /* BLTU sp, rx, .Lstackoverflow - If the location branched to holds a BREAK 3 instruction - then this is also stack overflow detection. */ + If the location branched to holds a TRAP or BREAK + instruction then this is also stack overflow detection. */ unsigned int next_insn; const struct nios2_opcode *next_op = nios2_fetch_insn (gdbarch, pc + imm, &next_insn); if (next_op != NULL - && nios2_match_break (next_insn, op, mach, &uimm)) + && (nios2_match_trap (next_insn, op, mach, &uimm) + || nios2_match_break (next_insn, op, mach, &uimm))) ; else break; @@ -1025,11 +1034,13 @@ nios2_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR start_pc, break; } - /* All other calls or jumps (including returns) terminate + /* All other calls, jumps, returns, TRAPs, or BREAKs terminate the prologue. */ else if (nios2_match_callr (insn, op, mach, &ra) || nios2_match_jmpr (insn, op, mach, &ra) - || nios2_match_jmpi (insn, op, mach, &uimm)) + || nios2_match_jmpi (insn, op, mach, &uimm) + || nios2_match_trap (insn, op, mach, &uimm) + || nios2_match_break (insn, op, mach, &uimm)) break; }