1 /*
   2  * Copyright (c) 2016, 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.elf;
  25 
  26 import java.util.ArrayList;
  27 import java.nio.ByteBuffer;
  28 
  29 import jdk.tools.jaotc.binformat.elf.ElfRelocEntry;
  30 import jdk.tools.jaotc.binformat.elf.Elf.Elf64_Rela;
  31 import jdk.tools.jaotc.binformat.elf.ElfByteBuffer;
  32 
  33 final class ElfRelocTable {
  34     private final ArrayList<ArrayList<ElfRelocEntry>> relocEntries;
  35 
  36     ElfRelocTable(int numsects) {
  37         relocEntries = new ArrayList<>(numsects);
  38         for (int i = 0; i < numsects; i++) {
  39             relocEntries.add(new ArrayList<ElfRelocEntry>());
  40         }
  41     }
  42 
  43     void createRelocationEntry(int sectindex, int offset, int symno, int type, int addend) {
  44         ElfRelocEntry entry = new ElfRelocEntry(offset, symno, type, addend);
  45         relocEntries.get(sectindex).add(entry);
  46     }
  47 
  48     int getNumRelocs(int section_index) {
  49         return relocEntries.get(section_index).size();
  50     }
  51 
  52     // Return the relocation entries for a single section
  53     // or null if no entries added to section
  54     byte[] getRelocData(int section_index) {
  55         ArrayList<ElfRelocEntry> entryList = relocEntries.get(section_index);
  56 
  57         if (entryList.size() == 0) {
  58             return null;
  59         }
  60         ByteBuffer relocData = ElfByteBuffer.allocate(entryList.size() * Elf64_Rela.totalsize);
  61 
  62         // Copy each entry to a single ByteBuffer
  63         for (int i = 0; i < entryList.size(); i++) {
  64             ElfRelocEntry entry = entryList.get(i);
  65             relocData.put(entry.getArray());
  66         }
  67 
  68         return (relocData.array());
  69     }
  70 }