From patchwork Thu Aug 19 04:07:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Hua X-Patchwork-Id: 44707 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0E962384841B for ; Thu, 19 Aug 2021 04:08:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0E962384841B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1629346092; bh=lNR3B0r4BpzFgoLBEvA/lrb+sWVrprWIlXmn0PL5gC0=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=mUbrFfIICEl4ZuMF89ZKUmwzYUYwyPUbnAmLxX8pl1rXcEKU9JxEGPKOx1oTW6kLP ElESeGaxCvjmlZZwDWaQGzimYiu3sECWluCXlkO4Y1QFey7egNDcwmzFHMAUnySddH u6M7sxcn5wJWT8zli8tb0ViVe4+t5hafoRYdkuA4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb2a.google.com (mail-yb1-xb2a.google.com [IPv6:2607:f8b0:4864:20::b2a]) by sourceware.org (Postfix) with ESMTPS id 955F4384BC32 for ; Thu, 19 Aug 2021 04:07:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 955F4384BC32 Received: by mail-yb1-xb2a.google.com with SMTP id i8so9716912ybt.7 for ; Wed, 18 Aug 2021 21:07:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to:cc; bh=lNR3B0r4BpzFgoLBEvA/lrb+sWVrprWIlXmn0PL5gC0=; b=oD6QUajYTw/vnNVgSviZvQnVQmM6irMcxYf9FbV4NbWz+0/AP0HZD/NcJo3B1mpY+y V8hcow/UKOveumuvViDlRoHjhUjXlBcD893NZmbi/wfN4uL3tmjZq8dsfCNwYWPQjMMf TJkNjFeidU5dr9NXmTqky67j7P+FGBHj5yXkqWWNBf/MPhyQTd7ZPtFuf6DfQsHaqUy8 E1/Oo710MOcVtd4mpCO+0q5EORA0zlNxfmi9O0+iKerUXw9A82SVEx8bRtaEI1/jdCWN HmOBXtEJegAFAuZvJ6X3IzIublvh2l4sOdrZWYbmOZU5xWCirx6gEk2mkeoPODsrxzST ZwgQ== X-Gm-Message-State: AOAM530CY+zpKZke9CzWsM/aLKM/X1eXPDmijV5j7yaJZIa1RpWmDrEC 9pfbmffP0qD4NhdhpnBQA4I2NhHn86xzvZIv2VAbmHedlh7TPW98 X-Google-Smtp-Source: ABdhPJzOuB2hscZDx6xmYfLMsei1udbKXvhEagfdR6THof4YgHO1VUQYkJL22WLUWHoe6skcShAnP8gusGNmF4/k8ng= X-Received: by 2002:a25:c583:: with SMTP id v125mr15911690ybe.4.1629346061063; Wed, 18 Aug 2021 21:07:41 -0700 (PDT) MIME-Version: 1.0 Date: Thu, 19 Aug 2021 12:07:30 +0800 Message-ID: Subject: [PATCH 8/14] [LoongArch] Linux Startup and Dynamic Loading Code To: libc-alpha@sourceware.org X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Paul Hua via Libc-alpha From: Paul Hua Reply-To: Paul Hua Cc: Xu Chenghua , huangpei@loongson.cn, caiyinyu@loongson.cn Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From 74fcaf6fef4f5e0780f7b25d08a9965fe500deb8 Mon Sep 17 00:00:00 2001 From: caiyinyu Date: Tue, 27 Jul 2021 15:59:12 +0800 Subject: [PATCH 08/14] LoongArch: Linux Startup and Dynamic Loading Code This contains the Linux-specific code for loading programs on LoongArch. * sysdeps/unix/sysv/linux/loongarch/dl-static.c: New file. * sysdeps/unix/sysv/linux/loongarch/ldsodefs.h: Likewise. --- sysdeps/unix/sysv/linux/loongarch/dl-static.c | 79 +++++++++++++++++++ sysdeps/unix/sysv/linux/loongarch/ldsodefs.h | 32 ++++++++ 2 files changed, 111 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/loongarch/dl-static.c create mode 100644 sysdeps/unix/sysv/linux/loongarch/ldsodefs.h diff --git a/sysdeps/unix/sysv/linux/loongarch/dl-static.c b/sysdeps/unix/sysv/linux/loongarch/dl-static.c new file mode 100644 index 0000000000..49d359a682 --- /dev/null +++ b/sysdeps/unix/sysv/linux/loongarch/dl-static.c @@ -0,0 +1,79 @@ +/* Copyright (C) 2021 Free Software Foundation, Inc. + + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library. If not, see + . */ + +#include + +#ifdef SHARED + +void +_dl_var_init (void *array[]) +{ + /* It has to match "variables" below. */ + enum + { + DL_PAGESIZE = 0 + }; + + GLRO (dl_pagesize) = *((size_t *) array[DL_PAGESIZE]); +} + +#else + +static void *variables[] = {&GLRO (dl_pagesize)}; + +static void _dl_unprotect_relro (struct link_map *l) +{ + ElfW (Addr) start = ((l->l_addr + l->l_relro_addr) + & ~(GLRO (dl_pagesize) - 1)); + ElfW (Addr) end = ((l->l_addr + l->l_relro_addr + l->l_relro_size) + & ~(GLRO (dl_pagesize) - 1)); + + if (start != end) + __mprotect ((void *) start, end - start, PROT_READ | PROT_WRITE); +} + +void dl_static_init (struct link_map *l) +{ + struct link_map *rtld_map = l; + struct r_scope_elem **scope; + const ElfW (Sym) *ref = NULL; + lookup_t loadbase; + void (*f) (void *[]); + size_t i; + + loadbase = _dl_lookup_symbol_x ("_dl_var_init", l, &ref, l->l_local_scope, + NULL, 0, 1, NULL); + + for (scope = l->l_local_scope; *scope != NULL; scope++) + for (i = 0; i < (*scope)->r_nlist; i++) + if ((*scope)->r_list[i] == loadbase) + { + rtld_map = (*scope)->r_list[i]; + break; + } + + if (ref != NULL) + { + f = (void (*) (void *[])) DL_SYMBOL_ADDRESS (loadbase, ref); + _dl_unprotect_relro (rtld_map); + f (variables); + _dl_protect_relro (rtld_map); + } +} + +#endif diff --git a/sysdeps/unix/sysv/linux/loongarch/ldsodefs.h b/sysdeps/unix/sysv/linux/loongarch/ldsodefs.h new file mode 100644 index 0000000000..137b0801c0 --- /dev/null +++ b/sysdeps/unix/sysv/linux/loongarch/ldsodefs.h @@ -0,0 +1,32 @@ +/* Copyright (C) 2021 Free Software Foundation, Inc. + + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library. If not, see + . */ + +#ifndef _LDSODEFS_H + +/* Get the real definitions. */ +#include_next + +/* Now define our stuff. */ + +/* We need special support to initialize DSO loaded for statically linked + binaries. */ +extern void _dl_static_init (struct link_map *map); +#undef DL_STATIC_INIT +#define DL_STATIC_INIT(map) _dl_static_init (map) + +#endif /* ldsodefs.h */