22
22
import org .apache .commons .collections4 .ListValuedMap ;
23
23
import org .apache .commons .collections4 .multimap .ArrayListValuedHashMap ;
24
24
25
- import ghidra .app .util .bin .BinaryReader ;
26
- import ghidra .app .util .bin .ByteProvider ;
25
+ import ghidra .app .util .bin .*;
27
26
import ghidra .app .util .bin .format .dwarf4 .*;
28
27
import ghidra .app .util .bin .format .dwarf4 .attribs .DWARFAttributeFactory ;
29
28
import ghidra .app .util .bin .format .dwarf4 .encoding .*;
30
29
import ghidra .app .util .bin .format .dwarf4 .expression .DWARFExpressionException ;
31
30
import ghidra .app .util .bin .format .dwarf4 .next .sectionprovider .*;
32
31
import ghidra .app .util .opinion .ElfLoader ;
33
32
import ghidra .app .util .opinion .MachoLoader ;
33
+ import ghidra .program .model .address .Address ;
34
+ import ghidra .program .model .address .AddressSet ;
34
35
import ghidra .program .model .data .CategoryPath ;
35
36
import ghidra .program .model .listing .Program ;
36
37
import ghidra .program .model .symbol .SymbolUtilities ;
@@ -108,7 +109,7 @@ else if (MachoLoader.MACH_O_NAME.equals(format) &&
108
109
new HashMap <>();
109
110
110
111
private BinaryReader debugLocation ;
111
- private ByteProvider debugRanges ;
112
+ private BinaryReader debugRanges ;
112
113
private BinaryReader debugInfoBR ;
113
114
private BinaryReader debugLineBR ;
114
115
private BinaryReader debugAbbrBR ;
@@ -185,10 +186,7 @@ public DWARFProgram(Program program, DWARFImportOptions importOptions, TaskMonit
185
186
this .importOptions = importOptions ;
186
187
this .nameLengthCutoffSize = Math .max (MIN_NAME_LENGTH_CUTOFF ,
187
188
Math .min (importOptions .getNameLengthCutoff (), MAX_NAME_LENGTH_CUTOFF ));
188
- Long oib = ElfLoader .getElfOriginalImageBase (program );
189
- if (oib != null && oib .longValue () != program .getImageBase ().getOffset ()) {
190
- this .programBaseAddressFixup = program .getImageBase ().getOffset () - oib .longValue ();
191
- }
189
+
192
190
193
191
monitor .setMessage ("Reading DWARF debug string table" );
194
192
this .debugStrings = StringTable .readStringTable (
@@ -201,7 +199,17 @@ public DWARFProgram(Program program, DWARFImportOptions importOptions, TaskMonit
201
199
this .debugInfoBR = getBinaryReaderFor (DWARFSectionNames .DEBUG_INFO );
202
200
this .debugLineBR = getBinaryReaderFor (DWARFSectionNames .DEBUG_LINE );
203
201
this .debugAbbrBR = getBinaryReaderFor (DWARFSectionNames .DEBUG_ABBREV );
204
- this .debugRanges = sectionProvider .getSectionAsByteProvider (DWARFSectionNames .DEBUG_RANGES );
202
+ this .debugRanges = getBinaryReaderFor (DWARFSectionNames .DEBUG_RANGES );// sectionProvider.getSectionAsByteProvider(DWARFSectionNames.DEBUG_RANGES);
203
+
204
+ // if there are relocations (already handled by the ghidra loader) anywhere in the debuginfo or debugrange sections, then
205
+ // we don't need to manually fix up addresses extracted from DWARF data.
206
+ boolean hasRelocations = hasRelocations (debugInfoBR ) || hasRelocations (debugRanges );
207
+ if (!hasRelocations ) {
208
+ Long oib = ElfLoader .getElfOriginalImageBase (program );
209
+ if (oib != null && oib .longValue () != program .getImageBase ().getOffset ()) {
210
+ this .programBaseAddressFixup = program .getImageBase ().getOffset () - oib .longValue ();
211
+ }
212
+ }
205
213
206
214
dwarfRegisterMappings =
207
215
DWARFRegisterMappingsManager .hasDWARFRegisterMapping (program .getLanguage ())
@@ -219,10 +227,7 @@ public void close() throws IOException {
219
227
debugInfoBR = null ;
220
228
debugLineBR = null ;
221
229
debugLocation = null ;
222
- if (debugRanges != null ) {
223
- debugRanges .close ();
224
- debugRanges = null ;
225
- }
230
+ debugRanges = null ;
226
231
debugStrings .clear ();
227
232
dniCache .clear ();
228
233
clearDIEIndexes ();
@@ -249,6 +254,23 @@ private BinaryReader getBinaryReaderFor(String sectionName) throws IOException {
249
254
return (bp != null ) ? new BinaryReader (bp , !isBigEndian ()) : null ;
250
255
}
251
256
257
+ private boolean hasRelocations (BinaryReader br ) throws IOException {
258
+ if (br == null ) {
259
+ return false ;
260
+ }
261
+ ByteProvider bp = br .getByteProvider ();
262
+ if (bp instanceof MemoryByteProvider && bp .length () > 0 ) {
263
+ MemoryByteProvider mbp = (MemoryByteProvider ) bp ;
264
+ Address startAddr = mbp .getAddress (0 );
265
+ Address endAddr = mbp .getAddress (mbp .length () - 1 );
266
+ if (program .getRelocationTable ().getRelocations (
267
+ new AddressSet (startAddr , endAddr )).hasNext ()) {
268
+ return true ;
269
+ }
270
+ }
271
+ return false ;
272
+ }
273
+
252
274
//-------------------------------------------------------------------------
253
275
private static boolean isAnonDWARFName (String name ) {
254
276
return (name == null ) || name .startsWith ("._" ) || name .startsWith ("<anonymous" );
@@ -594,7 +616,7 @@ public BinaryReader getDebugLocation() {
594
616
return debugLocation ;
595
617
}
596
618
597
- public ByteProvider getDebugRanges () {
619
+ public BinaryReader getDebugRanges () {
598
620
return debugRanges ;
599
621
}
600
622
0 commit comments