VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/UefiPayloadPkg/Tools/ElfFv.py@ 108793

Last change on this file since 108793 was 101291, checked in by vboxsync, 19 months ago

EFI/FirmwareNew: Make edk2-stable202308 build on all supported platforms (using gcc at least, msvc not tested yet), bugref:4643

  • Property svn:eol-style set to native
File size: 44.5 KB
Line 
1## @file
2# OBJCOPY parser, it's used to replace FV
3#
4# Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
5# SPDX-License-Identifier: BSD-2-Clause-Patent
6##
7
8import argparse
9from ctypes import *
10import struct
11
12class ElfSectionHeader64:
13 def __init__(self, sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link, sh_info, sh_addralign, sh_entsize):
14 self.sh_name = sh_name
15 self.sh_type = sh_type
16 self.sh_flags = sh_flags
17 self.sh_addr = sh_addr
18 self.sh_offset = sh_offset
19 self.sh_size = sh_size
20 self.sh_link = sh_link
21 self.sh_info = sh_info
22 self.sh_addralign = sh_addralign
23 self.sh_entsize = sh_entsize
24
25 def pack(self):
26 return struct.pack('<IIQQQQIIQQ', self.sh_name, self.sh_type, self.sh_flags, self.sh_addr, self.sh_offset, self.sh_size, self.sh_link, self.sh_info, self.sh_addralign, self.sh_entsize)
27
28 @classmethod
29 def unpack(cls, data):
30 unpacked_data = struct.unpack('<IIQQQQIIQQ', data)
31 return cls(*unpacked_data)
32
33class ElfHeader64:
34 def __init__(self, data):
35 # Parse the ELF identification bytes
36 self.e_ident = struct.unpack('16s', data[:16])[0]
37 self.e_type = struct.unpack('H', data[16:18])[0]
38 self.e_machine = struct.unpack('H', data[18:20])[0]
39 self.e_version = struct.unpack('I', data[20:24])[0]
40 self.e_entry = struct.unpack('Q', data[24:32])[0]
41 self.e_phoff = struct.unpack('Q', data[32:40])[0]
42 self.e_shoff = struct.unpack('Q', data[40:48])[0]
43 self.e_flags = struct.unpack('I', data[48:52])[0]
44 self.e_ehsize = struct.unpack('H', data[52:54])[0]
45 self.e_phentsize = struct.unpack('H', data[54:56])[0]
46 self.e_phnum = struct.unpack('H', data[56:58])[0]
47 self.e_shentsize = struct.unpack('H', data[58:60])[0]
48 self.e_shnum = struct.unpack('H', data[60:62])[0]
49 self.e_shstrndx = struct.unpack('H', data[62:64])[0]
50
51 def pack(self):
52 # Pack the ELF header data into a binary string
53 data = b''
54 data += struct.pack('16s', self.e_ident)
55 data += struct.pack('H', self.e_type)
56 data += struct.pack('H', self.e_machine)
57 data += struct.pack('I', self.e_version)
58 data += struct.pack('Q', self.e_entry)
59 data += struct.pack('Q', self.e_phoff)
60 data += struct.pack('Q', self.e_shoff)
61 data += struct.pack('I', self.e_flags)
62 data += struct.pack('H', self.e_ehsize)
63 data += struct.pack('H', self.e_phentsize)
64 data += struct.pack('H', self.e_phnum)
65 data += struct.pack('H', self.e_shentsize)
66 data += struct.pack('H', self.e_shnum)
67 data += struct.pack('H', self.e_shstrndx)
68 return data
69
70class Elf64_Phdr:
71 def __init__(self, data):
72 self.p_type = struct.unpack("<L", data[0:4])[0]
73 self.p_flags = struct.unpack("<L", data[4:8])[0]
74 self.p_offset = struct.unpack("<Q", data[8:16])[0]
75 self.p_vaddr = struct.unpack("<Q", data[16:24])[0]
76 self.p_paddr = struct.unpack("<Q", data[24:32])[0]
77 self.p_filesz = struct.unpack("<Q", data[32:40])[0]
78 self.p_memsz = struct.unpack("<Q", data[40:48])[0]
79 self.p_align = struct.unpack("<Q", data[48:56])[0]
80
81 def pack(self):
82 # Pack the Program header table into a binary string
83 data = b''
84 data += struct.pack('<L', self.p_type)
85 data += struct.pack('<L', self.p_flags)
86 data += struct.pack('<Q', self.p_offset)
87 data += struct.pack('<Q', self.p_vaddr)
88 data += struct.pack('<Q', self.p_paddr)
89 data += struct.pack('<Q', self.p_filesz)
90 data += struct.pack('<Q', self.p_memsz)
91 data += struct.pack('<Q', self.p_align)
92 return data
93
94class ElfSectionHeader32:
95 def __init__(self, sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link, sh_info, sh_addralign, sh_entsize):
96 self.sh_name = sh_name
97 self.sh_type = sh_type
98 self.sh_flags = sh_flags
99 self.sh_addr = sh_addr
100 self.sh_offset = sh_offset
101 self.sh_size = sh_size
102 self.sh_link = sh_link
103 self.sh_info = sh_info
104 self.sh_addralign = sh_addralign
105 self.sh_entsize = sh_entsize
106
107 def pack(self):
108 return struct.pack('<IIIIIIIIII', self.sh_name, self.sh_type, self.sh_flags, self.sh_addr, self.sh_offset, self.sh_size, self.sh_link, self.sh_info, self.sh_addralign, self.sh_entsize)
109
110 @classmethod
111 def unpack(cls, data):
112 unpacked_data = struct.unpack('<IIIIIIIIII', data)
113 return cls(*unpacked_data)
114
115class ElfHeader32:
116 def __init__(self, data):
117 # Parse the ELF identification bytes
118 self.e_ident = struct.unpack('16s', data[:16])[0]
119 self.e_type = struct.unpack('H', data[16:18])[0]
120 self.e_machine = struct.unpack('H', data[18:20])[0]
121 self.e_version = struct.unpack('I', data[20:24])[0]
122 self.e_entry = struct.unpack('I', data[24:28])[0]
123 self.e_phoff = struct.unpack('I', data[28:32])[0]
124 self.e_shoff = struct.unpack('I', data[32:36])[0]
125 self.e_flags = struct.unpack('I', data[36:40])[0]
126 self.e_ehsize = struct.unpack('H', data[40:42])[0]
127 self.e_phentsize = struct.unpack('H', data[42:44])[0]
128 self.e_phnum = struct.unpack('H', data[44:46])[0]
129 self.e_shentsize = struct.unpack('H', data[46:48])[0]
130 self.e_shnum = struct.unpack('H', data[48:50])[0]
131 self.e_shstrndx = struct.unpack('H', data[50:52])[0]
132
133 def pack(self):
134 # Pack the ELF header data into a binary string
135 data = b''
136 data += struct.pack('16s', self.e_ident)
137 data += struct.pack('H', self.e_type)
138 data += struct.pack('H', self.e_machine)
139 data += struct.pack('I', self.e_version)
140 data += struct.pack('I', self.e_entry)
141 data += struct.pack('I', self.e_phoff)
142 data += struct.pack('I', self.e_shoff)
143 data += struct.pack('I', self.e_flags)
144 data += struct.pack('H', self.e_ehsize)
145 data += struct.pack('H', self.e_phentsize)
146 data += struct.pack('H', self.e_phnum)
147 data += struct.pack('H', self.e_shentsize)
148 data += struct.pack('H', self.e_shnum)
149 data += struct.pack('H', self.e_shstrndx)
150 return data
151
152class Elf32_Phdr:
153 def __init__(self, data):
154 self.p_type = struct.unpack("<L", data[0:4])[0]
155 self.p_offset = struct.unpack("<L", data[4:8])[0]
156 self.p_vaddr = struct.unpack("<L", data[8:12])[0]
157 self.p_paddr = struct.unpack("<L", data[12:16])[0]
158 self.p_filesz = struct.unpack("<L", data[16:20])[0]
159 self.p_memsz = struct.unpack("<L", data[20:24])[0]
160 self.p_flags = struct.unpack("<L", data[24:28])[0]
161 self.p_align = struct.unpack("<L", data[28:32])[0]
162
163 def pack(self):
164 # Pack the Program header table into a binary string
165 data = b''
166 data += struct.pack('<L', self.p_type)
167 data += struct.pack('<L', self.p_offset)
168 data += struct.pack('<L', self.p_vaddr)
169 data += struct.pack('<L', self.p_paddr)
170 data += struct.pack('<L', self.p_filesz)
171 data += struct.pack('<L', self.p_memsz)
172 data += struct.pack('<L', self.p_flags)
173 data += struct.pack('<L', self.p_align)
174 return data
175
176def SectionAlignment(NewUPLEntry, AlignmentIndex):
177 # Section entry Alignment
178 # Alignment is transfer to integer if AlignmentIndex is string.
179 if isinstance(AlignmentIndex, str):
180 int_num = int(AlignmentIndex, 16)
181 int_num = 10 * (int_num//16) + int_num % 16
182 else:
183 int_num = AlignmentIndex
184 if (int_num != 0 or int_num != 1):
185 if ((len(NewUPLEntry) % int_num) != 0):
186 AlignNumber = int_num - (len(NewUPLEntry) % int_num)
187 if (AlignNumber != 0):
188 for x in range(AlignNumber):
189 NewUPLEntry = NewUPLEntry + bytearray(b'\0')
190 return NewUPLEntry
191
192def SectionEntryFill(SectionEntry, Alignment, Value, Offset):
193 # Alignment
194 n = 0
195 if (len (Value) < Alignment):
196 Value = Value.zfill(Alignment)
197 for x in range(0, (Alignment//2)):
198 Index = '0x' + Value[n] + Value[n + 1]
199 SectionEntry[Offset - x] = int(Index,16)
200 n += 2
201 return SectionEntry
202
203def ElfHeaderParser(UPLEntry):
204 # Read EI_CLASS, it stores information that elf with 32-bit or 64-bit architectures.
205 EI_CLASS = UPLEntry[4]
206 # If Elf is 64-bit objects.
207 if (EI_CLASS == 2):
208 # Elf header is stored at 0x0-0x40 in 64-bits objects
209 ElfHeaderData = UPLEntry[:64]
210 # If Elf is 32-bit objects.
211 else:
212 # Elf header is stored at 0x0-0x34 in 32-bits objects
213 ElfHeaderData = UPLEntry[:53]
214 # If Elf is 64-bit objects.
215 if (EI_CLASS == 2):
216 elf_header = ElfHeader64(ElfHeaderData)
217 ElfHeaderOffset = elf_header.e_shoff
218 SectionHeaderEntryNumber = elf_header.e_shnum
219 StringIndexNumber = elf_header.e_shstrndx
220 SectionHeaderEntrySize = elf_header.e_shentsize
221 StringIndexEntryOffset = ElfHeaderOffset + (StringIndexNumber * SectionHeaderEntrySize)
222 unpacked_header = ElfSectionHeader64.unpack(UPLEntry[StringIndexEntryOffset: (StringIndexEntryOffset + SectionHeaderEntrySize)])
223 StringIndexSize = unpacked_header.sh_size
224 StringIndexOffset = unpacked_header.sh_offset
225 # If elf is 32-bit objects.
226 else:
227 elf_header = ElfHeader32(ElfHeaderData)
228 ElfHeaderOffset = elf_header.e_shoff
229 SectionHeaderEntryNumber = elf_header.e_shnum
230 StringIndexNumber = elf_header.e_shstrndx
231 SectionHeaderEntrySize = elf_header.e_shentsize
232 StringIndexEntryOffset = ElfHeaderOffset + (StringIndexNumber * SectionHeaderEntrySize)
233 unpacked_header = ElfSectionHeader32.unpack(UPLEntry[StringIndexEntryOffset: (StringIndexEntryOffset + SectionHeaderEntrySize)])
234 StringIndexSize = unpacked_header.sh_size
235 StringIndexOffset = unpacked_header.sh_offset
236 return ElfHeaderOffset, SectionHeaderEntryNumber, StringIndexNumber, StringIndexEntryOffset, StringIndexSize, SectionHeaderEntrySize, StringIndexOffset, EI_CLASS
237
238def FindSection(UPLEntry, SectionName):
239 ElfHeaderOffset, SectionHeaderEntryNumber, StringIndexNumber, _, StringIndexSize, SectionHeaderEntrySize, StringIndexOffset, EI_CLASS = ElfHeaderParser(UPLEntry)
240 # StringIndex is String Index section
241 StringIndex = UPLEntry[StringIndexOffset:StringIndexOffset+StringIndexSize]
242 # Section header isn't exist if SectionNameOffset = -1.
243 StringIndex = StringIndex.decode('utf-8', errors='ignore')
244 SectionNameOffset = StringIndex.find(SectionName)
245 return SectionNameOffset, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, StringIndexOffset, StringIndexNumber, EI_CLASS
246
247def AddNewSectionEntry64(LastUPLEntrylen, StringIndexValue, SectionSize, Alignment):
248 # If elf is 64-bit objects.
249 NewSectionEntry = ElfSectionHeader64 (StringIndexValue, 1, 0, 0, LastUPLEntrylen, SectionSize, 0, 0, Alignment, 0)
250 sh_bytes = NewSectionEntry.pack()
251 return sh_bytes
252
253def AddNewSectionEntry32(LastUPLEntrylen, StringIndexValue, SectionSize, Alignment):
254 # If elf is 32-bit objects.
255 NewSectionEntry = ElfSectionHeader32 (StringIndexValue, 1, 0, 0, LastUPLEntrylen, SectionSize, 0, 0, Alignment, 0)
256 sh_bytes = NewSectionEntry.pack()
257 return sh_bytes
258
259def AddSectionHeader64(SHentry, NewUPLEntrylen, SectionHeaderEntrySize, Index, RemoveNameOffset, SectionName, StringIndexNumber):
260 SHentry = bytearray(SHentry)
261 unpacked_header = ElfSectionHeader64.unpack(SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
262 # Section header of section 0 shows 0. It don't modify any offset.
263 if (Index != 0):
264 # read section offset.
265 unpacked_header.sh_offset = NewUPLEntrylen
266 # Modify offset of name in section entry
267 # if RemoveNameOffset != 0 that is remove function.
268 if (RemoveNameOffset != 0):
269 if (unpacked_header.sh_name > RemoveNameOffset):
270 unpacked_header.sh_name -= len (SectionName)
271 # Modify size of name string section entry in section entry.
272 if (Index == StringIndexNumber):
273 unpacked_header.sh_size -= len (SectionName)
274 # added section
275 else :
276 if (Index == StringIndexNumber):
277 unpacked_header.sh_size += len (SectionName)
278 NewSHentry = ElfSectionHeader64 (
279 unpacked_header.sh_name,
280 unpacked_header.sh_type,
281 unpacked_header.sh_flags,
282 unpacked_header.sh_addr,
283 unpacked_header.sh_offset,
284 unpacked_header.sh_size,
285 unpacked_header.sh_link,
286 unpacked_header.sh_info,
287 unpacked_header.sh_addralign,
288 unpacked_header.sh_entsize).pack()
289 return NewSHentry
290
291def AddSectionHeader32(SHentry, NewUPLEntrylen, SectionHeaderEntrySize, Index, RemoveNameOffset, SectionName, StringIndexNumber):
292 SHentry = bytearray(SHentry)
293 unpacked_header = ElfSectionHeader32.unpack(SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
294 if (Index != 0):
295 NewSHentry = SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)]
296 unpacked_header.sh_offset = NewUPLEntrylen
297 # Modify offset of name in section entry
298 # if RemoveNameOffset != 0 that is remove function.
299 if (RemoveNameOffset != 0):
300 if (unpacked_header.sh_name > RemoveNameOffset):
301 unpacked_header.sh_name -= len (SectionName)
302 # Modify size of name string section entry in section entry.
303 if (Index == StringIndexNumber):
304 unpacked_header.sh_size -= len (SectionName)
305 # added section
306 else :
307 if (Index == StringIndexNumber):
308 unpacked_header.sh_size += len (SectionName)
309 NewSHentry = ElfSectionHeader32 (
310 unpacked_header.sh_name,
311 unpacked_header.sh_type,
312 unpacked_header.sh_flags,
313 unpacked_header.sh_addr,
314 unpacked_header.sh_offset,
315 unpacked_header.sh_size,
316 unpacked_header.sh_link,
317 unpacked_header.sh_info,
318 unpacked_header.sh_addralign,
319 unpacked_header.sh_entsize).pack()
320 return NewSHentry
321
322def ModifyPHSegmentOffset64(NewUPLEntry, ElfHeaderOffset, PHSegmentName):
323 # Modify offset and address of program header tables.
324 elf_header = ElfHeader64(NewUPLEntry[:64])
325 SHentry = NewUPLEntry[ElfHeaderOffset:]
326 # Elf program header tables start from 0x40 in 64-bits objects
327 PHentry = NewUPLEntry[64: 64 + (elf_header.e_phnum * elf_header.e_phentsize)]
328 PHdrs = []
329 SHdrs = []
330 for i in range(elf_header.e_shnum):
331 SHData = SHentry[(i * elf_header.e_shentsize): (i * elf_header.e_shentsize) + elf_header.e_shentsize]
332 unpacked_SectionHeader = ElfSectionHeader64.unpack(SHData)
333 SHdrs.append(unpacked_SectionHeader)
334 for i in range(elf_header.e_phnum):
335 PHData = PHentry[(i * elf_header.e_phentsize): (i * elf_header.e_phentsize) + elf_header.e_phentsize]
336 unpacked_ProgramHeader = Elf64_Phdr(PHData)
337 PHdrs.append(unpacked_ProgramHeader)
338 if (PHSegmentName == '.text'):
339 PHdrs[0].p_offset = SHdrs[1].sh_offset
340 PHdrs[0].p_paddr = SHdrs[1].sh_addr
341 PHdrs[4].p_offset = SHdrs[1].sh_offset
342 PHdrs[4].p_paddr = SHdrs[1].sh_addr
343 elif (PHSegmentName == '.dynamic'):
344 PHdrs[1].p_offset = SHdrs[2].sh_offset
345 PHdrs[1].p_paddr = SHdrs[2].sh_addr
346 PHdrs[3].p_offset = SHdrs[2].sh_offset
347 PHdrs[3].p_paddr = SHdrs[2].sh_addr
348 elif (PHSegmentName == '.data'):
349 PHdrs[2].p_offset = SHdrs[3].sh_offset
350 PHdrs[2].p_paddr = SHdrs[3].sh_addr
351 packed_PHData = b''
352 for phdr in PHdrs:
353 packed_PHData += phdr.pack()
354 NewUPLEntry = bytearray(NewUPLEntry)
355 NewUPLEntry[64: 64 + (elf_header.e_phnum * elf_header.e_phentsize)] = packed_PHData
356 return NewUPLEntry
357
358def ModifyPHSegmentOffset32(NewUPLEntry, ElfHeaderOffset, PHSegmentName):
359 # Modify offset and address of program header tables.
360 # Elf header is stored at 0x0-0x34 in 32-bits objects
361 elf_header = ElfHeader32(NewUPLEntry[:52])
362 SHentry = NewUPLEntry[ElfHeaderOffset:]
363 # Elf program header tables start from 0x34 in 32-bits objects
364 PHentry = NewUPLEntry[52: 52 + (elf_header.e_phnum * elf_header.e_phentsize)]
365 PHdrs = []
366 SHdrs = []
367 for i in range(elf_header.e_shnum):
368 SHData = SHentry[(i * elf_header.e_shentsize): (i * elf_header.e_shentsize) + elf_header.e_shentsize]
369 unpacked_SectionHeader = ElfSectionHeader32.unpack(SHData)
370 SHdrs.append(unpacked_SectionHeader)
371 for i in range(elf_header.e_phnum):
372 PHData = PHentry[(i * elf_header.e_phentsize): (i * elf_header.e_phentsize) + elf_header.e_phentsize]
373 unpacked_ProgramHeader = Elf32_Phdr(PHData)
374 PHdrs.append(unpacked_ProgramHeader)
375 if (PHSegmentName == '.text'):
376 PHdrs[0].p_offset = SHdrs[1].sh_offset
377 PHdrs[0].p_paddr = SHdrs[1].sh_addr
378 PHdrs[0].p_vaddr = SHdrs[1].sh_addr
379 PHdrs[2].p_offset = SHdrs[1].sh_offset
380 PHdrs[2].p_paddr = SHdrs[1].sh_addr
381 PHdrs[0].p_vaddr = SHdrs[1].sh_addr
382 elif (PHSegmentName == '.data'):
383 PHdrs[1].p_offset = SHdrs[2].sh_offset
384 PHdrs[1].p_paddr = SHdrs[2].sh_addr
385 PHdrs[1].p_vaddr = SHdrs[2].sh_addr
386 packed_PHData = b''
387 for phdr in PHdrs:
388 packed_PHData += phdr.pack()
389 NewUPLEntry = bytearray(NewUPLEntry)
390 NewUPLEntry[52: 52 + (elf_header.e_phnum * elf_header.e_phentsize)] = packed_PHData
391 return NewUPLEntry
392
393def RemoveSection64(UniversalPayloadEntry, RemoveSectionName):
394 # If elf is 64-bit objects.
395 # Get offsets as follows:
396 # 1. Section name which will remove in section name string.
397 # 2. Section which will remove.
398 # 3. Section header which will remove.
399 with open(UniversalPayloadEntry,'rb') as f:
400 UPLEntry = f.read()
401 RemoveSectionNameOffset, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, _ = FindSection(UPLEntry, RemoveSectionName)
402 if (RemoveSectionNameOffset == -1):
403 raise argparse.ArgumentTypeError ('Section: {} not found.'.format (RemoveSectionNameOffset))
404 # Read section header entry
405 SHentry = UPLEntry[ElfHeaderOffset:]
406 # find deleted fv section offset.
407 # Elf header is stored at 0x0-0x40 in 64-bits objects
408 elf_header = ElfHeader64(UPLEntry[:64])
409 Counter = 0
410 RemoveIndex = 0
411 RemoveNameOffset = 0
412 for Index in range(0, elf_header.e_shnum):
413 # Read Index of section header.
414 unpacked_SectionHeader = ElfSectionHeader64.unpack(SHentry[(Index * elf_header.e_shentsize):((Index * elf_header.e_shentsize) + elf_header.e_shentsize)])
415 # Find offset of section name which is removed.
416 if (unpacked_SectionHeader.sh_name == RemoveSectionNameOffset):
417 RemoveIndex = Counter
418 Counter += 1
419 else:
420 Counter += 1
421 # Elf header is recombined.
422 # Elf header and program header table in front of first section are reserved.
423 # Elf header size is 0x40 with 64-bit object.
424 ElfHeaderSize = 64
425 ElfHandPH = ElfHeaderSize + (elf_header.e_phnum * elf_header.e_phentsize)
426 NewUPLEntry = UPLEntry[:ElfHandPH]
427 # Keep Section header and program header table, RemoveSection64() only recombined section and section header.
428 NewUPLEntry = bytearray(NewUPLEntry)
429 # Sections is recombined.
430 # 1. name of deleted section is removed in name string section.
431 # 2. deleted section is removed in dll file.
432 # 3. re-align sections before and after deleted section.
433 NewUPLEntrylen = []
434 for Index in range(0, (SectionHeaderEntryNumber)):
435 unpacked_SectionHeader = ElfSectionHeader64.unpack(SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
436 NewUPLEntrylen.append(len(NewUPLEntry))
437 if (Index == 0):
438 # Address alignment, section will align with alignment of next section.
439 AlignmentIndex = 8
440 if (SectionHeaderEntryNumber > 2):
441 unpacked_NextSectionHeader = ElfSectionHeader64.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
442 NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
443 # Section in front of removed section
444 elif (Index + 1 == RemoveIndex):
445 NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
446 # Read section address alignment
447 # If section that will be removed in .dll is not first and last one .
448 # Address alignment, section will align with alignment of section after deleted section.
449 # Check next and the section after next are not end of section.
450 if ((Index + 2) < (SectionHeaderEntryNumber - 1)):
451 unpacked_Next2SectionHeader = ElfSectionHeader64.unpack(SHentry[((Index + 2) * SectionHeaderEntrySize):(((Index + 2) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
452 NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_Next2SectionHeader.sh_addralign)
453 else:
454 # It is align 8 bytes if next section or the section after next is last one.
455 AlignmentIndex = 8
456 NewUPLEntry = SectionAlignment(NewUPLEntry, AlignmentIndex)
457 # section is Deleted section
458 elif (Index == RemoveIndex):
459 # Don't add removed section to elf.
460 # Find offset of section name.
461 RemoveNameOffset = unpacked_SectionHeader.sh_name
462 # section is name string section.
463 elif (Index == StringIndexNumber):
464 # StringIndex is String Index section
465 StringIndex = UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
466 # Remove name of removed section in name string section.
467 # Section header isn't exist if RemoveSectionNameOffset equal to -1.
468 StringIndex = bytearray(StringIndex)
469 RemoveSectionName = bytearray(RemoveSectionName, encoding='utf-8')
470 RemoveSectionName = RemoveSectionName + bytes('\0', encoding='utf-8')
471 StringIndex = StringIndex.replace(RemoveSectionName,b'')
472 NewUPLEntry += StringIndex
473 # other sections.
474 else:
475 NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
476 # Address alignment, section will align with alignment of next section.
477 if (Index < (SectionHeaderEntryNumber - 1)):
478 NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
479 else:
480 # If section is last one.
481 AlignmentIndex = 8
482 NewUPLEntry = SectionAlignment(NewUPLEntry, AlignmentIndex)
483 SectionHeaderOffset = len(NewUPLEntry)
484 # Add section header
485 for Number in range(0, (SectionHeaderEntryNumber)):
486 if (Number != RemoveIndex):
487 NewSHentry = AddSectionHeader64(SHentry, NewUPLEntrylen[Number], SectionHeaderEntrySize, Number, RemoveNameOffset, RemoveSectionName, StringIndexNumber)
488 NewUPLEntry += NewSHentry
489 # Modify number of sections and offset of section header in Elf header.
490 elf_header.e_shoff = SectionHeaderOffset
491 elf_header.e_shnum -= 1
492 NewUPLEntry = elf_header.pack() + NewUPLEntry[64:]
493 # write to Elf.
494 with open(UniversalPayloadEntry,'wb') as f:
495 f.write(NewUPLEntry)
496
497def RemoveSection32(UniversalPayloadEntry, RemoveSectionName):
498 # If elf is 32-bit objects.
499 # Get offsets as follows:
500 # 1. Section name which will remove in section name string.
501 # 2. Section which will remove.
502 # 3. Section header which will remove.
503 with open(UniversalPayloadEntry,'rb') as f:
504 UPLEntry = f.read()
505 RemoveSectionNameOffset, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, EI_CLASS = FindSection(UPLEntry, RemoveSectionName)
506 if (RemoveSectionNameOffset == -1):
507 raise argparse.ArgumentTypeError ('Section: {} not found.'.format (RemoveSectionNameOffset))
508 # Read section header entry
509 SHentry = UPLEntry[ElfHeaderOffset:]
510 # find deleted fv section offset.
511 # Elf header is stored at 0x0-0x34 in 32-bits objects
512 elf_header = ElfHeader32(UPLEntry[:52])
513 Counter = 0
514 RemoveIndex = 0
515 RemoveNameOffset = 0
516 for Index in range(0, elf_header.e_shnum):
517 # Read Index of section header.
518 unpacked_SectionHeader = ElfSectionHeader32.unpack(SHentry[(Index * elf_header.e_shentsize):((Index * elf_header.e_shentsize) + elf_header.e_shentsize)])
519 # Find offset of section name which is removed.
520 if (unpacked_SectionHeader.sh_name == RemoveSectionNameOffset):
521 RemoveIndex = Counter
522 Counter += 1
523 else:
524 Counter += 1
525 # Elf header is recombined.
526 # Elf header and program header table in front of first section are reserved.
527 # Elf header size is 0x34 with 32-bit object.
528 ElfHeaderSize = 52
529 ElfHandPH = ElfHeaderSize + (elf_header.e_phnum * elf_header.e_phentsize)
530 NewUPLEntry = UPLEntry[:ElfHandPH]
531 # Keep Section header and program header table, RemoveSection32() only recombined section and section header.
532 NewUPLEntry = bytearray(NewUPLEntry)
533 # Sections is recombined.
534 # 1. name of deleted section is removed in name string section.
535 # 2. deleted section is removed in dll file.
536 # 3. re-align sections before and after deleted section.
537 NewUPLEntrylen = []
538 for Index in range(0, (SectionHeaderEntryNumber)):
539 unpacked_SectionHeader = ElfSectionHeader32.unpack(SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
540 NewUPLEntrylen.append(len(NewUPLEntry))
541 if (Index == 0):
542 # Address alignment, section will align with alignment of next section.
543 AlignmentIndex = 8
544 if (SectionHeaderEntryNumber > 2):
545 unpacked_NextSectionHeader = ElfSectionHeader32.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
546 NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
547 # Section in front of removed section
548 elif (Index + 1 == RemoveIndex):
549 NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
550 # Read section address alignment
551 # If section that will be removed in .dll is not first and last one .
552 # Address alignment, section will align with alignment of section after deleted section.
553 # Check next and the section after next are not end of section.
554 if ((Index + 2) < (SectionHeaderEntryNumber - 1)):
555 unpacked_Next2SectionHeader = ElfSectionHeader32.unpack(SHentry[((Index + 2) * SectionHeaderEntrySize):(((Index + 2) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
556 NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_Next2SectionHeader.sh_addralign)
557 else:
558 # It is align 8 bytes if next section or the section after next is last one.
559 AlignmentIndex = 8
560 NewUPLEntry = SectionAlignment(NewUPLEntry, AlignmentIndex)
561 # section is Deleted section
562 elif (Index == RemoveIndex):
563 # Don't add removed section to elf.
564 # Find offset of section name.
565 RemoveNameOffset = unpacked_SectionHeader.sh_name
566 # section is name string section.
567 elif (Index == StringIndexNumber):
568 # StringIndex is String Index section
569 StringIndex = UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
570 # Remove name of removed section in name string section.
571 # Section header isn't exist if RemoveSectionNameOffset equal to -1.
572 StringIndex = bytearray(StringIndex)
573 RemoveSectionName = bytearray(RemoveSectionName, encoding='utf-8')
574 RemoveSectionName = RemoveSectionName + bytes('\0', encoding='utf-8')
575 StringIndex = StringIndex.replace(RemoveSectionName,b'')
576 NewUPLEntry += StringIndex
577 # other sections.
578 else:
579 NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
580 # Address alignment, section will align with alignment of next section.
581 if (Index < (SectionHeaderEntryNumber - 1)):
582 NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
583 else:
584 # If section is last one.
585 AlignmentIndex = 8
586 NewUPLEntry = SectionAlignment(NewUPLEntry, AlignmentIndex)
587 SectionHeaderOffset = len(NewUPLEntry)
588 # Add section header
589 for Number in range(0, (SectionHeaderEntryNumber)):
590 if (Number != RemoveIndex):
591 NewSHentry = AddSectionHeader32(SHentry, NewUPLEntrylen[Number], SectionHeaderEntrySize, Number, RemoveNameOffset, RemoveSectionName, StringIndexNumber)
592 NewUPLEntry += NewSHentry
593 # Modify number of sections and offset of section header in Elf header.
594 elf_header.e_shoff = SectionHeaderOffset
595 elf_header.e_shnum -= 1
596 NewUPLEntry = elf_header.pack() + NewUPLEntry[52:]
597 # write to Elf.
598 with open(UniversalPayloadEntry,'wb') as f:
599 f.write(NewUPLEntry)
600
601def AddSection64(UniversalPayloadEntry, AddSectionName, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, StringIndexNumber, FileBinary, Alignment):
602 with open(UniversalPayloadEntry,'rb+') as f:
603 UPLEntry = f.read()
604 fFileBinary = open(FileBinary, 'rb')
605 Binary_File = fFileBinary.read()
606 ElfHeaderOffset, SectionHeaderEntryNumber, StringIndexNumber, _, _, SectionHeaderEntrySize, _, _ = ElfHeaderParser(UPLEntry)
607 # Read section header entry
608 SHentry = UPLEntry[ElfHeaderOffset:]
609 # Elf header is recombined.
610 # Elf header and program header table in front of first section are reserved.
611 # Elf header is stored at 0x0-0x40 in 64-bits objects
612 elf_header = ElfHeader64(UPLEntry[:64])
613 # Elf header size is 0x40 with 64-bit object.
614 ElfHeaderSize = 64
615 ElfHandPH = ElfHeaderSize + (elf_header.e_phnum * elf_header.e_phentsize)
616 NewUPLEntry = UPLEntry[:ElfHandPH]
617 # Keep Section header and program header table, AddSection64() only recombined section and section header.
618 NewUPLEntry = bytearray(NewUPLEntry)
619 # Sections is recombined.
620 # 1. name of added section is added in name string section.
621 # 2. added section is added in dll file.
622 # 3. re-align sections before and after added section.
623 NewUPLEntrylen = []
624 StringIndexValue = 0
625 for Index in range(0, SectionHeaderEntryNumber):
626 NewUPLEntrylen.append(len(NewUPLEntry))
627 unpacked_SectionHeader = ElfSectionHeader64.unpack(SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
628 # Sections is recombined.
629 if (Index == 0):
630 # Address alignment, section will align with alignment of next section.
631 AlignmentIndex = 8
632 if (SectionHeaderEntryNumber > 2):
633 unpacked_NextSectionHeader = ElfSectionHeader64.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
634 NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
635 # Section is last one.
636 elif (Index == (SectionHeaderEntryNumber - 1)):
637 # Add new section at the end.
638 NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
639 NewUPLEntry = SectionAlignment(NewUPLEntry, Alignment)
640 LastUPLEntrylen = len(NewUPLEntry)
641 NewUPLEntry += Binary_File
642 # Address alignment, section will align with alignment of next section.
643 AlignmentIndex = 8
644 NewUPLEntry = SectionAlignment(NewUPLEntry, AlignmentIndex)
645 # section is name string section.
646 elif (Index == StringIndexNumber):
647 # StringIndex is String Index section
648 StringIndex = UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
649 # Read name of added Section after StringIndex is transform into string.
650 StringIndex = bytearray(StringIndex)
651 StringIndexValue = len(StringIndex)
652 AddSectionName = bytearray(AddSectionName, encoding='utf-8') + bytes('\0', encoding='utf-8')
653 StringIndex += AddSectionName
654 NewUPLEntry += StringIndex
655 # section after name string section but not last one.
656 elif ((Index > StringIndexNumber) and (Index < (SectionHeaderEntryNumber - 1))):
657 NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
658 # Address alignment, section will align with alignment of next section.
659 unpacked_NextSectionHeader = ElfSectionHeader64.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
660 NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
661 # Section before name string section.
662 else:
663 NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
664 # Address alignment, section will align with alignment of next section.
665 if (Index < (SectionHeaderEntryNumber - 1)):
666 unpacked_NextSectionHeader = ElfSectionHeader64.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
667 NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
668 SectionHeaderOffset = len(NewUPLEntry)
669 RemoveNameOffset = 0
670 # Add section header
671 for Number in range(0, (SectionHeaderEntryNumber)):
672 NewSHentry = AddSectionHeader64(SHentry, NewUPLEntrylen[Number], SectionHeaderEntrySize, Number, RemoveNameOffset, AddSectionName, StringIndexNumber)
673 NewUPLEntry += NewSHentry
674 NewUPLEntry += bytearray(AddNewSectionEntry64(LastUPLEntrylen, StringIndexValue, len(Binary_File), Alignment))
675 # Modify number of sections and offset of section header in Elf header.
676 # Modify offset in in Elf header.
677 elf_header.e_shoff = SectionHeaderOffset
678 elf_header.e_shnum += 1
679 elf_header = elf_header.pack()
680 UPLEntryBin = elf_header + NewUPLEntry[64:]
681 # Modify offsets and address of program header table in elf.
682 PHSegmentName = '.text'
683 _, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, _ = FindSection(UPLEntryBin, PHSegmentName)
684 UPLEntryBin = ModifyPHSegmentOffset64(UPLEntryBin, ElfHeaderOffset, PHSegmentName)
685 # Modify offsets and address of program header table in elf.
686 PHSegmentName = '.dynamic'
687 _, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, _ = FindSection(UPLEntryBin, PHSegmentName)
688 UPLEntryBin = ModifyPHSegmentOffset64(UPLEntryBin, ElfHeaderOffset, PHSegmentName)
689 # Modify offsets and address of program header table in elf.
690 PHSegmentName = '.data'
691 _, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, _ = FindSection(UPLEntryBin, PHSegmentName)
692 UPLEntryBin = ModifyPHSegmentOffset64(UPLEntryBin, ElfHeaderOffset, PHSegmentName)
693 fFileBinary.close()
694 return UPLEntryBin
695
696def AddSection32(UniversalPayloadEntry, AddSectionName, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, StringIndexNumber, FileBinary, Alignment):
697 with open(UniversalPayloadEntry,'rb+') as f:
698 # Read Elf and binary which will be write to elf.
699 UPLEntry = f.read()
700 fFileBinary = open(FileBinary, 'rb')
701 Binary_File = fFileBinary.read()
702 ElfHeaderOffset, SectionHeaderEntryNumber, StringIndexNumber, _, _, SectionHeaderEntrySize, _, _ = ElfHeaderParser(UPLEntry)
703 # Read section header entry
704 SHentry = UPLEntry[ElfHeaderOffset:]
705 # Elf header is recombined.
706 # Elf header and program header table in front of first section are reserved.
707 # Elf header is stored at 0x0-0x34 in 32-bits objects
708 elf_header = ElfHeader32(UPLEntry[:52])
709 # Elf header size is 0x34 with 32-bit object.
710 ElfHeaderSize = 52
711 ElfHandPH = ElfHeaderSize + (elf_header.e_phnum * elf_header.e_phentsize)
712 NewUPLEntry = UPLEntry[:ElfHandPH]
713 # Keep Section header and program header table, AddSection32() only recombined section and section header.
714 NewUPLEntry = bytearray(NewUPLEntry)
715 # Sections is recombined.
716 # 1. name of added section is added in name string section.
717 # 2. added section is added in dll file.
718 # 3. re-align sections before and after added section.
719 NewUPLEntrylen = []
720 StringIndexValue = 0
721 for Index in range(0, SectionHeaderEntryNumber):
722 NewUPLEntrylen.append(len(NewUPLEntry))
723 unpacked_SectionHeader = ElfSectionHeader32.unpack(SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
724 # Sections is recombined.
725 if (Index == 0):
726 # Address alignment, section will align with alignment of next section.
727 AlignmentIndex = 8
728 if (SectionHeaderEntryNumber > 2):
729 unpacked_NextSectionHeader = ElfSectionHeader32.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
730 NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
731 # Section is last one.
732 elif (Index == (SectionHeaderEntryNumber - 1)):
733 # Add new section at the end.
734 NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
735 NewUPLEntry = SectionAlignment(NewUPLEntry, Alignment)
736 LastUPLEntrylen = len(NewUPLEntry)
737 NewUPLEntry += Binary_File
738 # Address alignment, section will align with alignment of next section.
739 AlignmentIndex = 8
740 NewUPLEntry = SectionAlignment(NewUPLEntry, AlignmentIndex)
741 # section is name string section.
742 elif (Index == StringIndexNumber):
743 # StringIndex is String Index section
744 StringIndex = UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
745 # Read name of added Section after StringIndex is transform into string.
746 StringIndex = bytearray(StringIndex)
747 StringIndexValue = len(StringIndex)
748 AddSectionName = bytearray(AddSectionName, encoding='utf-8') + bytes('\0', encoding='utf-8')
749 StringIndex += AddSectionName
750 NewUPLEntry += StringIndex
751 # section after name string section but not last one.
752 elif ((Index > StringIndexNumber) and (Index < (SectionHeaderEntryNumber - 1))):
753 NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
754 # Address alignment, section will align with alignment of next section.
755 unpacked_NextSectionHeader = ElfSectionHeader32.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
756 NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
757 # Section before name string section.
758 else:
759 NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
760 # Address alignment, section will align with alignment of next section.
761 if (Index < (SectionHeaderEntryNumber - 1)):
762 unpacked_NextSectionHeader = ElfSectionHeader32.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
763 NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
764 SectionHeaderOffset = len(NewUPLEntry)
765 RemoveNameOffset = 0
766 # Add section header
767 for Number in range(0, (SectionHeaderEntryNumber)):
768 NewSHentry = AddSectionHeader32(SHentry, NewUPLEntrylen[Number], SectionHeaderEntrySize, Number, RemoveNameOffset, AddSectionName, StringIndexNumber)
769 NewUPLEntry += NewSHentry
770 NewUPLEntry += bytearray(AddNewSectionEntry32(LastUPLEntrylen, StringIndexValue, len(Binary_File), Alignment))
771 # Modify number of sections and offset of section header in Elf header.
772 # Modify offset in in Elf header.
773 elf_header.e_shoff = SectionHeaderOffset
774 elf_header.e_shnum += 1
775 PHTableSize = elf_header.e_phentsize
776 elf_header = elf_header.pack()
777 UPLEntryBin = elf_header + NewUPLEntry[52:]
778 # Modify offsets and address of program header table in elf.
779 PHSegmentName = '.text'
780 _, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, _ = FindSection(UPLEntryBin, PHSegmentName)
781 UPLEntryBin = ModifyPHSegmentOffset32(UPLEntryBin, ElfHeaderOffset, PHSegmentName)
782 # Modify offsets and address of program header table in elf. Its are stored at 0x08-0x0F and 0x10-0x17
783 PHSegmentName = '.data'
784 _, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, _ = FindSection(UPLEntryBin, PHSegmentName)
785 UPLEntryBin = ModifyPHSegmentOffset32(UPLEntryBin, ElfHeaderOffset, PHSegmentName)
786 fFileBinary.close()
787 return UPLEntryBin
788
789def ReplaceFv (UniversalPayloadEntry, FileBinary, AddSectionName, Alignment = 16):
790 with open(UniversalPayloadEntry,'rb+') as f:
791 UPLEntry = f.read()
792 SectionNameOffset, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, EI_CLASS = FindSection(UPLEntry, AddSectionName)
793 # If elf is 64-bit objects.
794 if (EI_CLASS == 2):
795 # Remove section if it exists.
796 if (SectionNameOffset != -1):
797 RemoveSection64(UniversalPayloadEntry, AddSectionName)
798 # Add section.
799 NewUPLEntry = AddSection64(UniversalPayloadEntry, AddSectionName, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, StringIndexNumber, FileBinary, Alignment)
800 # If elf is 32-bit objects.
801 else:
802 # Remove section if it exists.
803 if (SectionNameOffset != -1):
804 RemoveSection32(UniversalPayloadEntry, AddSectionName)
805 # Add section.
806 NewUPLEntry = AddSection32(UniversalPayloadEntry, AddSectionName, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, StringIndexNumber, FileBinary, Alignment)
807 with open(UniversalPayloadEntry,'wb') as f:
808 f.write(NewUPLEntry)
809 return 0
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette