36 37 public class ElfSection { 38 String name; 39 ByteBuffer section; 40 byte [] data; 41 boolean hasrelocations; 42 int sectionIndex; 43 44 /** 45 * String holding section name strings 46 */ 47 private static StringBuilder sectNameTab = new StringBuilder(); 48 49 /** 50 * Keeps track of bytes in section string table since strTabContent.length() 51 * is number of chars, not bytes. 52 */ 53 private static int shStrTabNrOfBytes = 0; 54 55 public ElfSection(String sectName, byte [] sectData, int sectFlags, 56 int sectType, boolean hasRelocations, int sectIndex) { 57 58 long align; 59 60 section = ElfByteBuffer.allocate(Elf64_Shdr.totalsize); 61 62 // Return all 0's for NULL section 63 if (sectIndex == 0) { 64 sectNameTab.append('\0'); 65 shStrTabNrOfBytes += 1; 66 data = null; 67 hasrelocations = false; 68 sectionIndex = 0; 69 return; 70 } 71 72 section.putInt(Elf64_Shdr.sh_name.off, shStrTabNrOfBytes); 73 sectNameTab.append(sectName).append('\0'); 74 shStrTabNrOfBytes += (sectName.getBytes().length + 1); 75 name = sectName; 76 77 section.putInt(Elf64_Shdr.sh_type.off, sectType); 78 section.putLong(Elf64_Shdr.sh_flags.off, sectFlags); 79 section.putLong(Elf64_Shdr.sh_addr.off, 0); 80 section.putLong(Elf64_Shdr.sh_offset.off, 0); 81 82 if (sectName.equals(".shstrtab")) { 83 section.putLong(Elf64_Shdr.sh_size.off, shStrTabNrOfBytes); 84 data = sectNameTab.toString().getBytes(); 85 } 86 else { 87 data = sectData; 88 section.putLong(Elf64_Shdr.sh_size.off, sectData.length); 89 } 90 91 section.putLong(Elf64_Shdr.sh_entsize.off, 0); 92 93 // Determine the alignment and entrysize 94 // based on type of section 95 switch (sectType) { 96 case Elf64_Shdr.SHT_PROGBITS: 97 if ((sectFlags & Elf64_Shdr.SHF_EXECINSTR) != 0) 98 align = 16; 99 else 100 align = 4; 101 break; 102 case Elf64_Shdr.SHT_SYMTAB: 103 align = 8; 104 section.putLong(Elf64_Shdr.sh_entsize.off, Elf64_Sym.totalsize); 105 break; 106 case Elf64_Shdr.SHT_STRTAB: 107 align = 1; 108 break; 109 case Elf64_Shdr.SHT_RELA: 110 align = 8; 111 section.putLong(Elf64_Shdr.sh_entsize.off, Elf64_Rela.totalsize); 112 break; 113 case Elf64_Shdr.SHT_REL: 114 align = 8; 115 section.putLong(Elf64_Shdr.sh_entsize.off, Elf64_Rel.totalsize); 116 break; 117 case Elf64_Shdr.SHT_NOBITS: 118 align = 4; 119 break; 120 default: 121 align = 8; 122 break; 123 } 124 section.putLong(Elf64_Shdr.sh_addralign.off, align); 125 126 hasrelocations = hasRelocations; 127 sectionIndex = sectIndex; 128 } 129 130 public String getName() { 131 return name; 132 } 133 134 public long getSize() { 135 return section.getLong(Elf64_Shdr.sh_size.off); 136 } 137 138 public int getDataAlign() { 139 return ((int)section.getLong(Elf64_Shdr.sh_addralign.off)); 140 } 141 | 36 37 public class ElfSection { 38 String name; 39 ByteBuffer section; 40 byte [] data; 41 boolean hasrelocations; 42 int sectionIndex; 43 44 /** 45 * String holding section name strings 46 */ 47 private static StringBuilder sectNameTab = new StringBuilder(); 48 49 /** 50 * Keeps track of bytes in section string table since strTabContent.length() 51 * is number of chars, not bytes. 52 */ 53 private static int shStrTabNrOfBytes = 0; 54 55 public ElfSection(String sectName, byte [] sectData, int sectFlags, 56 int sectType, boolean hasRelocations, int align, 57 int sectIndex) { 58 59 section = ElfByteBuffer.allocate(Elf64_Shdr.totalsize); 60 61 // Return all 0's for NULL section 62 if (sectIndex == 0) { 63 sectNameTab.append('\0'); 64 shStrTabNrOfBytes += 1; 65 data = null; 66 hasrelocations = false; 67 sectionIndex = 0; 68 return; 69 } 70 71 section.putInt(Elf64_Shdr.sh_name.off, shStrTabNrOfBytes); 72 sectNameTab.append(sectName).append('\0'); 73 shStrTabNrOfBytes += (sectName.getBytes().length + 1); 74 name = sectName; 75 76 section.putInt(Elf64_Shdr.sh_type.off, sectType); 77 section.putLong(Elf64_Shdr.sh_flags.off, sectFlags); 78 section.putLong(Elf64_Shdr.sh_addr.off, 0); 79 section.putLong(Elf64_Shdr.sh_offset.off, 0); 80 81 if (sectName.equals(".shstrtab")) { 82 section.putLong(Elf64_Shdr.sh_size.off, shStrTabNrOfBytes); 83 data = sectNameTab.toString().getBytes(); 84 } 85 else { 86 data = sectData; 87 section.putLong(Elf64_Shdr.sh_size.off, sectData.length); 88 } 89 90 section.putLong(Elf64_Shdr.sh_entsize.off, 0); 91 92 // Determine the entrysize 93 // based on type of section 94 switch (sectType) { 95 case Elf64_Shdr.SHT_SYMTAB: 96 section.putLong(Elf64_Shdr.sh_entsize.off, Elf64_Sym.totalsize); 97 break; 98 case Elf64_Shdr.SHT_RELA: 99 section.putLong(Elf64_Shdr.sh_entsize.off, Elf64_Rela.totalsize); 100 break; 101 case Elf64_Shdr.SHT_REL: 102 section.putLong(Elf64_Shdr.sh_entsize.off, Elf64_Rel.totalsize); 103 break; 104 default: 105 break; 106 } 107 section.putLong(Elf64_Shdr.sh_addralign.off, align); 108 109 hasrelocations = hasRelocations; 110 sectionIndex = sectIndex; 111 } 112 113 public String getName() { 114 return name; 115 } 116 117 public long getSize() { 118 return section.getLong(Elf64_Shdr.sh_size.off); 119 } 120 121 public int getDataAlign() { 122 return ((int)section.getLong(Elf64_Shdr.sh_addralign.off)); 123 } 124 |