From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 76887 invoked by alias); 25 Jun 2019 14:41:18 -0000 Mailing-List: contact dwz-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Post: List-Help: List-Subscribe: Sender: dwz-owner@sourceware.org Received: (qmail 76877 invoked by uid 89); 25 Jun 2019 14:41:17 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Checked: by ClamAV 0.100.3 on sourceware.org X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.6 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_PASS autolearn=ham version=3.3.1 spammy=phases, responsibility X-Spam-Status: No, score=-24.6 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_PASS autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on sourceware.org X-Spam-Level: X-HELO: mx1.suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Subject: [committed] Make updating sections after section header string table order-independent From: Tom de Vries To: dwz@sourceware.org, jakub@redhat.com References: <20190315151221.GA7938@delia> Message-ID: Date: Tue, 01 Jan 2019 00:00:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20190315151221.GA7938@delia> Content-Type: multipart/mixed; boundary="------------7FC642A9E01D9DCDFE060ABA" Content-Language: en-US X-SW-Source: 2019-q2/txt/msg00071.txt.bz2 This is a multi-part message in MIME format. --------------7FC642A9E01D9DCDFE060ABA Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-length: 1579 [ was: [PATCH] Fix potential wrong-code issue in write_dso ] On 15-03-19 16:12, Tom de Vries wrote: > Hi, > > The function write_dso has the responsibility to update: > - the file offset of the sections, and > - the file offset of the section header table. > > It does this in two steps: > - it calculates the effects of adding, removing and updating > debug sections > - it makes sure the file offsets have the required alignment > > The second step may or may not update the file offsets, but if it does, it > requires the entries in the section header table to be in file offset > order. > > However, if the second step does not update the file offsets, no check on > section header table order is done, which implies that the first step should > be able to handle an unsorted section header table. > > That is not the case for this update loop: > ... > for (j = dso->ehdr.e_shstrndx + 1; j < dso->ehdr.e_shnum; ++j) > dso->shdr[j].sh_offset += len; > ... > > This loop adds an increase of the size of the section header string table to > sections 'after' the section header string table. But the after test here is > implemented in terms of order in the section header table, which only works if > if the section header table is sorted. > > Fix this by rewriting the after test in terms of sh_offset: > ... > for (j = 1; j < dso->ehdr.e_shnum; ++j) > if (dso->shdr[j].sh_offset > > dso->shdr[dso->ehdr.e_shstrndx].sh_offset) > dso->shdr[j].sh_offset += len; > ... > > OK for trunk? > Cleaned up rationale and committed. Thanks, - Tom --------------7FC642A9E01D9DCDFE060ABA Content-Type: text/x-patch; name="0001-Make-updating-sections-after-section-header-string-table-order-independent.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename*0="0001-Make-updating-sections-after-section-header-string-tabl"; filename*1="e-order-independent.patch" Content-length: 2020 Make updating sections after section header string table order-independent Function write_dso updates the section header table: it adds, removes and updates sections. It does this in two phases: - it calculates the effect of changing sections on the following sections sh_offset (as well as on the section header table e_shoff) - it makes sure sh_offset is sh_addralign aligned (as well as that the section header table e_shoff is ELFCLASS-appropriately aligned). The first phase is independent of the order of the section header table, apart from this loop: ... for (j = dso->ehdr.e_shstrndx + 1; j < dso->ehdr.e_shnum; ++j) dso->shdr[j].sh_offset += len; ... It adds an increase of the size of the section header string table to sections 'after' the section header string table. But the after test here is implemented in terms of order in the section header table, which only works if if the section header table is sorted. Make the first phase order-independent by rewriting the after test in terms of sh_offset. 2019-06-25 Tom de Vries * dwz.c (write_dso): Make updating of sections after section header string table robust against unsorted section header table. --- dwz.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dwz.c b/dwz.c index 9e39824..101b6e3 100644 --- a/dwz.c +++ b/dwz.c @@ -10405,8 +10405,10 @@ write_dso (DSO *dso, const char *file, struct stat *st) dso->shdr[dso->ehdr.e_shstrndx].sh_size += len; if (dso->shdr[dso->ehdr.e_shstrndx].sh_offset < min_shoff) min_shoff = dso->shdr[dso->ehdr.e_shstrndx].sh_offset; - for (j = dso->ehdr.e_shstrndx + 1; j < dso->ehdr.e_shnum; ++j) - dso->shdr[j].sh_offset += len; + for (j = 1; j < dso->ehdr.e_shnum; ++j) + if (dso->shdr[j].sh_offset + > dso->shdr[dso->ehdr.e_shstrndx].sh_offset) + dso->shdr[j].sh_offset += len; if (ehdr.e_shoff > dso->shdr[dso->ehdr.e_shstrndx].sh_offset) ehdr.e_shoff += len; shstrtabadd += len; --------------7FC642A9E01D9DCDFE060ABA--