From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id 24C3F3858429 for ; Mon, 26 Feb 2024 19:33:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 24C3F3858429 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=osandov.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=osandov.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 24C3F3858429 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708975984; cv=none; b=jcdBMxlscFUxIWX+KYlVYG1DHzjcEumY8jl/MI4cfk+G06+3K5qalUpfqL77JoegOV/MXLR/7rSAj3LubbAHCaUWIH2+SBu2RU0dunKolscTXiLspBWFum2pbsCKUZaPtx8FqiRZwdf3jB7q0QgnG4IHoEGBSl2hQ5uYx1EiYdc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708975984; c=relaxed/simple; bh=mMNq19sTrKAFWo8Y1fRhxqZg285Un/II2K7RzlCi/qk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=jSZVfFEXcuhaH2iaMSMGB5mvwhbemfgrJI0rOPhE3FVOIYbRgoyCkl81/aRD3+iOJBMRYkIYqv1UpCSWEN4aSvuGDG64o6zAesvukIGn20WxVizcgsjNnHLtV08FFESXLNqjfw5W9cVitEr/FwQI/TCAdnuf0pHEPxEiXdsZWo8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1d93edfa76dso30002005ad.1 for ; Mon, 26 Feb 2024 11:33:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20230601.gappssmtp.com; s=20230601; t=1708975980; x=1709580780; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=jhKbmsW1xKazxd/Wbjb08zdCybqLAQvQ9DmLMkPPriQ=; b=WcT5ybS/q/GBH+a8SxeBlRhMfGW2jItXexoBiPBNTL2ffGY59Vf2S1QvtAkV3SkIeg TJdSYSD4u8GuzkDNuo5s/BLBnglnqOH3vQMZQXtv3lomdGlHXykRNXQ5g3RM+yp6SboH w/fkWMGnTFV1Wp9Q4UwEvuJng2/ctplYy2Zn6FRnKHmBSumL5Y3DSoVAkVO/uJSvXMeI TWJZOSpCNGSlFJ7pHkUFHE5ntrNqzypjdaxY+wHfjt7yjhgFscamUYV2ek6O9/3AsDuE Imy8PefYB44QTbI76SQjidm9sJEX6VxMBgLYPo365nQpFRI+5Je4tslaHV67Td/hOkvx le3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708975980; x=1709580780; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jhKbmsW1xKazxd/Wbjb08zdCybqLAQvQ9DmLMkPPriQ=; b=A4E+PpDNM/omZaETxznZcvlDuTAi6bNL63+pHh9HFOMTmK//zSGikmH47gqZC5+/9N ItwwTcA3CBx8fgmFkRwcZTN4N4E3+9y4KBj+7RZ+OpZdey4gucAKruVJgbqaAzoTx+Tq 40ELcv+7h2ftwN+GlIR7K7IgRL8ZmIitG2X6DMnQ1YY94n77tLMJkrrm8grqRc9xXIQ2 NTOukKQ08FCji46jbnc5Qvsuh+ciiKCxfBpuV0ReFadSsFaUdf/2iyLlNSseFjyqPyRp wPy0dSr5imIFKbO1xbVW524XaooJCoV4AWHN0ouqwLvUcQchka+m+dKj8jmyWQq8GvKq eHDA== X-Gm-Message-State: AOJu0YxzpAmURyWupCfsfkL46ywOVKio7yYbdmWGGhNOtOH57KCQ61ki tcaJiYWm3OFemexmw5YSJQI/6aNYDgCanueH31PvlbKOqofOBF8lCRe3vQFgN69a9SmMVMyoCV0 F X-Google-Smtp-Source: AGHT+IHRuXbK0ovmYla/9e+MlZxpujwm1L8LzoOvOd75Wgvlb98qNgOs86fDn/o3ekQofkk/SfeIuQ== X-Received: by 2002:a17:902:d587:b0:1dc:ae92:7e9 with SMTP id k7-20020a170902d58700b001dcae9207e9mr2337198plh.62.1708975980570; Mon, 26 Feb 2024 11:33:00 -0800 (PST) Received: from telecaster.thefacebook.com ([2620:10d:c090:500::4:e023]) by smtp.gmail.com with ESMTPSA id s21-20020a170902b19500b001dbc3f2e7f4sm56980plr.101.2024.02.26.11.33.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 11:33:00 -0800 (PST) From: Omar Sandoval To: elfutils-devel@sourceware.org Subject: [PATCH v3 2/4] libdw: Refactor dwarf_next_lines and fix skipped CU Date: Mon, 26 Feb 2024 11:32:49 -0800 Message-ID: X-Mailer: git-send-email 2.43.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Omar Sandoval dwarf_next_lines has two loops over CUs: one from the CU after the given CU to the end, and one from the first CU up to _but not including_ the given CU. This means that the given CU is never checked. This is unlikely to matter in practice since CUs usually correspond 1:1 with line number tables in the same order, but let's fix it anyways. Refactoring it to one loop fixes the problem and simplifies the next change to support DWARF package files. * libdw/dwarf_next_lines.c (dwarf_next_lines): Refactor loops over CUs into one loop. Signed-off-by: Omar Sandoval --- libdw/dwarf_next_lines.c | 75 +++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/libdw/dwarf_next_lines.c b/libdw/dwarf_next_lines.c index 74854ecd..6a9fe361 100644 --- a/libdw/dwarf_next_lines.c +++ b/libdw/dwarf_next_lines.c @@ -95,62 +95,49 @@ dwarf_next_lines (Dwarf *dbg, Dwarf_Off off, { /* We need to find the matching CU to get the comp_dir. Use the given CU as hint where to start searching. Normally it will - be the next CU that has a statement list. */ + be the next CU that has a statement list. If the CUs are in a + different order from the line tables, then we do a linear + search. */ Dwarf_CU *given_cu = *cu; Dwarf_CU *next_cu = given_cu; - bool found = false; - while (INTUSE(dwarf_get_units) (dbg, next_cu, &next_cu, NULL, NULL, - &cudie, NULL) == 0) + bool restarted = false; + while (1) { + if (restarted && next_cu == given_cu) + { + /* We checked all of the CUs and there was no match. */ + *cu = NULL; + break; + } + if (INTUSE(dwarf_get_units) (dbg, next_cu, &next_cu, NULL, NULL, + &cudie, NULL) != 0) + { + /* We didn't find the matching CU after the starting point. + Check the CUs up to the starting point. */ + next_cu = NULL; + restarted = true; + continue; + } + + Dwarf_Word stmt_off = 0; if (dwarf_hasattr (&cudie, DW_AT_stmt_list)) { Dwarf_Attribute attr; - Dwarf_Word stmt_off; if (dwarf_formudata (dwarf_attr (&cudie, DW_AT_stmt_list, &attr), - &stmt_off) == 0 - && stmt_off == off) - { - found = true; - break; - } + &stmt_off) != 0) + continue; } - else if (off == 0 - && (next_cu->unit_type == DW_UT_split_compile - || next_cu->unit_type == DW_UT_split_type)) + /* Split units have an implicit offset of 0. */ + else if (next_cu->unit_type != DW_UT_split_compile + && next_cu->unit_type != DW_UT_split_type) + continue; + + if (stmt_off == off) { - /* For split units (in .dwo files) there is only one table - at offset zero (containing just the files, no lines). */ - found = true; + *cu = next_cu; break; } } - - if (!found && given_cu != NULL) - { - /* The CUs might be in a different order from the line - tables. Need to do a linear search (but stop at the given - CU, since we already searched those. */ - next_cu = NULL; - while (INTUSE(dwarf_get_units) (dbg, next_cu, &next_cu, NULL, NULL, - &cudie, NULL) == 0 - && next_cu != given_cu) - { - Dwarf_Attribute attr; - Dwarf_Word stmt_off; - if (dwarf_formudata (dwarf_attr (&cudie, DW_AT_stmt_list, &attr), - &stmt_off) == 0 - && stmt_off == off) - { - found = true; - break; - } - } - } - - if (found) - *cu = next_cu; - else - *cu = NULL; } else *cu = NULL; -- 2.43.2