--- /dev/null 2016-12-30 14:52:30.584485998 -0500 +++ new/src/jdk.aot/share/classes/jdk.tools.jaotc.binformat/src/jdk/tools/jaotc/binformat/macho/MachOSymtab.java 2017-02-08 13:42:24.989000440 -0500 @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.tools.jaotc.binformat.macho; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.ArrayList; + +import jdk.tools.jaotc.binformat.macho.MachO; +import jdk.tools.jaotc.binformat.macho.MachO.symtab_command; +import jdk.tools.jaotc.binformat.macho.MachO.nlist_64; +import jdk.tools.jaotc.binformat.macho.MachOSymbol; +import jdk.tools.jaotc.binformat.macho.MachOByteBuffer; + +public class MachOSymtab { + + /** + * ByteBuffer holding the LC_SYMTAB command contents + */ + ByteBuffer symtabCmd; + + /** + * ByteBuffer holding the symbol table entries and strings + */ + ByteBuffer symtabData; + + int symtabDataSize; + + ArrayListlocalSymbols = new ArrayList(); + ArrayListglobalSymbols = new ArrayList(); + ArrayListundefSymbols = new ArrayList(); + + /** + * number of symbols added + */ + int symbolCount; + + /** + * String holding symbol table strings + */ + private StringBuilder strTabContent = new StringBuilder(); + + /** + * Keeps track of bytes in string table since strTabContent.length() + * is number of chars, not bytes. + */ + private int strTabNrOfBytes = 0; + + public MachOSymtab() { + symtabCmd = MachOByteBuffer.allocate(symtab_command.totalsize); + + symtabCmd.putInt(symtab_command.cmd.off, symtab_command.LC_SYMTAB); + symtabCmd.putInt(symtab_command.cmdsize.off, symtab_command.totalsize); + + symbolCount = 0; + + } + + public int getAlign() { + return (4); + } + + public MachOSymbol addSymbolEntry(String name, byte type, byte secHdrIndex, long offset) { + // Get the current symbol index and append symbol name to string table. + int index; + MachOSymbol sym; + + if (name.isEmpty()) { + index = 0; + strTabContent.append('\0'); + strTabNrOfBytes += 1; + sym = new MachOSymbol(symbolCount, index, type, secHdrIndex, offset); + localSymbols.add(sym); + } else { + // We can't trust strTabContent.length() since that is + // chars (UTF16), keep track of bytes on our own. + index = strTabNrOfBytes; + strTabContent.append("_").append(name).append('\0'); + // + 1 for null, + 1 for "_" + strTabNrOfBytes += (name.getBytes().length + 1 + 1); + + sym = new MachOSymbol(symbolCount, index, type, secHdrIndex, offset); + switch (type) { + case nlist_64.N_EXT: + undefSymbols.add(sym); + break; + case nlist_64.N_SECT: + case nlist_64.N_UNDF: // null symbol + localSymbols.add(sym); + break; + case nlist_64.N_SECT|nlist_64.N_EXT: + globalSymbols.add(sym); + break; + default: + System.out.println("Unsupported Symbol type " + type); + break; + } + } + symbolCount++; + return (sym); + } + + public void setOffset(int symoff) { + symtabCmd.putInt(symtab_command.symoff.off, symoff); + } + + // Update the symbol indexes once all symbols have been added. + // This is required since we'll be reordering the symbols in the + // file to be in the order of Local, global and Undefined. + public void updateIndexes() { + int index = 0; + + // Update the local symbol indexes + for (int i = 0; i < localSymbols.size(); i++ ) { + MachOSymbol sym = localSymbols.get(i); + sym.setIndex(index++); + } + + // Update the global symbol indexes + for (int i = 0; i < globalSymbols.size(); i++ ) { + MachOSymbol sym = globalSymbols.get(i); + sym.setIndex(index++); + } + + // Update the undefined symbol indexes + for (int i = index; i < undefSymbols.size(); i++ ) { + MachOSymbol sym = undefSymbols.get(i); + sym.setIndex(index++); + } + } + + // Update LC_SYMTAB command fields based on the number of symbols added + // return the file size taken up by symbol table entries and strings + public int calcSizes() { + int stroff; + + stroff = symtabCmd.getInt(symtab_command.symoff.off) + (nlist_64.totalsize * symbolCount); + symtabCmd.putInt(symtab_command.nsyms.off, symbolCount); + symtabCmd.putInt(symtab_command.stroff.off, stroff); + symtabCmd.putInt(symtab_command.strsize.off, strTabNrOfBytes); + symtabDataSize = (nlist_64.totalsize * symbolCount) + strTabNrOfBytes; + + return (symtabDataSize); + } + + public int getNumLocalSyms() { return localSymbols.size(); } + public int getNumGlobalSyms() { return globalSymbols.size(); } + public int getNumUndefSyms() { return undefSymbols.size(); } + + public byte[] getCmdArray() { + return symtabCmd.array(); + } + + // Create a single byte array that contains the symbol table entries + // and string table + public byte[] getDataArray() { + int index = 0; + symtabData = MachOByteBuffer.allocate(symtabDataSize); + byte [] retarray; + + // Add the local symbols + for (int i = 0; i < localSymbols.size(); i++ ) { + MachOSymbol sym = localSymbols.get(i); + byte [] arr = sym.getArray(); + symtabData.put(arr); + } + // Add the global symbols + for (int i = 0; i < globalSymbols.size(); i++ ) { + MachOSymbol sym = globalSymbols.get(i); + byte [] arr = sym.getArray(); + symtabData.put(arr); + } + // Add the undefined symbols + for (int i = 0; i < undefSymbols.size(); i++ ) { + MachOSymbol sym = undefSymbols.get(i); + byte [] arr = sym.getArray(); + symtabData.put(arr); + } + + // Add the stringtable + byte [] strs = strTabContent.toString().getBytes(); + symtabData.put(strs); + + retarray = symtabData.array(); + + return (retarray); + } +} + +