From 9ca7cf32da79a386de3e9db011ae450ad5beac12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20M=2E=20Kruczy=C5=84ski?= Date: Mon, 14 Nov 2022 18:24:19 +0100 Subject: [PATCH] First obtain correct number of sections, then read string table. Fixes #89. --- CHANGELOG.md | 4 ++++ ELFSharp/ELF/ELF.cs | 18 +++++++++--------- ELFSharp/ELFSharp.csproj | 4 ++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a663f9c..29e9abb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.15.2] - 2022-11-14 +### Fixed +- ELF: case when section number is larger than `SHN_LORESERVE` and string table is present is now handled properly. + ## [2.15.1] - 2022-11-12 ### Fixed - ELF: case when section number is larger than `SHN_LORESERVE` is now handled properly. diff --git a/ELFSharp/ELF/ELF.cs b/ELFSharp/ELF/ELF.cs index 9d20fa6..c03a006 100644 --- a/ELFSharp/ELF/ELF.cs +++ b/ELFSharp/ELF/ELF.cs @@ -228,15 +228,6 @@ private void ReadSections() sectionIndicesByName = new Dictionary(); } - // If the number of sections is greater than or equal to SHN_LORESERVE (0xff00), this member has the - // value zero and the actual number of section header table entries is contained in the sh_size field - // of the section header at index 0. (Otherwise, the sh_size member of the initial entry contains 0.) - if (sectionHeaderEntryCount == 0) - { - var firstSectionHeader = ReadSectionHeader(0, true); - sectionHeaderEntryCount = checked((uint)firstSectionHeader.Size); - } - for (var i = 0; i < sectionHeaderEntryCount; i++) { var header = ReadSectionHeader(i); @@ -370,6 +361,15 @@ private void ReadFields() sectionHeaderEntrySize = reader.ReadUInt16(); sectionHeaderEntryCount = reader.ReadUInt16(); stringTableIndex = reader.ReadUInt16(); + + // If the number of sections is greater than or equal to SHN_LORESERVE (0xff00), this member has the + // value zero and the actual number of section header table entries is contained in the sh_size field + // of the section header at index 0. (Otherwise, the sh_size member of the initial entry contains 0.) + if (sectionHeaderEntryCount == 0) + { + var firstSectionHeader = ReadSectionHeader(0, true); + sectionHeaderEntryCount = checked((uint)firstSectionHeader.Size); + } } private GetSectionResult TryGetSectionInner(string name, out Section section) diff --git a/ELFSharp/ELFSharp.csproj b/ELFSharp/ELFSharp.csproj index b47c58c..3e6befb 100644 --- a/ELFSharp/ELFSharp.csproj +++ b/ELFSharp/ELFSharp.csproj @@ -4,12 +4,12 @@ netstandard2.0 True sgKey.snk - 2.15.1.0 + 2.15.2.0 2.0 Konrad Kruczyński, Piotr Zierhoffer, Łukasz Kucharski, Bastian Eicher, Cameron, Everett Maus, Fox, Reuben Olinsky, Connor Christie, Rollrat, Ulysses Wu, Cédric Luthi, Yong Yan, Filip Navara, Dedmen Miller, Murat Ocaktürk Konrad Kruczyński http://elfsharp.it/ - Fixed bug when wrong ELF section numbers count is read. Detailed changelog available at: https://github.com/konrad-kruczynski/elfsharp/blob/master/CHANGELOG.md + Fixed bug with reading string table with large numbers of sections. Detailed changelog available at: https://github.com/konrad-kruczynski/elfsharp/blob/master/CHANGELOG.md C# library for reading binary ELF, UImage, Mach-O files ELF UImage Mach-O ELFSharp