1 /*
   2  * Copyright (c) 2016, 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.jnilibelf.test;
  25 
  26 import java.nio.ByteBuffer;
  27 import java.nio.IntBuffer;
  28 
  29 import jdk.tools.jaotc.jnilibelf.JNIELFContainer;
  30 import jdk.tools.jaotc.jnilibelf.JNILibELFAPI.ELF;
  31 import jdk.tools.jaotc.jnilibelf.JNILibELFAPI.LibELF.Elf_Cmd;
  32 import jdk.tools.jaotc.jnilibelf.JNILibELFAPI.LibELF.Elf_Type;
  33 
  34 public class JNILibELFTest {
  35 
  36     public static void main(String[] args) {
  37         // if (args.length != 2) {
  38         // System.out.println("Please provide file-name as argument");
  39         // return;
  40         // }
  41         createSharedLib();
  42     }
  43 
  44     private static boolean createSharedLib() {
  45 
  46         int numProgHdrs = 1;
  47         JNIELFContainer elfContainer = new JNIELFContainer("ELF");
  48 
  49         // Allocate ELF Header
  50         elfContainer.createELFHeader(ELF.ET_DYN);
  51 
  52         // Allocate 'numProgHdrs' program headers
  53 
  54         if (!elfContainer.createProgramHeader(numProgHdrs)) {
  55             System.out.println("Failed to create Program Headers");
  56             return false;
  57         }
  58 
  59         // Hash table content
  60         int[] bhashWords = {0x01234567, 0x89abcdef, 0xdeadc0de};
  61         // int[] data = { 100, 200, 300, 400 };
  62 
  63         ByteBuffer byteBuffer = ByteBuffer.allocate(bhashWords.length * 4);
  64         IntBuffer intBuffer = byteBuffer.asIntBuffer();
  65         intBuffer.put(bhashWords);
  66 
  67         // byte[] int_hash_array = byteBuffer.array();
  68 
  69         // Hash Table content
  70         // ByteBuffer hash_words = ByteBuffer.allocate(14).putInt(0x01234567);
  71         // hash_words.putInt(0x89abcdef);
  72         // hash_words.putInt(0xdeadc0de);
  73 
  74         // Create a hash section
  75         // Setting sh_link as 0 since this is just a demo - the value should actually be the section
  76         // header index
  77         // of the symbol table to which the hash table applies.
  78         int index = elfContainer.createSection(".hash", byteBuffer.array(), Elf_Type.ELF_T_WORD, 4, ELF.SHT_HASH, ELF.SHF_ALLOC, 0, 0);
  79         if (index == 0) {
  80             System.out.println("Failed to create hash section");
  81             return false;
  82         }
  83 
  84         elfContainer.createSection(".strtab", elfContainer.getStrTabContent().getBytes(), Elf_Type.ELF_T_BYTE, 1, ELF.SHT_STRTAB, (ELF.SHF_STRINGS | ELF.SHF_ALLOC), ELF.SHN_UNDEF, 0);
  85         // Now, finally, after creating all sections, create shstrtab section
  86         elfContainer.createSection(".shstrtab", elfContainer.getShStrTabContent().getBytes(), Elf_Type.ELF_T_BYTE, 1, ELF.SHT_STRTAB, 0, ELF.SHN_UNDEF, 0);
  87         // Run elf_update
  88         elfContainer.elfUpdate(Elf_Cmd.ELF_C_NULL);
  89 
  90         // Set program header type to self
  91         elfContainer.setProgHdrTypeToSelf();
  92         // Setting pheader to self type also sets it to be dirty. So run elfUpdate again
  93         // to write it out.
  94         elfContainer.elfUpdate(Elf_Cmd.ELF_C_WRITE);
  95         // Finish ELF processing
  96         elfContainer.elfEnd();
  97         return true;
  98     }
  99 }