@@ -134,6 +134,65 @@ public void TestDebugLineHelloWorld()
134134 Assert . AreEqual ( inputDebugLineBuffer , outputDebugLineBuffer ) ;
135135 }
136136
137+ [ Test ]
138+ public void TestDebugLineLibMultipleObjs ( )
139+ {
140+ var cppName = "lib" ;
141+ var libShared = $ "{ cppName } _debug.so";
142+ LinuxUtil . RunLinuxExe ( "gcc" , $ "{ cppName } _*.cpp -g -shared -o { libShared } ") ;
143+
144+ ElfObjectFile elf ;
145+ using ( var inStream = File . OpenRead ( libShared ) )
146+ {
147+ Console . WriteLine ( $ "ReadBack from { libShared } ") ;
148+ elf = ElfObjectFile . Read ( inStream ) ;
149+ elf . Print ( Console . Out ) ;
150+ }
151+
152+ var elfContext = new DwarfElfContext ( elf ) ;
153+ var inputContext = new DwarfReaderContext ( elfContext ) ;
154+ inputContext . DebugLinePrinter = Console . Out ;
155+ var dwarf = DwarfFile . Read ( inputContext ) ;
156+
157+ inputContext . DebugLineStream . Position = 0 ;
158+
159+ var copyInputDebugLineStream = new MemoryStream ( ) ;
160+ inputContext . DebugLineStream . CopyTo ( copyInputDebugLineStream ) ;
161+ inputContext . DebugLineStream . Position = 0 ;
162+
163+ var outputContext = new DwarfWriterContext
164+ {
165+ IsLittleEndian = inputContext . IsLittleEndian ,
166+ EnableRelocation = false ,
167+ AddressSize = inputContext . AddressSize ,
168+ DebugLineStream = new MemoryStream ( )
169+ } ;
170+ dwarf . Write ( outputContext ) ;
171+
172+ Console . WriteLine ( ) ;
173+ Console . WriteLine ( "=====================================================" ) ;
174+ Console . WriteLine ( "Readback" ) ;
175+ Console . WriteLine ( "=====================================================" ) ;
176+ Console . WriteLine ( ) ;
177+
178+ var reloadContext = new DwarfReaderContext ( )
179+ {
180+ IsLittleEndian = outputContext . IsLittleEndian ,
181+ AddressSize = outputContext . AddressSize ,
182+ DebugLineStream = outputContext . DebugLineStream
183+ } ;
184+
185+ reloadContext . DebugLineStream . Position = 0 ;
186+ reloadContext . DebugLineStream = outputContext . DebugLineStream ;
187+ reloadContext . DebugLinePrinter = Console . Out ;
188+
189+ var dwarf2 = DwarfFile . Read ( reloadContext ) ;
190+
191+ var inputDebugLineBuffer = copyInputDebugLineStream . ToArray ( ) ;
192+ var outputDebugLineBuffer = ( ( MemoryStream ) reloadContext . DebugLineStream ) . ToArray ( ) ;
193+ Assert . AreEqual ( inputDebugLineBuffer , outputDebugLineBuffer ) ;
194+ }
195+
137196 [ Test ]
138197 public void TestDebugLineSmall ( )
139198 {
@@ -274,30 +333,56 @@ public void CreateDwarf()
274333 // Create .debug_line information
275334 var fileName = new DwarfFileName ( )
276335 {
277- Name = "check.cpp" ,
336+ Name = "check1.cpp" ,
337+ Directory = Environment . CurrentDirectory ,
338+ } ;
339+ var fileName2 = new DwarfFileName ( )
340+ {
341+ Name = "check2.cpp" ,
278342 Directory = Environment . CurrentDirectory ,
279343 } ;
280- dwarfFile . LineTable . AddressSize = DwarfAddressSize . Bit64 ;
281- dwarfFile . LineTable . FileNames . Add ( fileName ) ;
282- dwarfFile . LineTable . AddLineSequence ( new DwarfLineSequence ( )
283- {
284344
285- new DwarfLine ( )
345+ // First line table
346+ for ( int i = 0 ; i < 2 ; i ++ )
347+ {
348+ var lineTable = new DwarfLineProgramTable ( ) ;
349+ dwarfFile . LineSection . AddLineProgramTable ( lineTable ) ;
350+
351+ lineTable . AddressSize = DwarfAddressSize . Bit64 ;
352+ lineTable . FileNames . Add ( fileName ) ;
353+ lineTable . FileNames . Add ( fileName2 ) ;
354+ lineTable . AddLineSequence ( new DwarfLineSequence ( )
286355 {
287- File = fileName ,
288- Address = 0 ,
289- Column = 1 ,
290- Line = 1 ,
291- } ,
292- new DwarfLine ( )
356+
357+ new DwarfLine ( )
358+ {
359+ File = fileName ,
360+ Address = 0 ,
361+ Column = 1 ,
362+ Line = 1 ,
363+ } ,
364+ new DwarfLine ( )
365+ {
366+ File = fileName ,
367+ Address = 1 ,
368+ Column = 1 ,
369+ Line = 2 ,
370+ }
371+ }
372+ ) ;
373+ lineTable . AddLineSequence ( new DwarfLineSequence ( )
293374 {
294- File = fileName ,
295- Address = 0 ,
296- Column = 1 ,
297- Line = 1 ,
375+
376+ new DwarfLine ( )
377+ {
378+ File = fileName2 ,
379+ Address = 0 ,
380+ Column = 1 ,
381+ Line = 1 ,
382+ } ,
298383 }
299- }
300- ) ;
384+ ) ;
385+ }
301386
302387 // Create .debug_info
303388 var rootDIE = new DwarfDIECompileUnit ( )
@@ -306,11 +391,11 @@ public void CreateDwarf()
306391 LowPC = 0 , // 0 relative to base virtual address
307392 HighPC = ( int ) codeSection . Size , // default is offset/length after LowPC
308393 CompDir = fileName . Directory ,
309- StmtList = dwarfFile . LineTable . LineSequences [ 0 ] ,
394+ StmtList = dwarfFile . LineSection . LineTables [ 0 ] ,
310395 } ;
311396 var subProgram = new DwarfDIESubprogram ( )
312397 {
313- Name = "MyFunction"
398+ Name = "MyFunction" ,
314399 } ;
315400 rootDIE . AddChild ( subProgram ) ;
316401
@@ -330,7 +415,8 @@ public void CreateDwarf()
330415 var dwarfElfContext = new DwarfElfContext ( elf ) ;
331416 dwarfFile . WriteToElf ( dwarfElfContext ) ;
332417
333- using ( var output = new FileStream ( "check.o" , FileMode . Create ) )
418+ var outputFileName = "create_dwarf.o" ;
419+ using ( var output = new FileStream ( outputFileName , FileMode . Create ) )
334420 {
335421 elf . Write ( output ) ;
336422 }
@@ -342,6 +428,10 @@ public void CreateDwarf()
342428 dwarfFile . AddressRangeTable . Print ( Console . Out ) ;
343429 Console . WriteLine ( ) ;
344430 dwarfFile . InfoSection . Print ( Console . Out ) ;
431+
432+ Console . WriteLine ( "ReadBack --debug-dump=rawline" ) ;
433+ var readelf = LinuxUtil . ReadElf ( outputFileName , "--debug-dump=rawline" ) . TrimEnd ( ) ;
434+ Console . WriteLine ( readelf ) ;
345435 }
346436
347437 private static void PrintStreamLength ( DwarfReaderWriterContext context )
0 commit comments