From patchwork Fri Feb 24 07:53:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 61175 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp764313wrd; Thu, 23 Feb 2023 23:53:53 -0800 (PST) X-Google-Smtp-Source: AK7set8wT7amKeh/yOmaUMBPjZHYeV9VMWpSRYvuTsfCqQvolhcMpOTjMFwWltZ8xxZEpct3o9zI X-Received: by 2002:a17:906:8042:b0:8b1:788f:2198 with SMTP id x2-20020a170906804200b008b1788f2198mr27245814ejw.19.1677225233779; Thu, 23 Feb 2023 23:53:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677225233; cv=none; d=google.com; s=arc-20160816; b=0jnl7b9iq5zX72ailFM6MfzfJWsxRHhsmUm4jXJ7qjIaTQBBwKmapuEpx/MU1RR5lj 56SoPJaGvGSM5v6XrUemTUMG1K4wOGJgO4wF/YSLa0g4WCEPWkBwAPSkMDnrHIjisw+a rCZZkzNc6FTQKtXc2Pfz9XnUZoKfmGmKzYjpQfsE1K0ss0AR7dhM6NJxLSJ/O8o8cqpw MUoRZ1Sdj3XjOOtKb+0Vl+/VyV4s24fTZ6pbTaJIBgyK/Zy+UgFVw0Fi6woTczKUY/o4 Sm6SMO2/e9n4NUyq2Oh9pVOBiPbH33GQbjuDFtUxuj0mtf9Zc0t7Rk7kqjsyCsBIsw3g XHAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=GxYAOYkcB2338Tj7X/UYpz6Qh2VIB8QuV/jj9/fDrFo=; b=bgWvCFIxJgqzES3Ts7S1+1LP8LlnXoP4DP6mKbOR2rlMUWwpy//Vxbwj2QRRp0ODaZ cOz1kQsHxVzknU+9TzYR2KLJBjEPsWz0WormKnWmjpcTMbOpSJnLpoPa/R+XbcXRPdZh xVG3JIZnVcewlXF+iW676dkAZ2yBqFsTKuFxjhaR73lsI7KJWFaj/VE17xG25/m6KYik NM+S7mMqwNBX5kDzFLpFAHfdbinuBQy/feEj4sr6GVZqWlJ4C25kI4p2LIZ2xy4DST1P pFTcvoQn61p3w4kZjRYLJ4moa6hB2qGJ5jpWDrTHekvIxbc9FoYq581aJ3ZFjgGZwtLP LkbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=driKiaEP; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id bo13-20020a0564020b2d00b004ad15601457si9796436edb.629.2023.02.23.23.53.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 23:53:53 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=driKiaEP; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7FDC4385B50C for ; Fri, 24 Feb 2023 07:53:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7FDC4385B50C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677225232; bh=GxYAOYkcB2338Tj7X/UYpz6Qh2VIB8QuV/jj9/fDrFo=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=driKiaEPimTqxoOjaAlpJ9KyXTahlfe5EbsfhMXc248LZOd3x4wbEFwVD8J85omyR rmxT2ex6ikXgZnM1wFU/jOqcjbyPbkEVxZFwQwWEV+FMc3TgyfhcGXoJqP3N5eHfB4 goWA0wY536kuuhK3R4NA9XoF+AZ7/cLqt8YoPAj0= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by sourceware.org (Postfix) with ESMTPS id C2D6A385842B for ; Fri, 24 Feb 2023 07:53:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C2D6A385842B Received: by mail-pj1-x102a.google.com with SMTP id l1so5268866pjt.2 for ; Thu, 23 Feb 2023 23:53:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GxYAOYkcB2338Tj7X/UYpz6Qh2VIB8QuV/jj9/fDrFo=; b=J/aE8Ymb0J10MM2XHLeNbJ6ukSs6696nHLNy26/vJoFB44/LDLQYCN3uAwj6ytsYUH 3E8zoNg7i7emKVzAbMc5qgrrDGh4Dm7rGMVtnhMyMDp/o7HtEWJQpf9eeRK2KJRjx3c4 p7stRA93DyHHNC3LcqsLw0cWitldjd+gUkjQgbr1UbG+u2F4okV4PDd4hag8RSzVNDh0 3HehYGT+irI0AOWnOeJFOxZIqsKk/tcn97ssFZR20lVm/D93XgJcQnxX1UfRpcpxd7EW N7/TGYzC4F/76HS3YMn9e9UG2Q5WzduYhYfHghTsyMP80aKXYXw0w2jIpI6xCJc0J8Hj CCrA== X-Gm-Message-State: AO0yUKVL2A1BUL3IkN6boAYildaYG3ad+jVTsRJ0QtIUxW+p3WJg33QM JP80g32uNcDSUdK4p0MoMnnwk2+NyBE= X-Received: by 2002:a17:90b:1b4c:b0:233:d3ac:5dbf with SMTP id nv12-20020a17090b1b4c00b00233d3ac5dbfmr17295953pjb.10.1677225222425; Thu, 23 Feb 2023 23:53:42 -0800 (PST) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id l3-20020a17090a49c300b0023371cb020csm895718pjm.34.2023.02.23.23.53.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 23:53:42 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id DE0BA1142C9F; Fri, 24 Feb 2023 18:23:39 +1030 (ACDT) Date: Fri, 24 Feb 2023 18:23:39 +1030 To: binutils@sourceware.org Subject: PR30155, ld segfault in _bfd_nearby_section Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3035.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758698126655586150?= X-GMAIL-MSGID: =?utf-8?q?1758698126655586150?= The segfault was a symptom of messing with the absolute section next field, confusing bfd_section_removed_from_list in linker.c:fix_syms. That's not all that was going wrong. The INSERT list of output sections was being inserted into itself, ie. lost from the main list of linker statements. PR 30155 * ldlang.c (process_insert_statements): Handle pathological case of the insert script being inserted before the first output section statement in the default script. (output_prev_sec_find): Don't test section owner here. (insert_os_after): Change parameter to a list union pointer. (lang_insert_orphan): Test section owner here and adjust insert_os_after call. diff --git a/ld/ldlang.c b/ld/ldlang.c index 2852a4222d3..295de015da9 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -1774,7 +1774,7 @@ output_prev_sec_find (lang_output_section_statement_type *os) if (lookup->constraint < 0) continue; - if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL) + if (lookup->bfd_section != NULL) return lookup->bfd_section; } @@ -1793,13 +1793,13 @@ output_prev_sec_find (lang_output_section_statement_type *os) image symbols. */ static lang_statement_union_type ** -insert_os_after (lang_output_section_statement_type *after) +insert_os_after (lang_statement_union_type *after) { lang_statement_union_type **where; lang_statement_union_type **assign = NULL; bool ignore_first; - ignore_first = after == (void *) lang_os_list.head; + ignore_first = after == lang_os_list.head; for (where = &after->header.next; *where != NULL; @@ -1936,7 +1936,9 @@ lang_insert_orphan (asection *s, if (bfd_section == NULL) bfd_section = output_prev_sec_find (after); - if (bfd_section != NULL && bfd_section != snew) + if (bfd_section != NULL + && bfd_section->owner != NULL + && bfd_section != snew) place->section = &bfd_section->next; } @@ -2159,8 +2161,9 @@ lang_insert_orphan (asection *s, /* Place OS after AFTER if AFTER_NOTE is TRUE. */ if (place_after) { - lang_statement_union_type **where = insert_os_after (after); + lang_statement_union_type **where; + where = insert_os_after ((lang_statement_union_type *) after); *add.tail = *where; *where = add.head; @@ -4370,21 +4373,55 @@ process_insert_statements (lang_statement_union_type **start) else link_info.output_bfd->section_last = first_sec->prev; /* Add back. */ - last_sec->next = sec->next; - if (sec->next != NULL) - sec->next->prev = last_sec; + if (sec->owner == NULL) + /* SEC is the absolute section, from the + first dummy output section statement. Add + back the sections we trimmed off to the + start of the bfd sections. */ + sec = NULL; + if (sec != NULL) + last_sec->next = sec->next; + else + last_sec->next = link_info.output_bfd->sections; + if (last_sec->next != NULL) + last_sec->next->prev = last_sec; else link_info.output_bfd->section_last = last_sec; first_sec->prev = sec; - sec->next = first_sec; + if (first_sec->prev != NULL) + first_sec->prev->next = first_sec; + else + link_info.output_bfd->sections = first_sec; } } - - first_os = NULL; - last_os = NULL; } - ptr = insert_os_after (where); + lang_statement_union_type *after = (void *) where; + if (where == &lang_os_list.head->output_section_statement + && where->next == first_os) + { + /* PR30155. Handle a corner case where the statement + list is something like the following: + . LOAD t.o + . .data 0x0000000000000000 0x0 + . [0x0000000000000000] b = . + . *(.data) + . .data 0x0000000000000000 0x0 t.o + . 0x0000000000000000 0x4 LONG 0x0 + . INSERT BEFORE .text.start + . [0x0000000000000004] a = . + . .text.start 0x0000000000000000 0x0 + . [0x0000000000000000] c = . + . OUTPUT(a.out elf64-x86-64) + Here we do not want to allow insert_os_after to + choose a point inside the list we are moving. + That would lose the list. Instead, let + insert_os_after work from the INSERT, which in this + particular example will result in inserting after + the assignment "a = .". */ + after = *s; + } + ptr = insert_os_after (after); /* Snip everything from the start of the list, up to and including the insert statement we are currently processing. */ first = *start; @@ -4395,6 +4432,8 @@ process_insert_statements (lang_statement_union_type **start) statement_list.tail = s; *ptr = first; s = start; + first_os = NULL; + last_os = NULL; continue; } s = &(*s)->header.next;