1 /*
   2  * Copyright (c) 2017, 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.elf;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.util.ArrayList;
  30 
  31 import jdk.tools.jaotc.binformat.elf.Elf.Elf64_Sym;
  32 
  33 final class ElfSymtab {
  34 
  35     private final ArrayList<ElfSymbol> localSymbols = new ArrayList<>();
  36     private final ArrayList<ElfSymbol> globalSymbols = new ArrayList<>();
  37 
  38     /**
  39      * Number of symbols added.
  40      */
  41     private int symbolCount;
  42 
  43     /**
  44      * String holding symbol table strings.
  45      */
  46     private final StringBuilder strTabContent = new StringBuilder();
  47 
  48     /**
  49      * Keeps track of bytes in string table since strTabContent.length() is number of chars, not
  50      * bytes.
  51      */
  52     private int strTabNrOfBytes = 0;
  53 
  54     ElfSymtab() {
  55         symbolCount = 0;
  56     }
  57 
  58     ElfSymbol addSymbolEntry(String name, byte type, byte bind, byte secHdrIndex, long offset, long size) {
  59         // Get the current symbol index and append symbol name to string table.
  60         int index;
  61         ElfSymbol sym;
  62 
  63         if (name.isEmpty()) {
  64             index = 0;
  65             strTabContent.append('\0');
  66             strTabNrOfBytes += 1;
  67             sym = new ElfSymbol(symbolCount, index, type, bind, secHdrIndex, offset, size);
  68             localSymbols.add(sym);
  69         } else {
  70             // We can't trust strTabContent.length() since that is
  71             // chars (UTF16), keep track of bytes on our own.
  72             index = strTabNrOfBytes;
  73             // strTabContent.append("_").append(name).append('\0');
  74             strTabContent.append(name).append('\0');
  75             // + 1 for null, + 1 for "_"
  76             // strTabNrOfBytes += (name.getBytes().length + 1 + 1);
  77             strTabNrOfBytes += (name.getBytes().length + 1);
  78 
  79             sym = new ElfSymbol(symbolCount, index, type, bind, secHdrIndex, offset, size);
  80             if ((bind & Elf64_Sym.STB_GLOBAL) != 0) {
  81                 globalSymbols.add(sym);
  82             } else {
  83                 localSymbols.add(sym);
  84             }
  85         }
  86         symbolCount++;
  87         return (sym);
  88     }
  89 
  90     // Update the symbol indexes once all symbols have been added.
  91     // This is required since we'll be reordering the symbols in the
  92     // file to be in the order of Local then global.
  93     void updateIndexes() {
  94         int index = 0;
  95 
  96         // Update the local symbol indexes
  97         for (int i = 0; i < localSymbols.size(); i++) {
  98             ElfSymbol sym = localSymbols.get(i);
  99             sym.setIndex(index++);
 100         }
 101 
 102         // Update the global symbol indexes
 103         for (int i = 0; i < globalSymbols.size(); i++) {
 104             ElfSymbol sym = globalSymbols.get(i);
 105             sym.setIndex(index++);
 106         }
 107     }
 108 
 109     int getNumLocalSyms() {
 110         return localSymbols.size();
 111     }
 112 
 113     int getNumGlobalSyms() {
 114         return globalSymbols.size();
 115     }
 116 
 117     // Create a single byte array that contains the symbol table entries
 118     byte[] getSymtabArray() {
 119         ByteBuffer symtabData = ElfByteBuffer.allocate(symbolCount * Elf64_Sym.totalsize);
 120         byte[] retarray;
 121 
 122         updateIndexes();
 123 
 124         // Add the local symbols
 125         for (int i = 0; i < localSymbols.size(); i++) {
 126             ElfSymbol sym = localSymbols.get(i);
 127             byte[] arr = sym.getArray();
 128             symtabData.put(arr);
 129         }
 130         // Add the global symbols
 131         for (int i = 0; i < globalSymbols.size(); i++) {
 132             ElfSymbol sym = globalSymbols.get(i);
 133             byte[] arr = sym.getArray();
 134             symtabData.put(arr);
 135         }
 136         retarray = symtabData.array();
 137 
 138         return (retarray);
 139     }
 140 
 141     // Return the string table array
 142     byte[] getStrtabArray() {
 143         byte[] strs = strTabContent.toString().getBytes();
 144         return (strs);
 145     }
 146 }