1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

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.
This commit is contained in:
John Dallaway 2023-12-21 18:41:05 +00:00
parent dde763ac0c
commit c37626a3d1
5 changed files with 102 additions and 2 deletions

View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<component id="org.eclipse.cdt.core" version="2">
<resource path="utils/org/eclipse/cdt/utils/coff/Coff64.java" type="org.eclipse.cdt.utils.coff.Coff64$SectionHeader">
<filter id="336658481">
<message_arguments>
<message_argument value="org.eclipse.cdt.utils.coff.Coff64.SectionHeader"/>
<message_argument value="STYP_MEM_DISCARDABLE"/>
</message_arguments>
</filter>
<filter id="336658481">
<message_arguments>
<message_argument value="org.eclipse.cdt.utils.coff.Coff64.SectionHeader"/>
<message_argument value="STYP_MEM_WRITE"/>
</message_arguments>
</filter>
</resource>
</component>

View file

@ -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

View file

@ -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

View file

@ -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;
/**
* <code>PEHelper64</code> is a wrapper class for the <code>PE64</code> 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);
}
}

View file

@ -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 {