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