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