From c37626a3d1901dbe4075e21699c8061b19471d89 Mon Sep 17 00:00:00 2001 From: John Dallaway Date: Thu, 21 Dec 2023 18:41:05 +0000 Subject: [PATCH] Provide helper class for PE64 objects This class is intended to mirror the capabilities of the ElfHelper class over time. Initial support allows for correct presentation of section sizes within the Eclipse Properties view when a PE64 object file is selected. --- .../.settings/.api_filters | 17 +++++ .../org.eclipse.cdt.core/META-INF/MANIFEST.MF | 2 +- .../org/eclipse/cdt/utils/coff/Coff64.java | 7 +- .../eclipse/cdt/utils/coff/PEHelper64.java | 71 +++++++++++++++++++ .../utils/coff/parser/PEBinaryObject64.java | 7 ++ 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 core/org.eclipse.cdt.core/.settings/.api_filters create mode 100644 core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PEHelper64.java diff --git a/core/org.eclipse.cdt.core/.settings/.api_filters b/core/org.eclipse.cdt.core/.settings/.api_filters new file mode 100644 index 00000000000..9dabfaff023 --- /dev/null +++ b/core/org.eclipse.cdt.core/.settings/.api_filters @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF index 9492fc2e2b4..5c121e5c37f 100644 --- a/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.cdt.core; singleton:=true -Bundle-Version: 8.3.200.qualifier +Bundle-Version: 8.4.0.qualifier Bundle-Activator: org.eclipse.cdt.core.CCorePlugin Bundle-Vendor: %providerName Bundle-Localization: plugin diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/Coff64.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/Coff64.java index 3cd29d76e47..412f8d52aae 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/Coff64.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/Coff64.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2020 Space Codesign Systems and others. + * Copyright (c) 2000, 2023 Space Codesign Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -12,6 +12,7 @@ * Space Codesign Systems - Initial API and implementation * QNX Software Systems - Initial Coff class * Alexander Fedorov (ArSysOp) - Bug 561992 + * John Dallaway - Provide additional section header flags (#652) *******************************************************************************/ package org.eclipse.cdt.utils.coff; @@ -310,6 +311,10 @@ public class Coff64 { word boundary. */ public final static int STYP_LIT = 0x8020; /* Literal data (like STYP_TEXT) */ + /** @since 8.4 */ + public static final int STYP_MEM_DISCARDABLE = 0x02000000; /* section is discardable */ + /** @since 8.4 */ + public static final int STYP_MEM_WRITE = 0x80000000; /* section can be written to */ public byte[] s_name = new byte[8]; // 8 bytes: section name public int s_paddr; // 4 bytes: physical address, aliased s_nlib diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PEHelper64.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PEHelper64.java new file mode 100644 index 00000000000..0ecbc3a3228 --- /dev/null +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PEHelper64.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2000, 2023 QNX Software Systems and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * QNX Software Systems - Initial API and implementation of ElfHelper + * John Dallaway - Initial PEHelper64 implementation (#652) + *******************************************************************************/ +package org.eclipse.cdt.utils.coff; + +import java.io.IOException; + +import org.eclipse.cdt.utils.coff.Coff64.FileHeader; +import org.eclipse.cdt.utils.coff.Coff64.SectionHeader; + +/** + * PEHelper64 is a wrapper class for the PE64 class + * to provide higher level API for sorting/searching the COFF data. + * + * @see PE64 + * @since 8.4 + */ +public final class PEHelper64 { + + public static record Sizes(long text, long data, long bss) { + + public long total() { + return text + data + bss; + } + + } + + private final PE64 pe64; + + public PEHelper64(PE64 pe64) { + this.pe64 = pe64; + } + + public Sizes getSizes() throws IOException { + long bss = 0; + long data = 0; + long text = 0; + + boolean isExecutable = (pe64.getFileHeader().f_flags & FileHeader.F_EXEC) != 0; + for (SectionHeader section : pe64.getSectionHeaders()) { + if ((section.s_flags & SectionHeader.STYP_MEM_DISCARDABLE) != 0) { + continue; // ignore discardable section + } + long size = isExecutable ? section.s_paddr : section.s_size; + if ((section.s_flags & SectionHeader.STYP_BSS) != 0) { + bss += size; + } else if ((section.s_flags & SectionHeader.STYP_DATA) != 0) { + if ((section.s_flags & SectionHeader.STYP_MEM_WRITE) != 0) { + data += size; // treat writable data section as data + } else { + text += size; // treat non-writable data section as text + } + } else if ((section.s_flags & SectionHeader.STYP_TEXT) != 0) { + text += size; + } + } + return new Sizes(text, data, bss); + } + +} diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryObject64.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryObject64.java index 56b3e3ca6f5..573c74d30d4 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryObject64.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/parser/PEBinaryObject64.java @@ -12,6 +12,7 @@ * Space Codesign Systems - Initial API and implementation * QNX Software Systems - Initial PEBinaryObject class * John Dallaway - Fix archive header processing (#630) + * John Dallaway - Support sections sizes in binary info (#652) *******************************************************************************/ package org.eclipse.cdt.utils.coff.parser; @@ -34,6 +35,8 @@ import org.eclipse.cdt.utils.BinaryObjectAdapter; import org.eclipse.cdt.utils.Symbol; import org.eclipse.cdt.utils.coff.Coff64; import org.eclipse.cdt.utils.coff.PE64; +import org.eclipse.cdt.utils.coff.PEHelper64; +import org.eclipse.cdt.utils.coff.PEHelper64.Sizes; import org.eclipse.core.runtime.IPath; /** @@ -146,6 +149,10 @@ public class PEBinaryObject64 extends BinaryObjectAdapter { info.isLittleEndian = attribute.isLittleEndian(); info.hasDebug = attribute.hasDebug(); info.cpu = attribute.getCPU(); + Sizes sizes = new PEHelper64(pe).getSizes(); + info.bss = sizes.bss(); + info.data = sizes.data(); + info.text = sizes.text(); } protected void loadSymbols(PE64 pe) throws IOException {