1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 
  26 package jdk.tools.jaotc.binformat;
  27 
  28 import java.util.Objects;
  29 
  30 public class Symbol {
  31 
  32     // @formatter:off (workaround for Eclipse formatting bug)
  33     public enum Binding {
  34         UNDEFINED,
  35         LOCAL,
  36         GLOBAL
  37     }
  38 
  39     public enum Kind {
  40         UNDEFINED,
  41         NATIVE_FUNCTION,
  42         JAVA_FUNCTION,
  43         OBJECT,
  44         NOTYPE
  45     }
  46     // @formatter:on
  47 
  48     private final String name;
  49     private final int size;
  50     private final int offset;
  51     private final Binding binding;
  52     private final Kind kind;
  53 
  54     private ByteContainer section;
  55     private NativeSymbol nativeSymbol;
  56 
  57     /**
  58      * Create symbol info.
  59      *
  60      * @param offset section offset for the defined symbol
  61      * @param kind kind of the symbol (UNDEFINED, FUNC, etc)
  62      * @param binding binding of the symbol (LOCAL, GLOBAL, ...)
  63      * @param section section in which this symbol is "defined"
  64      * @param size size of the symbol
  65      * @param name name of the symbol
  66      */
  67 
  68     public Symbol(int offset, Kind kind, Binding binding, ByteContainer section, int size, String name) {
  69         this.binding = binding;
  70         this.kind = kind;
  71         this.section = section;
  72         this.size = size;
  73         this.offset = offset;
  74         this.name = name;
  75     }
  76 
  77     public String getName() {
  78         return name;
  79     }
  80 
  81     public NativeSymbol getNativeSymbol() {
  82         return nativeSymbol;
  83     }
  84 
  85     public void setNativeSymbol(NativeSymbol nativeSym) {
  86         this.nativeSymbol = nativeSym;
  87     }
  88 
  89     public Binding getBinding() {
  90         return binding;
  91     }
  92 
  93     public Kind getKind() {
  94         return kind;
  95     }
  96 
  97     public int getSize() {
  98         return size;
  99     }
 100 
 101     public ByteContainer getSection() {
 102         return section;
 103     }
 104 
 105     public int getOffset() {
 106         return offset;
 107     }
 108 
 109     @Override
 110     public boolean equals(Object obj) {
 111         if (this == obj) {
 112             return true;
 113         }
 114         if (!(obj instanceof Symbol)) {
 115             return false;
 116         }
 117         if (getClass() != obj.getClass()) {
 118             return false;
 119         }
 120 
 121         Symbol symbol = (Symbol) obj;
 122 
 123         if (size != symbol.size) {
 124             return false;
 125         }
 126         if (offset != symbol.offset) {
 127             return false;
 128         }
 129         if (!name.equals(symbol.name)) {
 130             return false;
 131         }
 132         if (binding != symbol.binding) {
 133             return false;
 134         }
 135         if (kind != symbol.kind) {
 136             return false;
 137         }
 138         return !(section != null ? !section.equals(symbol.section) : symbol.section != null);
 139     }
 140 
 141     @Override
 142     public int hashCode() {
 143         int result = Objects.hash(name, binding, kind, section);
 144         result = 31 * result + size;
 145         result = 31 * result + offset;
 146         return result;
 147     }
 148 
 149     @Override
 150     public String toString() {
 151         return "[" + name + ", " + size + ", " + offset + ", " + binding + ", " + kind + ", " + section + "]";
 152     }
 153 
 154 }