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.elf;
  25 
  26 import java.nio.ByteBuffer;
  27 import java.nio.ByteOrder;
  28 
  29 import jdk.tools.jaotc.binformat.elf.Elf;
  30 import jdk.tools.jaotc.binformat.elf.Elf.Elf64_Ehdr;
  31 import jdk.tools.jaotc.binformat.elf.Elf.Elf64_Shdr;
  32 import jdk.tools.jaotc.binformat.elf.ElfTargetInfo;
  33 import jdk.tools.jaotc.binformat.elf.ElfByteBuffer;
  34 
  35 public class ElfHeader {
  36     ByteBuffer header;
  37 
  38     public ElfHeader() {
  39         header = ElfByteBuffer.allocate(Elf64_Ehdr.totalsize);
  40 
  41         header.put(Elf64_Ehdr.e_ident.off+Elf64_Ehdr.EI_MAG0, Elf64_Ehdr.ELFMAG0);
  42         header.put(Elf64_Ehdr.e_ident.off+Elf64_Ehdr.EI_MAG1, Elf64_Ehdr.ELFMAG1);
  43         header.put(Elf64_Ehdr.e_ident.off+Elf64_Ehdr.EI_MAG2, Elf64_Ehdr.ELFMAG2);
  44         header.put(Elf64_Ehdr.e_ident.off+Elf64_Ehdr.EI_MAG3, Elf64_Ehdr.ELFMAG3);
  45         header.put(Elf64_Ehdr.e_ident.off+Elf64_Ehdr.EI_CLASS, Elf64_Ehdr.ELFCLASS64);
  46         header.put(Elf64_Ehdr.e_ident.off+Elf64_Ehdr.EI_DATA, Elf64_Ehdr.ELFDATA2LSB);
  47         header.put(Elf64_Ehdr.e_ident.off+Elf64_Ehdr.EI_VERSION, Elf64_Ehdr.EV_CURRENT);
  48         header.put(Elf64_Ehdr.e_ident.off+Elf64_Ehdr.EI_OSABI, Elf64_Ehdr.ELFOSABI_NONE);
  49 
  50         header.putChar(Elf64_Ehdr.e_type.off, Elf64_Ehdr.ET_REL);
  51         header.putChar(Elf64_Ehdr.e_machine.off, ElfTargetInfo.getElfArch());
  52         header.putInt(Elf64_Ehdr.e_version.off, Elf64_Ehdr.EV_CURRENT);
  53         header.putChar(Elf64_Ehdr.e_ehsize.off, (char)Elf64_Ehdr.totalsize);
  54         header.putChar(Elf64_Ehdr.e_shentsize.off, (char)Elf64_Shdr.totalsize);
  55 
  56     }
  57 
  58     // Update header with file offset of first section
  59     public void setSectionOff(int offset) {
  60         header.putLong(Elf64_Ehdr.e_shoff.off, offset);
  61     }
  62 
  63     // Update header with the number of total sections
  64     public void setSectionNum(int count) {
  65         header.putChar(Elf64_Ehdr.e_shnum.off, (char)count);
  66     }
  67 
  68     // Update header with the section index containing the
  69     // string table for section names
  70     public void setSectionStrNdx(int index) {
  71         header.putChar(Elf64_Ehdr.e_shstrndx.off, (char)index);
  72     }
  73 
  74     public byte[] getArray() {
  75         return header.array();
  76     }
  77 }
  78