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