Skip to content

Commit

Permalink
Support section data lookup within static libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
jld01 committed Jan 2, 2024
1 parent 8e3012c commit 0173da5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
8 changes: 8 additions & 0 deletions core/org.eclipse.cdt.core/.settings/.api_filters
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
</message_arguments>
</filter>
</resource>
<resource path="utils/org/eclipse/cdt/utils/coff/Coff64.java" type="org.eclipse.cdt.utils.coff.Coff64$Symbol">
<filter id="336658481">
<message_arguments>
<message_argument value="org.eclipse.cdt.utils.coff.Coff64.Symbol"/>
<message_argument value="SC_EXTERNAL"/>
</message_arguments>
</filter>
</resource>
</component>
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 15 additions & 3 deletions core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/Elf.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -182,6 +183,9 @@ protected void addSymbols(Elf.Symbol[] array, int type, List<Symbol> list) {
public <T> T getAdapter(Class<T> 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) {
}
Expand Down

0 comments on commit 0173da5

Please sign in to comment.