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 package jdk.tools.jaotc.binformat.pecoff; 25 26 import java.util.ArrayList; 27 import java.nio.ByteBuffer; 28 import java.nio.ByteOrder; 29 30 import jdk.tools.jaotc.binformat.pecoff.PECoff; 31 import jdk.tools.jaotc.binformat.pecoff.PECoff.IMAGE_RELOCATION; 32 import jdk.tools.jaotc.binformat.pecoff.PECoffRelocEntry; 33 import jdk.tools.jaotc.binformat.pecoff.PECoffByteBuffer; 34 35 public class PECoffRelocTable { 36 ArrayList<ArrayList<PECoffRelocEntry>> relocEntries; 37 38 public PECoffRelocTable(int numsects) { 39 relocEntries = new ArrayList<ArrayList<PECoffRelocEntry>>(numsects); 40 for (int i = 0; i < numsects; i++) 41 relocEntries.add(new ArrayList<PECoffRelocEntry>()); 42 } 43 44 public void createRelocationEntry(int sectindex, 45 int offset, 46 int symno, 47 int type) { 48 49 PECoffRelocEntry entry = new PECoffRelocEntry(offset, 50 symno, 51 type); 52 relocEntries.get(sectindex).add(entry); 53 } 54 55 public int getAlign() { return (4); } 56 57 public int getNumRelocs(int section_index) { 58 return relocEntries.get(section_index).size(); 59 } 60 61 // Return the relocation entries for a single section 62 // or null if no entries added to section 63 public byte [] getRelocData(int section_index) { 64 ArrayList<PECoffRelocEntry> entryList = relocEntries.get(section_index); 65 int entryCount = entryList.size(); 66 int allocCount = entryCount; 67 68 if (entryCount == 0) 69 return null; 70 71 if (entryCount > 0xFFFF) 72 allocCount++; 73 74 ByteBuffer relocData = PECoffByteBuffer.allocate(allocCount * IMAGE_RELOCATION.totalsize); 75 76 // If number of relocs exceeds 65K, add the real size 77 // in a dummy first reloc entry 78 if (entryCount > 0xFFFF) { 79 PECoffRelocEntry entry = new PECoffRelocEntry(allocCount, 0, 0); 80 relocData.put(entry.getArray()); 81 } 82 83 // Copy each entry to a single ByteBuffer 84 for (int i = 0; i < entryCount; i++) { 85 PECoffRelocEntry entry = entryList.get(i); 86 relocData.put(entry.getArray()); 87 } 88 89 return (relocData.array()); 90 } 91 } 92 | 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 package jdk.tools.jaotc.binformat.pecoff; 25 26 import java.util.ArrayList; 27 import java.nio.ByteBuffer; 28 29 import jdk.tools.jaotc.binformat.pecoff.PECoff.IMAGE_RELOCATION; 30 import jdk.tools.jaotc.binformat.pecoff.PECoffRelocEntry; 31 import jdk.tools.jaotc.binformat.pecoff.PECoffByteBuffer; 32 33 final class PECoffRelocTable { 34 ArrayList<ArrayList<PECoffRelocEntry>> relocEntries; 35 36 PECoffRelocTable(int numsects) { 37 relocEntries = new ArrayList<>(numsects); 38 for (int i = 0; i < numsects; i++) { 39 relocEntries.add(new ArrayList<PECoffRelocEntry>()); 40 } 41 } 42 43 void createRelocationEntry(int sectindex, int offset, int symno, int type) { 44 PECoffRelocEntry entry = new PECoffRelocEntry(offset, symno, type); 45 relocEntries.get(sectindex).add(entry); 46 } 47 48 static int getAlign() { 49 return (4); 50 } 51 52 int getNumRelocs(int section_index) { 53 return relocEntries.get(section_index).size(); 54 } 55 56 // Return the relocation entries for a single section 57 // or null if no entries added to section 58 byte[] getRelocData(int section_index) { 59 ArrayList<PECoffRelocEntry> entryList = relocEntries.get(section_index); 60 int entryCount = entryList.size(); 61 int allocCount = entryCount; 62 63 if (entryCount == 0) { 64 return null; 65 } 66 if (entryCount > 0xFFFF) { 67 allocCount++; 68 } 69 ByteBuffer relocData = PECoffByteBuffer.allocate(allocCount * IMAGE_RELOCATION.totalsize); 70 71 // If number of relocs exceeds 65K, add the real size 72 // in a dummy first reloc entry 73 if (entryCount > 0xFFFF) { 74 PECoffRelocEntry entry = new PECoffRelocEntry(allocCount, 0, 0); 75 relocData.put(entry.getArray()); 76 } 77 78 // Copy each entry to a single ByteBuffer 79 for (int i = 0; i < entryCount; i++) { 80 PECoffRelocEntry entry = entryList.get(i); 81 relocData.put(entry.getArray()); 82 } 83 84 return (relocData.array()); 85 } 86 } |