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.macho;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.util.ArrayList;
  30 
  31 import jdk.tools.jaotc.binformat.macho.MachO.reloc_info;
  32 
  33 final class MachORelocTable {
  34     private final ArrayList<ArrayList<MachORelocEntry>> relocEntries;
  35     int fileOffset;
  36 
  37     MachORelocTable(int numsects) {
  38         relocEntries = new ArrayList<>(numsects);
  39         for (int i = 0; i < numsects; i++) {
  40             relocEntries.add(new ArrayList<MachORelocEntry>());
  41         }
  42     }
  43 
  44     void createRelocationEntry(int sectindex, int offset, int symno, int pcrel, int length, int isextern, int type) {
  45         MachORelocEntry entry = new MachORelocEntry(offset, symno, pcrel, length, isextern, type);
  46         relocEntries.get(sectindex).add(entry);
  47     }
  48 
  49     static int getAlign() {
  50         return (4);
  51     }
  52 
  53     int getNumRelocs(int sectionIndex) {
  54         return relocEntries.get(sectionIndex).size();
  55     }
  56 
  57     // Return the relocation entries for a single section
  58     // or null if no entries added to section
  59     byte[] getRelocData(int sectionIndex) {
  60         ArrayList<MachORelocEntry> entryList = relocEntries.get(sectionIndex);
  61 
  62         if (entryList.size() == 0) {
  63             return null;
  64         }
  65         ByteBuffer relocData = MachOByteBuffer.allocate(entryList.size() * reloc_info.totalsize);
  66 
  67         // Copy each entry to a single ByteBuffer
  68         for (int i = 0; i < entryList.size(); i++) {
  69             MachORelocEntry entry = entryList.get(i);
  70             relocData.put(entry.getArray());
  71         }
  72 
  73         return (relocData.array());
  74     }
  75 }