1 /* 2 * Copyright (c) 2024-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package com.ohos.hapsigntool.codesigning.elf; 17 18 import com.ohos.hapsigntool.codesigning.exception.CodeSignErrMsg; 19 import com.ohos.hapsigntool.codesigning.exception.ElfFormatException; 20 import com.ohos.hapsigntool.zip.UnsignedDecimalUtil; 21 22 import java.io.IOException; 23 import java.io.InputStream; 24 import java.nio.ByteBuffer; 25 import java.nio.ByteOrder; 26 27 /** 28 * ELF program header info 29 * 30 * @since 2024/07/01 31 */ 32 public class ElfProgramHeader { 33 /** 34 * Segment type 35 */ 36 private int pType; 37 38 /** 39 * Segment flags 40 */ 41 private int pFlags; 42 43 /** 44 * Segment file offset 45 */ 46 private long pOffset; 47 48 /** 49 * Segment virtual address 50 */ 51 private long pVaddr; 52 53 /** 54 * Segment physical address 55 */ 56 private long pPaddr; 57 58 /** 59 * Segment size in file 60 */ 61 private long pFilesz; 62 63 /** 64 * Segment size in memory 65 */ 66 private long pMemsz; 67 68 /** 69 * Segment alignment 70 */ 71 private long pAlign; 72 73 /** 74 * Constructor for ElfPHeader 75 * 76 * @param is InputStream 77 * @param eiClass eiClass 78 * @param eiData eiData 79 * @throws IOException io error 80 * @throws ElfFormatException elf file format error 81 */ ElfProgramHeader(InputStream is, byte eiClass, byte eiData)82 public ElfProgramHeader(InputStream is, byte eiClass, byte eiData) throws IOException, ElfFormatException { 83 ByteOrder bo = ByteOrder.LITTLE_ENDIAN; 84 if (eiData == ElfDefine.ELF_DATA_2_LSB) { 85 bo = ByteOrder.LITTLE_ENDIAN; 86 } else if (eiData == ElfDefine.ELF_DATA_2_MSB) { 87 bo = ByteOrder.BIG_ENDIAN; 88 } else { 89 throw new ElfFormatException(CodeSignErrMsg.ELF_FILE_HEADER_ERROR.toString("ei_data")); 90 } 91 if (eiClass == ElfDefine.ELF_32_CLASS) { 92 byte[] bytes = new byte[ElfDefine.ELF_PHEADER_32_LEN]; 93 int read = is.read(bytes); 94 if (read != ElfDefine.ELF_PHEADER_32_LEN) { 95 throw new ElfFormatException(CodeSignErrMsg.ELF_FILE_HEADER_ERROR.toString("program header")); 96 } 97 ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); 98 byteBuffer.order(bo); 99 pType = byteBuffer.getInt(); 100 pOffset = UnsignedDecimalUtil.getUnsignedInt(byteBuffer); 101 pVaddr = UnsignedDecimalUtil.getUnsignedInt(byteBuffer); 102 pPaddr = UnsignedDecimalUtil.getUnsignedInt(byteBuffer); 103 pFilesz = byteBuffer.getInt(); 104 pMemsz = byteBuffer.getInt(); 105 pFlags = byteBuffer.getInt(); 106 pAlign = byteBuffer.getInt(); 107 } else { 108 byte[] bytes = new byte[ElfDefine.ELF_PHEADER_64_LEN]; 109 int read = is.read(bytes); 110 if (read != ElfDefine.ELF_PHEADER_64_LEN) { 111 throw new ElfFormatException(CodeSignErrMsg.ELF_FILE_HEADER_ERROR.toString("program header")); 112 } 113 ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); 114 byteBuffer.order(bo); 115 pType = byteBuffer.getInt(); 116 pFlags = byteBuffer.getInt(); 117 pOffset = byteBuffer.getLong(); 118 pVaddr = byteBuffer.getLong(); 119 pPaddr = byteBuffer.getLong(); 120 pFilesz = byteBuffer.getLong(); 121 pMemsz = byteBuffer.getLong(); 122 pAlign = byteBuffer.getLong(); 123 } 124 } 125 getPFlags()126 public int getPFlags() { 127 return pFlags; 128 } 129 getPOffset()130 public long getPOffset() { 131 return pOffset; 132 } 133 getPFilesz()134 public long getPFilesz() { 135 return pFilesz; 136 } 137 } 138