001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.dwarfdump.reader;
043:
044: import java.io.RandomAccessFile;
045: import org.netbeans.modules.cnd.dwarfdump.Magic;
046: import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.ATE;
047: import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.ATTR;
048: import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.FORM;
049: import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.LANG;
050: import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.SECTIONS;
051: import org.netbeans.modules.cnd.dwarfdump.section.DwarfAbbriviationTableSection;
052: import org.netbeans.modules.cnd.dwarfdump.section.DwarfArangesSection;
053: import org.netbeans.modules.cnd.dwarfdump.section.DwarfDebugInfoSection;
054: import org.netbeans.modules.cnd.dwarfdump.section.DwarfLineInfoSection;
055: import org.netbeans.modules.cnd.dwarfdump.section.DwarfMacroInfoSection;
056: import org.netbeans.modules.cnd.dwarfdump.section.ElfSection;
057: import org.netbeans.modules.cnd.dwarfdump.section.StringTableSection;
058: import org.netbeans.modules.cnd.dwarfdump.section.DwarfAttribute;
059: import org.netbeans.modules.cnd.dwarfdump.section.DwarfNameLookupTableSection;
060: import java.io.IOException;
061:
062: /**
063: *
064: * @author ak119685
065: */
066: public class DwarfReader extends ElfReader {
067:
068: public DwarfReader(String fname, RandomAccessFile reader,
069: Magic magic, long shift, long length) throws IOException {
070: super (fname, reader, magic, shift, length);
071: getSection(SECTIONS.DEBUG_STR);
072: }
073:
074: public Object readAttrValue(DwarfAttribute attr) throws IOException {
075: Object value = readForm(attr.valueForm);
076:
077: if (attr.attrName.equals(ATTR.DW_AT_language)) {
078: return LANG.get(((Number) value).intValue());
079: }
080:
081: if (attr.attrName.equals(ATTR.DW_AT_encoding)) {
082: return ATE.get(((Byte) value).byteValue());
083: }
084:
085: if (attr.attrName.equals(ATTR.DW_AT_decl_line)) {
086: // if (attr.valueForm.equals(FORM.DW_FORM_data2)) {
087: // byte[] val = (byte[])value;
088: // return new Integer((0xFF & val[0]) | ((0xFF & val[1]) << 8));
089: // }
090: return new Integer(((Number) value).intValue());
091: }
092:
093: return value;
094: }
095:
096: public Object readForm(FORM form) throws IOException {
097: if (form.equals(FORM.DW_FORM_addr)) {
098: return read(new byte[getAddressSize()]);
099: } else if (form.equals(FORM.DW_FORM_block2)) {
100: return read(new byte[readShort()]);
101: } else if (form.equals(FORM.DW_FORM_block4)) {
102: return read(new byte[readInt()]);
103: } else if (form.equals(FORM.DW_FORM_data2)) {
104: //TODO: check on all architectures!
105: //return read(new byte[2]);
106: return readShort();
107: } else if (form.equals(FORM.DW_FORM_data4)) {
108: //TODO: check on all architectures!
109: //return read(new byte[4]);
110: return readInt();
111: } else if (form.equals(FORM.DW_FORM_data8)) {
112: //TODO: check on all architectures!
113: //return read(new byte[8]);
114: return readLong();
115: } else if (form.equals(FORM.DW_FORM_string)) {
116: return readString();
117: } else if (form.equals(FORM.DW_FORM_block)) {
118: return read(new byte[readUnsignedLEB128()]);
119: } else if (form.equals(FORM.DW_FORM_block1)) {
120: return read(new byte[readUnsignedByte()]);
121: } else if (form.equals(FORM.DW_FORM_data1)) {
122: return readByte();
123: } else if (form.equals(FORM.DW_FORM_flag)) {
124: return readBoolean();
125: } else if (form.equals(FORM.DW_FORM_sdata)) {
126: return readSignedLEB128();
127: } else if (form.equals(FORM.DW_FORM_strp)) {
128: return ((StringTableSection) getSection(SECTIONS.DEBUG_STR))
129: .getString(readInt());
130: } else if (form.equals(FORM.DW_FORM_udata)) {
131: return readUnsignedLEB128();
132: } else if (form.equals(FORM.DW_FORM_ref_addr)) {
133: return read(new byte[getAddressSize()]);
134: } else if (form.equals(FORM.DW_FORM_ref1)) {
135: return read(new byte[readUnsignedByte()]);
136: } else if (form.equals(FORM.DW_FORM_ref2)) {
137: return read(new byte[2]);
138: } else if (form.equals(FORM.DW_FORM_ref4)) {
139: return readInt();
140: } else if (form.equals(FORM.DW_FORM_ref8)) {
141: return read(new byte[8]);
142: } else if (form.equals(FORM.DW_FORM_ref_udata)) {
143: return read(new byte[readUnsignedLEB128()]);
144: } else if (form.equals(FORM.DW_FORM_indirect)) {
145: return readForm(FORM.get(readUnsignedLEB128()));
146: } else {
147: throw new IOException("unknown type " + form); // NOI18N
148: }
149: }
150:
151: @Override
152: ElfSection initSection(Integer sectionIdx, String sectionName) {
153: if (sectionName.equals(SECTIONS.DEBUG_STR)) {
154: return new StringTableSection(this , sectionIdx);
155: }
156:
157: if (sectionName.equals(SECTIONS.DEBUG_ARANGES)) {
158: return new DwarfArangesSection(this , sectionIdx);
159: }
160:
161: if (sectionName.equals(SECTIONS.DEBUG_INFO)) {
162: return new DwarfDebugInfoSection(this , sectionIdx);
163: }
164:
165: if (sectionName.equals(SECTIONS.DEBUG_ABBREV)) {
166: return new DwarfAbbriviationTableSection(this , sectionIdx);
167: }
168:
169: if (sectionName.equals(SECTIONS.DEBUG_LINE)) {
170: return new DwarfLineInfoSection(this , sectionIdx);
171: }
172:
173: if (sectionName.equals(SECTIONS.DEBUG_MACINFO)) {
174: return new DwarfMacroInfoSection(this , sectionIdx);
175: }
176:
177: if (sectionName.equals(SECTIONS.DEBUG_PUBNAMES)) {
178: return new DwarfNameLookupTableSection(this, sectionIdx);
179: }
180:
181: return null;
182: }
183: }
|