From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id sPiEMRMie2AGOwAAWB0awg (envelope-from ) for ; Sat, 17 Apr 2021 13:59:47 -0400 Received: by simark.ca (Postfix, from userid 112) id C87C81F10E; Sat, 17 Apr 2021 13:59:47 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id DFBF81EE0E for ; Sat, 17 Apr 2021 13:59:45 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 37B8E394FC03; Sat, 17 Apr 2021 17:59:45 +0000 (GMT) Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by sourceware.org (Postfix) with ESMTPS id 1F1FB3938C08 for ; Sat, 17 Apr 2021 17:59:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1F1FB3938C08 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=jimw@sifive.com Received: by mail-pj1-x1032.google.com with SMTP id k23-20020a17090a5917b02901043e35ad4aso18106194pji.3 for ; Sat, 17 Apr 2021 10:59:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=lZyggnMja3O8ri6C+n6ppg10laC8ysw2IIj/UGeQB7g=; b=DVaAVxH/G0PWRg6eBrMzhNo4uz3mPzrF/2u3q05Z2wVFXDJ0Ffyzj7TNNKR3Ifn4G3 LtTanexosNeKUGrf0QyEZ/TnL42QKY6360ZfXAl2Dd1vW+KKFqLqDJEB16givD98n3kW BhTdhgWiIOIviwqHToQsQvLHzQ0TnpsbKd5+05iCaJYKf3n6BzJWA5wyIs1nhSViM8Ly tZElT1J7tOOY3yRJyf2VrUYXCXg2RFkvKgAbY+jDSbSmdWbRYLkkGXRwDS9hAnBiirh/ cZLQU1okAPWNjGrQVzRyh/xKRL6d8UJjFa2PxF7lnSo9a5LWIO3ZVAbdRup94T1F0kNI nVIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=lZyggnMja3O8ri6C+n6ppg10laC8ysw2IIj/UGeQB7g=; b=hT/TeZl073OylhkZ7++UivHGECwinIyxE0Ov//MgQNEOX1AArz8KJfQkFnLEu0ecw/ Svq40ftpsT2PPCqzwzpClUeoTU0Ec1YZCx852MzJqYvbu1CschdQSvAvswg6dvFz+IZ3 9lf0oSY8TWH/YUb1oBIwsJcQGUgGadrdjJpc5X9hoX3/L77vBF0Bkix1VgmqAqfp3rqU i8RyDaCYBSAb9JlQw1jy3JhRrIBVYPLaStK7F8wSwTLAqUCFqfrY6gzvj5EqgYGmhqdg MhYS9swEONjSCpMrCzmnABfkJ4JsnhiNp/W/tSKPrxD/ZTtY6jXwkaMf1kC7C2wrcXvZ RY/Q== X-Gm-Message-State: AOAM532sQIIGxXBWmWbLC+Ngkac64u8OKnX4k6Nfsna4HZXzuSD1bJwL TlDkSLgceDCcCLZbmQQaPOvVHyK5eVdaQw== X-Google-Smtp-Source: ABdhPJy/edptH/azC4f+G+4KDg9jgmwKc3n06qTYNkg5pgM6hORVcC0UdQizpkfoDhP8Rik3zTxF0g== X-Received: by 2002:a17:902:e549:b029:e6:6b3a:49f7 with SMTP id n9-20020a170902e549b02900e66b3a49f7mr15914031plf.52.1618682365945; Sat, 17 Apr 2021 10:59:25 -0700 (PDT) Received: from rohan.hsd1.ca.comcast.net ([2601:646:c180:b150:1820:3ed4:975:3fbe]) by smtp.gmail.com with ESMTPSA id u4sm5030705pfk.56.2021.04.17.10.59.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Apr 2021 10:59:25 -0700 (PDT) From: Jim Wilson To: gdb-patches@sourceware.org Subject: [PATCH 20/24] RISC-V sim: Set brk to _end if possible. Date: Sat, 17 Apr 2021 10:58:27 -0700 Message-Id: <20210417175831.16413-21-jimw@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210417175831.16413-1-jimw@sifive.com> References: <20210417175831.16413-1-jimw@sifive.com> X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kito Cheng Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" From: Kito Cheng Search the symbol table for the address of _end and use it to initialize brk. Otherwise, set it to the end of the section with the highest address. sim/riscv/ * interp.c (riscv_get_symbol): New. (sim_create_inferior): Call riscv_get_symbol to get address of _end. Use it to set cpu->endbrk. --- sim/riscv/interp.c | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/sim/riscv/interp.c b/sim/riscv/interp.c index c98f6ab..7af97d5 100644 --- a/sim/riscv/interp.c +++ b/sim/riscv/interp.c @@ -127,14 +127,35 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, return sd; } +/* Search the symbol table for the symbol SYM, and return its address. */ + +static bfd_vma +riscv_get_symbol (SIM_DESC sd, const char *sym) +{ + long symcount = STATE_PROG_SYMS_COUNT (sd); + asymbol **symtab = STATE_PROG_SYMS (sd); + int i; + + for (i = 0;i < symcount; ++i) + { + if (strcmp (sym, bfd_asymbol_name (symtab[i])) == 0) + { + bfd_vma sa; + sa = bfd_asymbol_value (symtab[i]); + return sa; + } + } + + /* Symbol not found. */ + return 0; +} + SIM_RC sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char * const *argv, char * const *env) { SIM_CPU *cpu = STATE_CPU (sd, 0); SIM_ADDR addr; - Elf_Internal_Phdr *phdr; - int i, phnum; /* Set the PC. */ if (abfd != NULL) @@ -143,13 +164,22 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd, addr = 0; sim_pc_set (cpu, addr); - /* Set endbrk to highest section end address. */ - phdr = elf_tdata (abfd)->phdr; - phnum = elf_elfheader (abfd)->e_phnum; - for (i = 0; i < phnum; i++) + /* Try to find _end symbol, and set it to the end of brk. */ + trace_load_symbols (sd); + cpu->endbrk = riscv_get_symbol (sd, "_end"); + + /* If not found, set end of brk to end of all section. */ + if (cpu->endbrk == 0) { - if (phdr[i].p_paddr + phdr[i].p_memsz > cpu->endbrk) - cpu->endbrk = phdr[i].p_paddr + phdr[i].p_memsz; + Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr; + int phnum = elf_elfheader (abfd)->e_phnum; + int i; + + for (i = 0; i < phnum; i++) + { + if (phdr[i].p_paddr + phdr[i].p_memsz > cpu->endbrk) + cpu->endbrk = phdr[i].p_paddr + phdr[i].p_memsz; + } } /* Standalone mode (i.e. `run`) will take care of the argv for us in -- 2.7.4