1 /*
   2  * Copyright (c) 2017, 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 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