From 228d62dd778efd3c9b064567d4cb08c4cd19c900 Mon Sep 17 00:00:00 2001 From: John Dallaway Date: Mon, 1 Jan 2024 08:13:35 +0000 Subject: [PATCH] Support section data lookup within static libraries --- .../.settings/.api_filters | 8 +++++++ .../org/eclipse/cdt/utils/coff/Coff64.java | 21 +++++++++++++++---- .../org/eclipse/cdt/utils/coff/PE64.java | 4 ++-- .../utils/org/eclipse/cdt/utils/elf/Elf.java | 18 +++++++++++++--- .../cdt/utils/elf/parser/ElfBinaryObject.java | 6 +++++- 5 files changed, 47 insertions(+), 10 deletions(-) diff --git a/core/org.eclipse.cdt.core/.settings/.api_filters b/core/org.eclipse.cdt.core/.settings/.api_filters index 9dabfaff023..bb09e12d1ba 100644 --- a/core/org.eclipse.cdt.core/.settings/.api_filters +++ b/core/org.eclipse.cdt.core/.settings/.api_filters @@ -14,4 +14,12 @@ + + + + + + + + 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 412f8d52aae..161fff19a31 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, 2023 Space Codesign Systems and others. + * Copyright (c) 2000, 2024 Space Codesign Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -13,6 +13,7 @@ * QNX Software Systems - Initial Coff class * Alexander Fedorov (ArSysOp) - Bug 561992 * John Dallaway - Provide additional section header flags (#652) + * John Dallaway - Support offset into archive file (#630) *******************************************************************************/ package org.eclipse.cdt.utils.coff; @@ -329,9 +330,17 @@ file, a new function (as RandomAccessFile sfile; - public SectionHeader(RandomAccessFile file, long offset) throws IOException { + private final long objOffset; // the offset of this binary object within the file (eg archive file) + + public SectionHeader(RandomAccessFile file, long hdrOffset) throws IOException { + this(file, 0, hdrOffset); + } + + /** @since 8.4 */ + public SectionHeader(RandomAccessFile file, long objOffset, long hdrOffset) throws IOException { sfile = file; - file.seek(offset); + this.objOffset = objOffset; + file.seek(hdrOffset + objOffset); byte[] hdr = new byte[SCNHSZ]; file.readFully(hdr); ReadMemoryAccess memory = new ReadMemoryAccess(hdr, true); @@ -409,7 +418,8 @@ public String toString() { * @since 5.1 */ public ByteBuffer mapSectionData() throws IOException { - return sfile.getChannel().map(MapMode.READ_ONLY, s_scnptr, s_paddr).load().asReadOnlyBuffer(); + long size = s_paddr == 0 ? s_size : s_paddr; + return sfile.getChannel().map(MapMode.READ_ONLY, s_scnptr + objOffset, size).load().asReadOnlyBuffer(); } } @@ -525,6 +535,9 @@ public static class Symbol { /** @since 5.3 */ public final static int T_LNGDBL = 0x16; /* -1 0000 long double (special case bit pattern) */ + /** @since 8.4 */ + public static final int SC_EXTERNAL = 2; /* external symbol storage class */ + public byte[] _n_name = new byte[SYMNMLEN]; /* Symbol name, or pointer into string table if symbol name is greater than SYMNMLEN. */ diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PE64.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PE64.java index ab0d7391c65..018faa924e1 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PE64.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/coff/PE64.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2023 Space Codesign Systems and others. + * Copyright (c) 2000, 2024 Space Codesign Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -762,7 +762,7 @@ public SectionHeader[] getSectionHeaders() throws IOException { } offset += FileHeader.FILHSZ + fileHeader.f_opthdr; for (int i = 0; i < scnhdrs.length; i++, offset += SectionHeader.SCNHSZ) { - scnhdrs[i] = new SectionHeader(accessFile, offset + peOffset); + scnhdrs[i] = new SectionHeader(accessFile, peOffset, offset); } } return scnhdrs; diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/Elf.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/Elf.java index fa3cc455aba..abe8f5b6142 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/Elf.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/Elf.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 QNX Software Systems and others. + * Copyright (c) 2000, 2024 QNX Software Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -11,6 +11,7 @@ * Contributors: * QNX Software Systems - initial API and implementation * Markus Schorn (Wind River Systems) + * John Dallaway - Support offset into archive file (#630) *******************************************************************************/ package org.eclipse.cdt.utils.elf; @@ -333,12 +334,23 @@ public class Section { public long sh_addralign; public long sh_entsize; + private final long objOffset; // the offset of this binary object within the file (eg archive file) + + public Section() { + this(0); + } + + /** @since 8.4 */ + public Section(long objOffset) { + this.objOffset = objOffset; + } + /** * @since 5.1 */ public ByteBuffer mapSectionData() throws IOException { makeSureNotCompressed(); - return efile.getChannel().map(MapMode.READ_ONLY, sh_offset, sh_size).load().asReadOnlyBuffer(); + return efile.getChannel().map(MapMode.READ_ONLY, sh_offset + objOffset, sh_size).load().asReadOnlyBuffer(); } public byte[] loadSectionData() throws IOException { @@ -1037,7 +1049,7 @@ public Section[] getSections() throws IOException { sections = new Section[length]; for (int i = 0; i < length; i++) { efile.seek(ehdr.e_shoff + i * (ehdr.e_shentsize & 0xffff)); // unsigned short - sections[i] = new Section(); + sections[i] = new Section(elfOffset); sections[i].sh_name = efile.readIntE(); sections[i].sh_type = efile.readIntE(); switch (ehdr.e_ident[ELFhdr.EI_CLASS]) { diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfBinaryObject.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfBinaryObject.java index a60d9a048d2..838ef7b0652 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfBinaryObject.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ElfBinaryObject.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2015 QNX Software Systems and others. + * Copyright (c) 2000, 2024 QNX Software Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,6 +10,7 @@ * * Contributors: * QNX Software Systems - Initial API and implementation + * John Dallaway - Fix archive header processing (#630) *******************************************************************************/ package org.eclipse.cdt.utils.elf.parser; @@ -182,6 +183,9 @@ protected void addSymbols(Elf.Symbol[] array, int type, List list) { public T getAdapter(Class adapter) { if (adapter.equals(Elf.class)) { try { + if (header != null) { + return (T) new Elf(getPath().toOSString(), header.getObjectDataOffset()); + } return (T) new Elf(getPath().toOSString()); } catch (IOException e) { }