11/*
22 * Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
3- * Copyright (c) 2020, Red Hat Inc. All rights reserved.
3+ * Copyright (c) 2020, 2020, Red Hat Inc. All rights reserved.
44 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55 *
66 * This code is free software; you can redistribute it and/or modify it
2424 * questions.
2525 */
2626
27- package com .oracle .objectfile .elf .dwarf ;
27+ package com .oracle .objectfile .debugentry ;
28+
29+ import com .oracle .objectfile .debuginfo .DebugInfoProvider ;
30+ import org .graalvm .compiler .debug .DebugContext ;
2831
2932import java .nio .ByteOrder ;
3033import java .nio .file .Path ;
3134import java .util .HashMap ;
3235import java .util .LinkedList ;
3336import java .util .List ;
3437import java .util .Map ;
35-
36- import org .graalvm .compiler .debug .DebugContext ;
37-
38- import com .oracle .objectfile .debugentry .ClassEntry ;
39- import com .oracle .objectfile .debugentry .DirEntry ;
40- import com .oracle .objectfile .debugentry .FileEntry ;
41- import com .oracle .objectfile .debugentry .Range ;
42- import com .oracle .objectfile .debugentry .StringTable ;
43- import com .oracle .objectfile .debuginfo .DebugInfoProvider ;
44- import com .oracle .objectfile .debuginfo .DebugInfoProvider .DebugFrameSizeChange ;
45- import com .oracle .objectfile .elf .ELFMachine ;
46-
4738/**
48- * A class that models the debug info in an organization that facilitates generation of the required
49- * DWARF sections. It groups common data and behaviours for use by the various subclasses of class
50- * DwarfSectionImpl that take responsibility for generating content for a specific section type.
39+ * An abstract class which indexes the information presented by the DebugInfoProvider
40+ * in an organization suitable for use by subclasses targeting a specific binary format.
5141 */
52- public class DwarfSections {
53-
54- /*
55- * Names of the different ELF sections we create or reference in reverse dependency order.
56- */
57- public static final String TEXT_SECTION_NAME = ".text" ;
58- public static final String DW_STR_SECTION_NAME = ".debug_str" ;
59- public static final String DW_LINE_SECTION_NAME = ".debug_line" ;
60- public static final String DW_FRAME_SECTION_NAME = ".debug_frame" ;
61- public static final String DW_ABBREV_SECTION_NAME = ".debug_abbrev" ;
62- public static final String DW_INFO_SECTION_NAME = ".debug_info" ;
63- public static final String DW_ARANGES_SECTION_NAME = ".debug_aranges" ;
64-
65- /**
66- * Currently generated debug info relies on DWARF spec vesion 2.
67- */
68- public static final short DW_VERSION_2 = 2 ;
69-
70- /*
71- * Define all the abbrev section codes we need for our DIEs.
72- */
73- @ SuppressWarnings ("unused" ) public static final int DW_ABBREV_CODE_null = 0 ;
74- public static final int DW_ABBREV_CODE_compile_unit = 1 ;
75- public static final int DW_ABBREV_CODE_subprogram = 2 ;
76-
77- /*
78- * Define all the Dwarf tags we need for our DIEs.
79- */
80- public static final int DW_TAG_compile_unit = 0x11 ;
81- public static final int DW_TAG_subprogram = 0x2e ;
82- /*
83- * Define all the Dwarf attributes we need for our DIEs.
84- */
85- public static final int DW_AT_null = 0x0 ;
86- public static final int DW_AT_name = 0x3 ;
87- @ SuppressWarnings ("unused" ) public static final int DW_AT_comp_dir = 0x1b ;
88- public static final int DW_AT_stmt_list = 0x10 ;
89- public static final int DW_AT_low_pc = 0x11 ;
90- public static final int DW_AT_hi_pc = 0x12 ;
91- public static final int DW_AT_language = 0x13 ;
92- public static final int DW_AT_external = 0x3f ;
93- @ SuppressWarnings ("unused" ) public static final int DW_AT_return_addr = 0x2a ;
94- @ SuppressWarnings ("unused" ) public static final int DW_AT_frame_base = 0x40 ;
95- /*
96- * Define all the Dwarf attribute forms we need for our DIEs.
97- */
98- public static final int DW_FORM_null = 0x0 ;
99- @ SuppressWarnings ("unused" ) private static final int DW_FORM_string = 0x8 ;
100- public static final int DW_FORM_strp = 0xe ;
101- public static final int DW_FORM_addr = 0x1 ;
102- public static final int DW_FORM_data1 = 0x0b ;
103- public static final int DW_FORM_data4 = 0x6 ;
104- @ SuppressWarnings ("unused" ) public static final int DW_FORM_data8 = 0x7 ;
105- @ SuppressWarnings ("unused" ) public static final int DW_FORM_block1 = 0x0a ;
106- public static final int DW_FORM_flag = 0xc ;
107-
108- /*
109- * Define specific attribute values for given attribute or form types.
110- */
111- /*
112- * DIE header has_children attribute values.
113- */
114- public static final byte DW_CHILDREN_no = 0 ;
115- public static final byte DW_CHILDREN_yes = 1 ;
116- /*
117- * DW_FORM_flag attribute values.
118- */
119- @ SuppressWarnings ("unused" ) public static final byte DW_FLAG_false = 0 ;
120- public static final byte DW_FLAG_true = 1 ;
121- /*
122- * Value for DW_AT_language attribute with form DATA1.
123- */
124- public static final byte DW_LANG_Java = 0xb ;
125-
126- /*
127- * DW_AT_Accessibility attribute values.
128- *
129- * These are not needed until we make functions members.
130- */
131- @ SuppressWarnings ("unused" ) public static final byte DW_ACCESS_public = 1 ;
132- @ SuppressWarnings ("unused" ) public static final byte DW_ACCESS_protected = 2 ;
133- @ SuppressWarnings ("unused" ) public static final byte DW_ACCESS_private = 3 ;
134-
135- /*
136- * Others that are not yet needed.
137- */
138- @ SuppressWarnings ("unused" ) public static final int DW_AT_type = 0 ; // only present for non-void
139- // functions
140- @ SuppressWarnings ("unused" ) public static final int DW_AT_accessibility = 0 ;
141-
142- /*
143- * CIE and FDE entries.
144- */
145-
146- /* Full byte/word values. */
147- public static final int DW_CFA_CIE_id = -1 ;
148- @ SuppressWarnings ("unused" ) public static final int DW_CFA_FDE_id = 0 ;
149-
150- public static final byte DW_CFA_CIE_version = 1 ;
151-
152- /* Values encoded in high 2 bits. */
153- public static final byte DW_CFA_advance_loc = 0x1 ;
154- public static final byte DW_CFA_offset = 0x2 ;
155- @ SuppressWarnings ("unused" ) public static final byte DW_CFA_restore = 0x3 ;
156-
157- /* Values encoded in low 6 bits. */
158- public static final byte DW_CFA_nop = 0x0 ;
159- @ SuppressWarnings ("unused" ) public static final byte DW_CFA_set_loc1 = 0x1 ;
160- public static final byte DW_CFA_advance_loc1 = 0x2 ;
161- public static final byte DW_CFA_advance_loc2 = 0x3 ;
162- public static final byte DW_CFA_advance_loc4 = 0x4 ;
163- @ SuppressWarnings ("unused" ) public static final byte DW_CFA_offset_extended = 0x5 ;
164- @ SuppressWarnings ("unused" ) public static final byte DW_CFA_restore_extended = 0x6 ;
165- @ SuppressWarnings ("unused" ) public static final byte DW_CFA_undefined = 0x7 ;
166- @ SuppressWarnings ("unused" ) public static final byte DW_CFA_same_value = 0x8 ;
167- public static final byte DW_CFA_register = 0x9 ;
168- public static final byte DW_CFA_def_cfa = 0xc ;
169- @ SuppressWarnings ("unused" ) public static final byte DW_CFA_def_cfa_register = 0xd ;
170- public static final byte DW_CFA_def_cfa_offset = 0xe ;
171-
172- private ByteOrder byteOrder ;
173- private DwarfStrSectionImpl dwarfStrSection ;
174- private DwarfAbbrevSectionImpl dwarfAbbrevSection ;
175- private DwarfInfoSectionImpl dwarfInfoSection ;
176- private DwarfARangesSectionImpl dwarfARangesSection ;
177- private DwarfLineSectionImpl dwarfLineSection ;
178- private DwarfFrameSectionImpl dwarfFameSection ;
179-
180- public DwarfSections (ELFMachine elfMachine , ByteOrder byteOrder ) {
181- this .byteOrder = byteOrder ;
182- dwarfStrSection = new DwarfStrSectionImpl (this );
183- dwarfAbbrevSection = new DwarfAbbrevSectionImpl (this );
184- dwarfInfoSection = new DwarfInfoSectionImpl (this );
185- dwarfARangesSection = new DwarfARangesSectionImpl (this );
186- dwarfLineSection = new DwarfLineSectionImpl (this );
187- if (elfMachine == ELFMachine .AArch64 ) {
188- dwarfFameSection = new DwarfFrameSectionImplAArch64 (this );
189- } else {
190- dwarfFameSection = new DwarfFrameSectionImplX86_64 (this );
191- }
192- }
193-
194- public DwarfStrSectionImpl getStrSectionImpl () {
195- return dwarfStrSection ;
196- }
197-
198- public DwarfAbbrevSectionImpl getAbbrevSectionImpl () {
199- return dwarfAbbrevSection ;
200- }
201-
202- public DwarfFrameSectionImpl getFrameSectionImpl () {
203- return dwarfFameSection ;
204- }
205-
206- public DwarfInfoSectionImpl getInfoSectionImpl () {
207- return dwarfInfoSection ;
208- }
209-
210- public DwarfARangesSectionImpl getARangesSectionImpl () {
211- return dwarfARangesSection ;
212- }
213-
214- public DwarfLineSectionImpl getLineSectionImpl () {
215- return dwarfLineSection ;
216- }
217-
42+ public abstract class DebugInfoBase {
43+ protected ByteOrder byteOrder ;
21844 /**
21945 * A table listing all known strings, some of which may be marked for insertion into the
22046 * debug_str section.
22147 */
222- private StringTable stringTable = new StringTable ();
223-
48+ protected StringTable stringTable = new StringTable ();
22449 /**
22550 * Index of all dirs in which files are found to reside either as part of substrate/compiler or
22651 * user code.
@@ -265,23 +90,18 @@ public DwarfLineSectionImpl getLineSectionImpl() {
26590 * index of already seen classes.
26691 */
26792 private Map <String , ClassEntry > primaryClassesIndex = new HashMap <>();
268-
26993 /**
27094 * Index of files which contain primary or secondary ranges.
27195 */
27296 private Map <Path , FileEntry > filesIndex = new HashMap <>();
273-
27497 /**
275- * Indirects this call to the string table.
276- *
277- * @param string the string whose index is required.
278- *
279- * @return the offset of the string in the .debug_str section.
98+ * List of of files which contain primary or secondary ranges.
28099 */
281- public int debugStringIndex (String string ) {
282- return stringTable .debugStringIndex (string );
283- }
100+ private LinkedList <FileEntry > files = new LinkedList <>();
284101
102+ public DebugInfoBase (ByteOrder byteOrder ) {
103+ this .byteOrder = byteOrder ;
104+ }
285105 /**
286106 * Entry point allowing ELFObjectFile to pass on information about types, code and heap data.
287107 *
@@ -347,7 +167,6 @@ public void installDebugInfo(DebugInfoProvider debugInfoProvider) {
347167 * String name = debugDataInfo.toString(); }
348168 */
349169 }
350-
351170 private ClassEntry ensureClassEntry (Range range ) {
352171 String className = range .getClassName ();
353172 /*
@@ -366,7 +185,6 @@ private ClassEntry ensureClassEntry(Range range) {
366185 assert classEntry .getClassName ().equals (className );
367186 return classEntry ;
368187 }
369-
370188 private FileEntry ensureFileEntry (Range range ) {
371189 String fileName = range .getFileName ();
372190 if (fileName == null ) {
@@ -381,6 +199,7 @@ private FileEntry ensureFileEntry(Range range) {
381199 if (fileEntry == null ) {
382200 DirEntry dirEntry = ensureDirEntry (filePath );
383201 fileEntry = new FileEntry (fileName , dirEntry );
202+ files .add (fileEntry );
384203 /*
385204 * Index the file entry by file path.
386205 */
@@ -394,13 +213,11 @@ private FileEntry ensureFileEntry(Range range) {
394213 }
395214 return fileEntry ;
396215 }
397-
398- private void addRange (Range primaryRange , List <DebugFrameSizeChange > frameSizeInfos , int frameSize ) {
216+ private void addRange (Range primaryRange , List <DebugInfoProvider .DebugFrameSizeChange > frameSizeInfos , int frameSize ) {
399217 assert primaryRange .isPrimary ();
400218 ClassEntry classEntry = ensureClassEntry (primaryRange );
401219 classEntry .addPrimary (primaryRange , frameSizeInfos , frameSize );
402220 }
403-
404221 private void addSubRange (Range primaryRange , Range subrange ) {
405222 assert primaryRange .isPrimary ();
406223 assert !subrange .isPrimary ();
@@ -415,7 +232,6 @@ private void addSubRange(Range primaryRange, Range subrange) {
415232 classEntry .addSubRange (subrange , subrangeFileEntry );
416233 }
417234 }
418-
419235 private DirEntry ensureDirEntry (Path filePath ) {
420236 if (filePath == null ) {
421237 return null ;
@@ -427,16 +243,32 @@ private DirEntry ensureDirEntry(Path filePath) {
427243 }
428244 return dirEntry ;
429245 }
430-
431- public StringTable getStringTable () {
432- return stringTable ;
246+ /* Accessors to query the debug info model. */
247+ public ByteOrder getByteOrder () {
248+ return byteOrder ;
433249 }
434-
435250 public LinkedList <ClassEntry > getPrimaryClasses () {
436251 return primaryClasses ;
437252 }
438-
439- public ByteOrder getByteOrder () {
440- return byteOrder ;
253+ @ SuppressWarnings ("unused" )
254+ public LinkedList <FileEntry > getFiles () {
255+ return files ;
256+ }
257+ @ SuppressWarnings ("unused" )
258+ public FileEntry findFile (Path fullFileName ) {
259+ return filesIndex .get (fullFileName );
260+ }
261+ public StringTable getStringTable () {
262+ return stringTable ;
263+ }
264+ /**
265+ * Indirects this call to the string table.
266+ *
267+ * @param string the string whose index is required.
268+ *
269+ * @return the offset of the string in the .debug_str section.
270+ */
271+ public int debugStringIndex (String string ) {
272+ return stringTable .debugStringIndex (string );
441273 }
442274}
0 commit comments