diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/attribs/DWARFForm.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/attribs/DWARFForm.java index be77fc8ab7..acf14c8e56 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/attribs/DWARFForm.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/attribs/DWARFForm.java @@ -177,8 +177,8 @@ public enum DWARFForm { DWARFForm indirectForm = DWARFForm.of(indirectFormInt); DWARFAttributeDef indirectAS = context.def().withForm(indirectForm); - DWARFFormContext indirectContext = - new DWARFFormContext(context.reader(), context.compUnit(), indirectAS); + DWARFFormContext indirectContext = new DWARFFormContext(context.reader(), + context.compUnit(), indirectAS, context.dwarfIntSize()); long indirectSize = indirectForm.getSize(indirectContext); return firstSize + indirectSize; @@ -189,8 +189,8 @@ public enum DWARFForm { int indirectFormInt = context.reader().readNextUnsignedVarIntExact(LEB128::unsigned); DWARFForm indirectForm = DWARFForm.of(indirectFormInt); DWARFAttributeDef indirectAS = context.def().withForm(indirectForm); - DWARFFormContext indirectContext = - new DWARFFormContext(context.reader(), context.compUnit(), indirectAS); + DWARFFormContext indirectContext = new DWARFFormContext(context.reader(), + context.compUnit(), indirectAS, context.dwarfIntSize()); return indirectForm.readValue(indirectContext); } }, @@ -335,7 +335,7 @@ public enum DWARFForm { public long getSize(DWARFFormContext context) throws IOException { switch (size) { case DWARF_INTSIZE: - return context.compUnit().getIntSize(); + return context.dwarfIntSize(); case LEB128_SIZE: return context.reader().readNext(LEB128::getLength); case DYNAMIC_SIZE: diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/attribs/DWARFFormContext.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/attribs/DWARFFormContext.java index bab61a90cb..33d09047e6 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/attribs/DWARFFormContext.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/attribs/DWARFFormContext.java @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,7 +16,8 @@ package ghidra.app.util.bin.format.dwarf.attribs; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf.*; +import ghidra.app.util.bin.format.dwarf.DWARFCompilationUnit; +import ghidra.app.util.bin.format.dwarf.DWARFProgram; /** * Context given to the {@link DWARFForm#readValue(DWARFFormContext)} method to enable it to @@ -25,15 +26,27 @@ import ghidra.app.util.bin.format.dwarf.*; * @param reader {@link BinaryReader} * @param compUnit {@link DWARFCompilationUnit} * @param def {@link DWARFAttributeDef} + * @param dwarfIntSize size of dwarf serialization ints, either 4 (32 bit dwarf) or + * 8 (64 bit dwarf). Can be different from compUnit's intSize if this context is being used + * to read values from a non-".debuginfo" section that has unit headers that specify an + * independent intSize. */ public record DWARFFormContext(BinaryReader reader, DWARFCompilationUnit compUnit, - DWARFAttributeDef def) { + DWARFAttributeDef def, int dwarfIntSize) { + + /** + * Creates a new DWARFFormContext, using the compUnit's int size + * + * @param reader stream that will be used to read the dwarf form value + * @param compUnit {@link DWARFCompilationUnit} that contains the value + * @param def identity info about the attribute being read + */ + public DWARFFormContext(BinaryReader reader, DWARFCompilationUnit compUnit, + DWARFAttributeDef def) { + this(reader, compUnit, def, compUnit.getIntSize()); + } DWARFProgram dprog() { return compUnit.getProgram(); } - - int dwarfIntSize() { - return compUnit.getIntSize(); - } } diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFFile.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFFile.java index 62d219b140..37990fbcc3 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFFile.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFFile.java @@ -59,12 +59,13 @@ public class DWARFFile { * @param reader BinaryReader * @param defs similar to a DIE's attributespec, a list of DWARFForms that define how values * will be deserialized from the stream + * @param dwarfIntSize size of serialized dwarf ints (might be different than the CU's dwarfIntSize) * @param cu {@link DWARFCompilationUnit} * @return new DWARFFile * @throws IOException if error reading */ public static DWARFFile readV5(BinaryReader reader, List defs, - DWARFCompilationUnit cu) throws IOException { + int dwarfIntSize, DWARFCompilationUnit cu) throws IOException { String name = null; int directoryIndex = -1; @@ -72,7 +73,7 @@ public class DWARFFile { long length = 0; byte[] md5 = null; for (DWARFLineContentType.Def def : defs) { - DWARFFormContext context = new DWARFFormContext(reader, cu, def); + DWARFFormContext context = new DWARFFormContext(reader, cu, def, dwarfIntSize); DWARFAttributeValue val = def.getAttributeForm().readValue(context); switch (def.getAttributeId()) { diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFLine.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFLine.java index e1c5206f61..98d1d7bd1c 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFLine.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFLine.java @@ -168,7 +168,7 @@ public class DWARFLine { // read the directories, which are defined the same way files are int directories_count = reader.readNextUnsignedVarIntExact(LEB128::unsigned); for (int i = 0; i < directories_count; i++) { - DWARFFile dir = DWARFFile.readV5(reader, dirFormatDefs, cu); + DWARFFile dir = DWARFFile.readV5(reader, dirFormatDefs, result.intSize, cu); dir = fixupDir(dir, defaultCompDir); result.directories.add(dir); } @@ -182,7 +182,7 @@ public class DWARFLine { int file_names_count = reader.readNextUnsignedVarIntExact(LEB128::unsigned); for (int i = 0; i < file_names_count; i++) { - DWARFFile dir = DWARFFile.readV5(reader, fileFormatDefs, cu); + DWARFFile dir = DWARFFile.readV5(reader, fileFormatDefs, result.intSize, cu); result.files.add(dir); } }