summaryrefslogtreecommitdiffstats
path: root/UefiPayloadPkg/Tools/ElfFv.py
blob: 3f0f9272a74346158de22e323c151c7ac55fa012 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
## @file
# OBJCOPY parser, it's used to replace FV
#
# Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
##

import argparse
from   ctypes import *
import struct

class ElfSectionHeader64:
    def __init__(self, sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link, sh_info, sh_addralign, sh_entsize):
        self.sh_name = sh_name
        self.sh_type = sh_type
        self.sh_flags = sh_flags
        self.sh_addr = sh_addr
        self.sh_offset = sh_offset
        self.sh_size = sh_size
        self.sh_link = sh_link
        self.sh_info = sh_info
        self.sh_addralign = sh_addralign
        self.sh_entsize = sh_entsize

    def pack(self):
        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)

    @classmethod
    def unpack(cls, data):
        unpacked_data = struct.unpack('<IIQQQQIIQQ', data)
        return cls(*unpacked_data)

class ElfHeader64:
    def __init__(self, data):
        # Parse the ELF identification bytes
        self.e_ident = struct.unpack('16s', data[:16])[0]
        self.e_type = struct.unpack('H', data[16:18])[0]
        self.e_machine = struct.unpack('H', data[18:20])[0]
        self.e_version = struct.unpack('I', data[20:24])[0]
        self.e_entry = struct.unpack('Q', data[24:32])[0]
        self.e_phoff = struct.unpack('Q', data[32:40])[0]
        self.e_shoff = struct.unpack('Q', data[40:48])[0]
        self.e_flags = struct.unpack('I', data[48:52])[0]
        self.e_ehsize = struct.unpack('H', data[52:54])[0]
        self.e_phentsize = struct.unpack('H', data[54:56])[0]
        self.e_phnum = struct.unpack('H', data[56:58])[0]
        self.e_shentsize = struct.unpack('H', data[58:60])[0]
        self.e_shnum = struct.unpack('H', data[60:62])[0]
        self.e_shstrndx = struct.unpack('H', data[62:64])[0]

    def pack(self):
        # Pack the ELF header data into a binary string
        data = b''
        data += struct.pack('16s', self.e_ident)
        data += struct.pack('H', self.e_type)
        data += struct.pack('H', self.e_machine)
        data += struct.pack('I', self.e_version)
        data += struct.pack('Q', self.e_entry)
        data += struct.pack('Q', self.e_phoff)
        data += struct.pack('Q', self.e_shoff)
        data += struct.pack('I', self.e_flags)
        data += struct.pack('H', self.e_ehsize)
        data += struct.pack('H', self.e_phentsize)
        data += struct.pack('H', self.e_phnum)
        data += struct.pack('H', self.e_shentsize)
        data += struct.pack('H', self.e_shnum)
        data += struct.pack('H', self.e_shstrndx)
        return data

class Elf64_Phdr:
    def __init__(self, data):
        self.p_type = struct.unpack("<L", data[0:4])[0]
        self.p_flags = struct.unpack("<L", data[4:8])[0]
        self.p_offset = struct.unpack("<Q", data[8:16])[0]
        self.p_vaddr = struct.unpack("<Q", data[16:24])[0]
        self.p_paddr = struct.unpack("<Q", data[24:32])[0]
        self.p_filesz = struct.unpack("<Q", data[32:40])[0]
        self.p_memsz = struct.unpack("<Q", data[40:48])[0]
        self.p_align = struct.unpack("<Q", data[48:56])[0]

    def pack(self):
        # Pack the Program header table into a binary string
        data = b''
        data += struct.pack('<L', self.p_type)
        data += struct.pack('<L', self.p_flags)
        data += struct.pack('<Q', self.p_offset)
        data += struct.pack('<Q', self.p_vaddr)
        data += struct.pack('<Q', self.p_paddr)
        data += struct.pack('<Q', self.p_filesz)
        data += struct.pack('<Q', self.p_memsz)
        data += struct.pack('<Q', self.p_align)
        return data

class ElfSectionHeader32:
    def __init__(self, sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link, sh_info, sh_addralign, sh_entsize):
        self.sh_name = sh_name
        self.sh_type = sh_type
        self.sh_flags = sh_flags
        self.sh_addr = sh_addr
        self.sh_offset = sh_offset
        self.sh_size = sh_size
        self.sh_link = sh_link
        self.sh_info = sh_info
        self.sh_addralign = sh_addralign
        self.sh_entsize = sh_entsize

    def pack(self):
        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)

    @classmethod
    def unpack(cls, data):
        unpacked_data = struct.unpack('<IIIIIIIIII', data)
        return cls(*unpacked_data)

class ElfHeader32:
    def __init__(self, data):
        # Parse the ELF identification bytes
        self.e_ident = struct.unpack('16s', data[:16])[0]
        self.e_type = struct.unpack('H', data[16:18])[0]
        self.e_machine = struct.unpack('H', data[18:20])[0]
        self.e_version = struct.unpack('I', data[20:24])[0]
        self.e_entry = struct.unpack('I', data[24:28])[0]
        self.e_phoff = struct.unpack('I', data[28:32])[0]
        self.e_shoff = struct.unpack('I', data[32:36])[0]
        self.e_flags = struct.unpack('I', data[36:40])[0]
        self.e_ehsize = struct.unpack('H', data[40:42])[0]
        self.e_phentsize = struct.unpack('H', data[42:44])[0]
        self.e_phnum = struct.unpack('H', data[44:46])[0]
        self.e_shentsize = struct.unpack('H', data[46:48])[0]
        self.e_shnum = struct.unpack('H', data[48:50])[0]
        self.e_shstrndx = struct.unpack('H', data[50:52])[0]

    def pack(self):
        # Pack the ELF header data into a binary string
        data = b''
        data += struct.pack('16s', self.e_ident)
        data += struct.pack('H', self.e_type)
        data += struct.pack('H', self.e_machine)
        data += struct.pack('I', self.e_version)
        data += struct.pack('I', self.e_entry)
        data += struct.pack('I', self.e_phoff)
        data += struct.pack('I', self.e_shoff)
        data += struct.pack('I', self.e_flags)
        data += struct.pack('H', self.e_ehsize)
        data += struct.pack('H', self.e_phentsize)
        data += struct.pack('H', self.e_phnum)
        data += struct.pack('H', self.e_shentsize)
        data += struct.pack('H', self.e_shnum)
        data += struct.pack('H', self.e_shstrndx)
        return data

class Elf32_Phdr:
    def __init__(self, data):
        self.p_type = struct.unpack("<L", data[0:4])[0]
        self.p_offset = struct.unpack("<L", data[4:8])[0]
        self.p_vaddr = struct.unpack("<L", data[8:12])[0]
        self.p_paddr = struct.unpack("<L", data[12:16])[0]
        self.p_filesz = struct.unpack("<L", data[16:20])[0]
        self.p_memsz = struct.unpack("<L", data[20:24])[0]
        self.p_flags = struct.unpack("<L", data[24:28])[0]
        self.p_align = struct.unpack("<L", data[28:32])[0]

    def pack(self):
        # Pack the Program header table into a binary string
        data = b''
        data += struct.pack('<L', self.p_type)
        data += struct.pack('<L', self.p_offset)
        data += struct.pack('<L', self.p_vaddr)
        data += struct.pack('<L', self.p_paddr)
        data += struct.pack('<L', self.p_filesz)
        data += struct.pack('<L', self.p_memsz)
        data += struct.pack('<L', self.p_flags)
        data += struct.pack('<L', self.p_align)
        return data

def SectionAlignment(NewUPLEntry, AlignmentIndex):
    # Section entry Alignment
    # Alignment is transfer to integer if AlignmentIndex is string.
    if isinstance(AlignmentIndex, str):
        int_num = int(AlignmentIndex, 16)
        int_num = 10 * (int_num//16) + int_num % 16
    else:
        int_num = AlignmentIndex
    if (int_num != 0 or int_num != 1):
        if ((len(NewUPLEntry) % int_num) != 0):
            AlignNumber = int_num - (len(NewUPLEntry) % int_num)
            if (AlignNumber != 0):
                for x in range(AlignNumber):
                    NewUPLEntry = NewUPLEntry + bytearray(b'\0')
    return NewUPLEntry

def SectionEntryFill(SectionEntry, Alignment, Value, Offset):
    # Alignment
    n = 0
    if (len (Value) < Alignment):
        Value = Value.zfill(Alignment)
    for x in range(0, (Alignment//2)):
        Index = '0x' + Value[n] + Value[n + 1]
        SectionEntry[Offset - x] = int(Index,16)
        n += 2
    return SectionEntry

def ElfHeaderParser(UPLEntry):
    # Read EI_CLASS, it stores information that elf with 32-bit or 64-bit architectures.
    EI_CLASS = UPLEntry[4]
    # If Elf is 64-bit objects.
    if (EI_CLASS == 2):
        # Elf header is stored at 0x0-0x40 in 64-bits objects
        ElfHeaderData = UPLEntry[:64]
    # If Elf is 32-bit objects.
    else:
        # Elf header is stored at 0x0-0x34 in 32-bits objects
        ElfHeaderData = UPLEntry[:53]
    # If Elf is 64-bit objects.
    if (EI_CLASS == 2):
        elf_header = ElfHeader64(ElfHeaderData)
        ElfHeaderOffset = elf_header.e_shoff
        SectionHeaderEntryNumber = elf_header.e_shnum
        StringIndexNumber = elf_header.e_shstrndx
        SectionHeaderEntrySize = elf_header.e_shentsize
        StringIndexEntryOffset = ElfHeaderOffset + (StringIndexNumber * SectionHeaderEntrySize)
        unpacked_header = ElfSectionHeader64.unpack(UPLEntry[StringIndexEntryOffset: (StringIndexEntryOffset + SectionHeaderEntrySize)])
        StringIndexSize = unpacked_header.sh_size
        StringIndexOffset = unpacked_header.sh_offset
    # If elf is 32-bit objects.
    else:
        elf_header = ElfHeader32(ElfHeaderData)
        ElfHeaderOffset = elf_header.e_shoff
        SectionHeaderEntryNumber = elf_header.e_shnum
        StringIndexNumber = elf_header.e_shstrndx
        SectionHeaderEntrySize = elf_header.e_shentsize
        StringIndexEntryOffset = ElfHeaderOffset + (StringIndexNumber * SectionHeaderEntrySize)
        unpacked_header = ElfSectionHeader32.unpack(UPLEntry[StringIndexEntryOffset: (StringIndexEntryOffset + SectionHeaderEntrySize)])
        StringIndexSize = unpacked_header.sh_size
        StringIndexOffset = unpacked_header.sh_offset
    return ElfHeaderOffset, SectionHeaderEntryNumber, StringIndexNumber, StringIndexEntryOffset, StringIndexSize, SectionHeaderEntrySize, StringIndexOffset, EI_CLASS

def FindSection(UPLEntry, SectionName):
    ElfHeaderOffset, SectionHeaderEntryNumber, StringIndexNumber, _, StringIndexSize, SectionHeaderEntrySize, StringIndexOffset, EI_CLASS = ElfHeaderParser(UPLEntry)
    # StringIndex is String Index section
    StringIndex = UPLEntry[StringIndexOffset:StringIndexOffset+StringIndexSize]
    # Section header isn't exist if SectionNameOffset = -1.
    StringIndex = StringIndex.decode('utf-8', errors='ignore')
    SectionNameOffset = StringIndex.find(SectionName)
    return SectionNameOffset, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, StringIndexOffset, StringIndexNumber, EI_CLASS

def AddNewSectionEntry64(LastUPLEntrylen, StringIndexValue, SectionSize, Alignment):
    # If elf is 64-bit objects.
    NewSectionEntry = ElfSectionHeader64 (StringIndexValue, 1, 0, 0, LastUPLEntrylen, SectionSize, 0, 0, Alignment, 0)
    sh_bytes = NewSectionEntry.pack()
    return sh_bytes

def AddNewSectionEntry32(LastUPLEntrylen, StringIndexValue, SectionSize, Alignment):
    # If elf is 32-bit objects.
    NewSectionEntry = ElfSectionHeader32 (StringIndexValue, 1, 0, 0, LastUPLEntrylen, SectionSize, 0, 0, Alignment, 0)
    sh_bytes = NewSectionEntry.pack()
    return sh_bytes

def AddSectionHeader64(SHentry, NewUPLEntrylen, SectionHeaderEntrySize, Index, RemoveNameOffset, SectionName, StringIndexNumber):
    SHentry = bytearray(SHentry)
    unpacked_header = ElfSectionHeader64.unpack(SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
    # Section header of section 0 shows 0. It don't modify any offset.
    if (Index != 0):
        # read section offset.
        unpacked_header.sh_offset = NewUPLEntrylen
        # Modify offset of name in section entry
        # if RemoveNameOffset != 0 that is remove function.
        if (RemoveNameOffset != 0):
            if (unpacked_header.sh_name > RemoveNameOffset):
                unpacked_header.sh_name -= len (SectionName)
            # Modify size of name string section entry in section entry.
            if (Index == StringIndexNumber):
                unpacked_header.sh_size -= len (SectionName)
        # added section
        else :
            if (Index == StringIndexNumber):
                unpacked_header.sh_size += len (SectionName)
    NewSHentry = ElfSectionHeader64 (
        unpacked_header.sh_name,
        unpacked_header.sh_type,
        unpacked_header.sh_flags,
        unpacked_header.sh_addr,
        unpacked_header.sh_offset,
        unpacked_header.sh_size,
        unpacked_header.sh_link,
        unpacked_header.sh_info,
        unpacked_header.sh_addralign,
        unpacked_header.sh_entsize).pack()
    return NewSHentry

def AddSectionHeader32(SHentry, NewUPLEntrylen, SectionHeaderEntrySize, Index, RemoveNameOffset, SectionName, StringIndexNumber):
    SHentry = bytearray(SHentry)
    unpacked_header = ElfSectionHeader32.unpack(SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
    if (Index != 0):
        NewSHentry = SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)]
        unpacked_header.sh_offset = NewUPLEntrylen
        # Modify offset of name in section entry
        # if RemoveNameOffset != 0 that is remove function.
        if (RemoveNameOffset != 0):
            if (unpacked_header.sh_name > RemoveNameOffset):
                unpacked_header.sh_name -= len (SectionName)
            # Modify size of name string section entry in section entry.
            if (Index == StringIndexNumber):
                unpacked_header.sh_size -= len (SectionName)
        # added section
        else :
            if (Index == StringIndexNumber):
                unpacked_header.sh_size += len (SectionName)
    NewSHentry = ElfSectionHeader32 (
        unpacked_header.sh_name,
        unpacked_header.sh_type,
        unpacked_header.sh_flags,
        unpacked_header.sh_addr,
        unpacked_header.sh_offset,
        unpacked_header.sh_size,
        unpacked_header.sh_link,
        unpacked_header.sh_info,
        unpacked_header.sh_addralign,
        unpacked_header.sh_entsize).pack()
    return NewSHentry

def ModifyPHSegmentOffset64(NewUPLEntry, ElfHeaderOffset, PHSegmentName):
    # Modify offset and address of program header tables.
    elf_header = ElfHeader64(NewUPLEntry[:64])
    SHentry = NewUPLEntry[ElfHeaderOffset:]
    # Elf program header tables start from 0x40 in 64-bits objects
    PHentry = NewUPLEntry[64: 64 + (elf_header.e_phnum * elf_header.e_phentsize)]
    PHdrs = []
    SHdrs = []
    for i in range(elf_header.e_shnum):
        SHData = SHentry[(i * elf_header.e_shentsize): (i * elf_header.e_shentsize) + elf_header.e_shentsize]
        unpacked_SectionHeader = ElfSectionHeader64.unpack(SHData)
        SHdrs.append(unpacked_SectionHeader)
    for i in range(elf_header.e_phnum):
        PHData = PHentry[(i * elf_header.e_phentsize): (i * elf_header.e_phentsize) + elf_header.e_phentsize]
        unpacked_ProgramHeader = Elf64_Phdr(PHData)
        PHdrs.append(unpacked_ProgramHeader)
    if (PHSegmentName == '.text'):
        PHdrs[0].p_offset = SHdrs[1].sh_offset
        PHdrs[0].p_paddr = SHdrs[1].sh_addr
        PHdrs[4].p_offset = SHdrs[1].sh_offset
        PHdrs[4].p_paddr = SHdrs[1].sh_addr
    elif (PHSegmentName == '.dynamic'):
        PHdrs[1].p_offset = SHdrs[2].sh_offset
        PHdrs[1].p_paddr = SHdrs[2].sh_addr
        PHdrs[3].p_offset = SHdrs[2].sh_offset
        PHdrs[3].p_paddr = SHdrs[2].sh_addr
    elif (PHSegmentName == '.data'):
        PHdrs[2].p_offset = SHdrs[3].sh_offset
        PHdrs[2].p_paddr = SHdrs[3].sh_addr
    packed_PHData = b''
    for phdr in PHdrs:
        packed_PHData += phdr.pack()
    NewUPLEntry = bytearray(NewUPLEntry)
    NewUPLEntry[64: 64 + (elf_header.e_phnum * elf_header.e_phentsize)] = packed_PHData
    return NewUPLEntry

def ModifyPHSegmentOffset32(NewUPLEntry, ElfHeaderOffset, PHSegmentName):
    # Modify offset and address of program header tables.
    # Elf header is stored at 0x0-0x34 in 32-bits objects
    elf_header = ElfHeader32(NewUPLEntry[:52])
    SHentry = NewUPLEntry[ElfHeaderOffset:]
    # Elf program header tables start from 0x34 in 32-bits objects
    PHentry = NewUPLEntry[52: 52 + (elf_header.e_phnum * elf_header.e_phentsize)]
    PHdrs = []
    SHdrs = []
    for i in range(elf_header.e_shnum):
        SHData = SHentry[(i * elf_header.e_shentsize): (i * elf_header.e_shentsize) + elf_header.e_shentsize]
        unpacked_SectionHeader = ElfSectionHeader32.unpack(SHData)
        SHdrs.append(unpacked_SectionHeader)
    for i in range(elf_header.e_phnum):
        PHData = PHentry[(i * elf_header.e_phentsize): (i * elf_header.e_phentsize) + elf_header.e_phentsize]
        unpacked_ProgramHeader = Elf32_Phdr(PHData)
        PHdrs.append(unpacked_ProgramHeader)
    if (PHSegmentName == '.text'):
        PHdrs[0].p_offset = SHdrs[1].sh_offset
        PHdrs[0].p_paddr = SHdrs[1].sh_addr
        PHdrs[0].p_vaddr = SHdrs[1].sh_addr
        PHdrs[2].p_offset = SHdrs[1].sh_offset
        PHdrs[2].p_paddr = SHdrs[1].sh_addr
        PHdrs[0].p_vaddr = SHdrs[1].sh_addr
    elif (PHSegmentName == '.data'):
        PHdrs[1].p_offset = SHdrs[2].sh_offset
        PHdrs[1].p_paddr = SHdrs[2].sh_addr
        PHdrs[1].p_vaddr = SHdrs[2].sh_addr
    packed_PHData = b''
    for phdr in PHdrs:
        packed_PHData += phdr.pack()
    NewUPLEntry = bytearray(NewUPLEntry)
    NewUPLEntry[52: 52 + (elf_header.e_phnum * elf_header.e_phentsize)] = packed_PHData
    return NewUPLEntry

def RemoveSection64(UniversalPayloadEntry, RemoveSectionName):
    # If elf is 64-bit objects.
    # Get offsets as follows:
    #                      1. Section name which will remove in section name string.
    #                      2. Section which will remove.
    #                      3. Section header which will remove.
    with open(UniversalPayloadEntry,'rb') as f:
        UPLEntry = f.read()
        RemoveSectionNameOffset, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, _ = FindSection(UPLEntry, RemoveSectionName)
        if (RemoveSectionNameOffset == -1):
            raise argparse.ArgumentTypeError ('Section: {} not found.'.format (RemoveSectionNameOffset))
        # Read section header entry
        SHentry = UPLEntry[ElfHeaderOffset:]
        # find deleted fv section offset.
        # Elf header is stored at 0x0-0x40 in 64-bits objects
        elf_header = ElfHeader64(UPLEntry[:64])
        Counter = 0
        RemoveIndex = 0
        RemoveNameOffset = 0
        for Index in range(0, elf_header.e_shnum):
            # Read Index of section header.
            unpacked_SectionHeader = ElfSectionHeader64.unpack(SHentry[(Index * elf_header.e_shentsize):((Index * elf_header.e_shentsize) + elf_header.e_shentsize)])
            # Find offset of section name which is removed.
            if (unpacked_SectionHeader.sh_name == RemoveSectionNameOffset):
                RemoveIndex = Counter
                Counter += 1
            else:
                Counter += 1
        # Elf header is recombined.
        # Elf header and program header table in front of first section are reserved.
        # Elf header size is 0x40 with 64-bit object.
        ElfHeaderSize = 64
        ElfHandPH = ElfHeaderSize + (elf_header.e_phnum * elf_header.e_phentsize)
        NewUPLEntry = UPLEntry[:ElfHandPH]
        # Keep Section header and program header table, RemoveSection64() only recombined section and section header.
        NewUPLEntry = bytearray(NewUPLEntry)
        # Sections is recombined.
        # 1. name of deleted section is removed in name string section.
        # 2. deleted section is removed in dll file.
        # 3. re-align sections before and after deleted section.
        NewUPLEntrylen = []
        for Index in range(0, (SectionHeaderEntryNumber)):
            unpacked_SectionHeader = ElfSectionHeader64.unpack(SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
            NewUPLEntrylen.append(len(NewUPLEntry))
            if (Index == 0):
                # Address alignment, section will align with alignment of next section.
                AlignmentIndex = 8
                if (SectionHeaderEntryNumber > 2):
                   unpacked_NextSectionHeader = ElfSectionHeader64.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
                NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
            # Section in front of removed section
            elif (Index + 1 == RemoveIndex):
                NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                # Read section address alignment
                # If section that will be removed in .dll is not first and last one .
                # Address alignment, section will align with alignment of section after deleted section.
                # Check next and the section after next are not end of section.
                if ((Index + 2) < (SectionHeaderEntryNumber - 1)):
                    unpacked_Next2SectionHeader = ElfSectionHeader64.unpack(SHentry[((Index + 2) * SectionHeaderEntrySize):(((Index + 2) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
                    NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_Next2SectionHeader.sh_addralign)
                else:
                    # It is align 8 bytes if next section or the section after next is last one.
                    AlignmentIndex = 8
                    NewUPLEntry = SectionAlignment(NewUPLEntry, AlignmentIndex)
            # section is Deleted section
            elif (Index  == RemoveIndex):
                # Don't add removed section to elf.
                # Find offset of section name.
                RemoveNameOffset = unpacked_SectionHeader.sh_name
            # section is name string section.
            elif (Index  == StringIndexNumber):
                # StringIndex is String Index section
                StringIndex = UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                # Remove name of removed section in name string section.
                # Section header isn't exist if RemoveSectionNameOffset equal to -1.
                StringIndex = bytearray(StringIndex)
                RemoveSectionName = bytearray(RemoveSectionName, encoding='utf-8')
                RemoveSectionName = RemoveSectionName + bytes('\0', encoding='utf-8')
                StringIndex = StringIndex.replace(RemoveSectionName,b'')
                NewUPLEntry += StringIndex
            # other sections.
            else:
                NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                # Address alignment, section will align with alignment of next section.
                if (Index < (SectionHeaderEntryNumber - 1)):
                    NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
                else:
                    # If section is last one.
                    AlignmentIndex = 8
                    NewUPLEntry = SectionAlignment(NewUPLEntry, AlignmentIndex)
        SectionHeaderOffset = len(NewUPLEntry)
        # Add section header
        for Number in range(0, (SectionHeaderEntryNumber)):
            if (Number  != RemoveIndex):
                NewSHentry = AddSectionHeader64(SHentry, NewUPLEntrylen[Number], SectionHeaderEntrySize, Number, RemoveNameOffset, RemoveSectionName, StringIndexNumber)
                NewUPLEntry += NewSHentry
        # Modify number of sections and offset of section header in Elf header.
        elf_header.e_shoff = SectionHeaderOffset
        elf_header.e_shnum -= 1
        NewUPLEntry = elf_header.pack() + NewUPLEntry[64:]
        # write to Elf.
        with open(UniversalPayloadEntry,'wb') as f:
            f.write(NewUPLEntry)

def RemoveSection32(UniversalPayloadEntry, RemoveSectionName):
    # If elf is 32-bit objects.
    # Get offsets as follows:
    #                      1. Section name which will remove in section name string.
    #                      2. Section which will remove.
    #                      3. Section header which will remove.
    with open(UniversalPayloadEntry,'rb') as f:
        UPLEntry = f.read()
        RemoveSectionNameOffset, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, EI_CLASS = FindSection(UPLEntry, RemoveSectionName)
        if (RemoveSectionNameOffset == -1):
            raise argparse.ArgumentTypeError ('Section: {} not found.'.format (RemoveSectionNameOffset))
        # Read section header entry
        SHentry = UPLEntry[ElfHeaderOffset:]
        # find deleted fv section offset.
        # Elf header is stored at 0x0-0x34 in 32-bits objects
        elf_header = ElfHeader32(UPLEntry[:52])
        Counter = 0
        RemoveIndex = 0
        RemoveNameOffset = 0
        for Index in range(0, elf_header.e_shnum):
            # Read Index of section header.
            unpacked_SectionHeader = ElfSectionHeader32.unpack(SHentry[(Index * elf_header.e_shentsize):((Index * elf_header.e_shentsize) + elf_header.e_shentsize)])
            # Find offset of section name which is removed.
            if (unpacked_SectionHeader.sh_name == RemoveSectionNameOffset):
                RemoveIndex = Counter
                Counter += 1
            else:
                Counter += 1
        # Elf header is recombined.
        # Elf header and program header table in front of first section are reserved.
        # Elf header size is 0x34 with 32-bit object.
        ElfHeaderSize = 52
        ElfHandPH = ElfHeaderSize + (elf_header.e_phnum * elf_header.e_phentsize)
        NewUPLEntry = UPLEntry[:ElfHandPH]
        # Keep Section header and program header table, RemoveSection32() only recombined section and section header.
        NewUPLEntry = bytearray(NewUPLEntry)
        # Sections is recombined.
        # 1. name of deleted section is removed in name string section.
        # 2. deleted section is removed in dll file.
        # 3. re-align sections before and after deleted section.
        NewUPLEntrylen = []
        for Index in range(0, (SectionHeaderEntryNumber)):
            unpacked_SectionHeader = ElfSectionHeader32.unpack(SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
            NewUPLEntrylen.append(len(NewUPLEntry))
            if (Index == 0):
                # Address alignment, section will align with alignment of next section.
                AlignmentIndex = 8
                if (SectionHeaderEntryNumber > 2):
                   unpacked_NextSectionHeader = ElfSectionHeader32.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
                NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
            # Section in front of removed section
            elif (Index + 1 == RemoveIndex):
                NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                # Read section address alignment
                # If section that will be removed in .dll is not first and last one .
                # Address alignment, section will align with alignment of section after deleted section.
                # Check next and the section after next are not end of section.
                if ((Index + 2) < (SectionHeaderEntryNumber - 1)):
                    unpacked_Next2SectionHeader = ElfSectionHeader32.unpack(SHentry[((Index + 2) * SectionHeaderEntrySize):(((Index + 2) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
                    NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_Next2SectionHeader.sh_addralign)
                else:
                    # It is align 8 bytes if next section or the section after next is last one.
                    AlignmentIndex = 8
                    NewUPLEntry = SectionAlignment(NewUPLEntry, AlignmentIndex)
            # section is Deleted section
            elif (Index  == RemoveIndex):
                # Don't add removed section to elf.
                # Find offset of section name.
                RemoveNameOffset = unpacked_SectionHeader.sh_name
            # section is name string section.
            elif (Index  == StringIndexNumber):
                # StringIndex is String Index section
                StringIndex = UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                # Remove name of removed section in name string section.
                # Section header isn't exist if RemoveSectionNameOffset equal to -1.
                StringIndex = bytearray(StringIndex)
                RemoveSectionName = bytearray(RemoveSectionName, encoding='utf-8')
                RemoveSectionName = RemoveSectionName + bytes('\0', encoding='utf-8')
                StringIndex = StringIndex.replace(RemoveSectionName,b'')
                NewUPLEntry += StringIndex
            # other sections.
            else:
                NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                # Address alignment, section will align with alignment of next section.
                if (Index < (SectionHeaderEntryNumber - 1)):
                    NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
                else:
                    # If section is last one.
                    AlignmentIndex = 8
                    NewUPLEntry = SectionAlignment(NewUPLEntry, AlignmentIndex)
        SectionHeaderOffset = len(NewUPLEntry)
        # Add section header
        for Number in range(0, (SectionHeaderEntryNumber)):
            if (Number  != RemoveIndex):
                NewSHentry = AddSectionHeader32(SHentry, NewUPLEntrylen[Number], SectionHeaderEntrySize, Number, RemoveNameOffset, RemoveSectionName, StringIndexNumber)
                NewUPLEntry += NewSHentry
        # Modify number of sections and offset of section header in Elf header.
        elf_header.e_shoff = SectionHeaderOffset
        elf_header.e_shnum -= 1
        NewUPLEntry = elf_header.pack() + NewUPLEntry[52:]
        # write to Elf.
        with open(UniversalPayloadEntry,'wb') as f:
            f.write(NewUPLEntry)

def AddSection64(UniversalPayloadEntry, AddSectionName, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, StringIndexNumber, FileBinary, Alignment):
    with open(UniversalPayloadEntry,'rb+') as f:
        UPLEntry = f.read()
        fFileBinary = open(FileBinary, 'rb')
        Binary_File = fFileBinary.read()
        ElfHeaderOffset, SectionHeaderEntryNumber, StringIndexNumber, _, _, SectionHeaderEntrySize, _, _ = ElfHeaderParser(UPLEntry)
        # Read section header entry
        SHentry = UPLEntry[ElfHeaderOffset:]
        # Elf header is recombined.
        # Elf header and program header table in front of first section are reserved.
        # Elf header is stored at 0x0-0x40 in 64-bits objects
        elf_header = ElfHeader64(UPLEntry[:64])
        # Elf header size is 0x40 with 64-bit object.
        ElfHeaderSize = 64
        ElfHandPH = ElfHeaderSize + (elf_header.e_phnum * elf_header.e_phentsize)
        NewUPLEntry = UPLEntry[:ElfHandPH]
        # Keep Section header and program header table, AddSection64() only recombined section and section header.
        NewUPLEntry = bytearray(NewUPLEntry)
        # Sections is recombined.
        # 1. name of added section is added in name string section.
        # 2. added section is added in dll file.
        # 3. re-align sections before and after added section.
        NewUPLEntrylen = []
        StringIndexValue = 0
        for Index in range(0, SectionHeaderEntryNumber):
            NewUPLEntrylen.append(len(NewUPLEntry))
            unpacked_SectionHeader = ElfSectionHeader64.unpack(SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
            # Sections is recombined.
            if (Index == 0):
                # Address alignment, section will align with alignment of next section.
                AlignmentIndex = 8
                if (SectionHeaderEntryNumber > 2):
                    unpacked_NextSectionHeader = ElfSectionHeader64.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
                NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
            # Section is last one.
            elif (Index == (SectionHeaderEntryNumber - 1)):
                # Add new section at the end.
                NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                NewUPLEntry = SectionAlignment(NewUPLEntry, Alignment)
                LastUPLEntrylen = len(NewUPLEntry)
                NewUPLEntry += Binary_File
                # Address alignment, section will align with alignment of next section.
                AlignmentIndex = 8
                NewUPLEntry = SectionAlignment(NewUPLEntry, AlignmentIndex)
            # section is name string section.
            elif (Index  == StringIndexNumber):
                # StringIndex is String Index section
                StringIndex = UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                # Read name of added Section after StringIndex is transform into string.
                StringIndex = bytearray(StringIndex)
                StringIndexValue = len(StringIndex)
                AddSectionName = bytearray(AddSectionName, encoding='utf-8') + bytes('\0', encoding='utf-8')
                StringIndex += AddSectionName
                NewUPLEntry += StringIndex
            # section after name string section but not last one.
            elif ((Index > StringIndexNumber) and (Index < (SectionHeaderEntryNumber - 1))):
                NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                # Address alignment, section will align with alignment of next section.
                unpacked_NextSectionHeader = ElfSectionHeader64.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
                NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
            # Section before name string section.
            else:
                NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                    # Address alignment, section will align with alignment of next section.
                if (Index < (SectionHeaderEntryNumber - 1)):
                    unpacked_NextSectionHeader = ElfSectionHeader64.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
                    NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
        SectionHeaderOffset = len(NewUPLEntry)
        RemoveNameOffset = 0
        # Add section header
        for Number in range(0, (SectionHeaderEntryNumber)):
            NewSHentry = AddSectionHeader64(SHentry, NewUPLEntrylen[Number], SectionHeaderEntrySize, Number, RemoveNameOffset, AddSectionName, StringIndexNumber)
            NewUPLEntry += NewSHentry
        NewUPLEntry += bytearray(AddNewSectionEntry64(LastUPLEntrylen, StringIndexValue, len(Binary_File), Alignment))
        # Modify number of sections and offset of section header in Elf header.
        # Modify offset in in Elf header.
        elf_header.e_shoff = SectionHeaderOffset
        elf_header.e_shnum += 1
        elf_header = elf_header.pack()
        UPLEntryBin = elf_header + NewUPLEntry[64:]
        # Modify offsets and address of program header table in elf.
        PHSegmentName = '.text'
        _, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, _ = FindSection(UPLEntryBin, PHSegmentName)
        UPLEntryBin = ModifyPHSegmentOffset64(UPLEntryBin, ElfHeaderOffset, PHSegmentName)
        # Modify offsets and address of program header table in elf.
        PHSegmentName = '.dynamic'
        _, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, _ = FindSection(UPLEntryBin, PHSegmentName)
        UPLEntryBin = ModifyPHSegmentOffset64(UPLEntryBin, ElfHeaderOffset, PHSegmentName)
        # Modify offsets and address of program header table in elf.
        PHSegmentName = '.data'
        _, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, _ = FindSection(UPLEntryBin, PHSegmentName)
        UPLEntryBin = ModifyPHSegmentOffset64(UPLEntryBin, ElfHeaderOffset, PHSegmentName)
    fFileBinary.close()
    return UPLEntryBin

def AddSection32(UniversalPayloadEntry, AddSectionName, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, StringIndexNumber, FileBinary, Alignment):
    with open(UniversalPayloadEntry,'rb+') as f:
        # Read Elf and binary which will be write to elf.
        UPLEntry = f.read()
        fFileBinary = open(FileBinary, 'rb')
        Binary_File = fFileBinary.read()
        ElfHeaderOffset, SectionHeaderEntryNumber, StringIndexNumber, _, _, SectionHeaderEntrySize, _, _ = ElfHeaderParser(UPLEntry)
        # Read section header entry
        SHentry = UPLEntry[ElfHeaderOffset:]
        # Elf header is recombined.
        # Elf header and program header table in front of first section are reserved.
        # Elf header is stored at 0x0-0x34 in 32-bits objects
        elf_header = ElfHeader32(UPLEntry[:52])
        # Elf header size is 0x34 with 32-bit object.
        ElfHeaderSize = 52
        ElfHandPH = ElfHeaderSize + (elf_header.e_phnum * elf_header.e_phentsize)
        NewUPLEntry = UPLEntry[:ElfHandPH]
        # Keep Section header and program header table, AddSection32() only recombined section and section header.
        NewUPLEntry = bytearray(NewUPLEntry)
        # Sections is recombined.
        # 1. name of added section is added in name string section.
        # 2. added section is added in dll file.
        # 3. re-align sections before and after added section.
        NewUPLEntrylen = []
        StringIndexValue = 0
        for Index in range(0, SectionHeaderEntryNumber):
            NewUPLEntrylen.append(len(NewUPLEntry))
            unpacked_SectionHeader = ElfSectionHeader32.unpack(SHentry[(Index * SectionHeaderEntrySize):((Index * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
            # Sections is recombined.
            if (Index == 0):
                # Address alignment, section will align with alignment of next section.
                AlignmentIndex = 8
                if (SectionHeaderEntryNumber > 2):
                    unpacked_NextSectionHeader = ElfSectionHeader32.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
                NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
            # Section is last one.
            elif (Index == (SectionHeaderEntryNumber - 1)):
                # Add new section at the end.
                NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                NewUPLEntry = SectionAlignment(NewUPLEntry, Alignment)
                LastUPLEntrylen = len(NewUPLEntry)
                NewUPLEntry += Binary_File
                # Address alignment, section will align with alignment of next section.
                AlignmentIndex = 8
                NewUPLEntry = SectionAlignment(NewUPLEntry, AlignmentIndex)
            # section is name string section.
            elif (Index  == StringIndexNumber):
                # StringIndex is String Index section
                StringIndex = UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                # Read name of added Section after StringIndex is transform into string.
                StringIndex = bytearray(StringIndex)
                StringIndexValue = len(StringIndex)
                AddSectionName = bytearray(AddSectionName, encoding='utf-8') + bytes('\0', encoding='utf-8')
                StringIndex += AddSectionName
                NewUPLEntry += StringIndex
            # section after name string section but not last one.
            elif ((Index > StringIndexNumber) and (Index < (SectionHeaderEntryNumber - 1))):
                NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                # Address alignment, section will align with alignment of next section.
                unpacked_NextSectionHeader = ElfSectionHeader32.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
                NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
            # Section before name string section.
            else:
                NewUPLEntry += UPLEntry[unpacked_SectionHeader.sh_offset:(unpacked_SectionHeader.sh_offset + unpacked_SectionHeader.sh_size)]
                    # Address alignment, section will align with alignment of next section.
                if (Index < (SectionHeaderEntryNumber - 1)):
                    unpacked_NextSectionHeader = ElfSectionHeader32.unpack(SHentry[((Index + 1) * SectionHeaderEntrySize):(((Index + 1) * SectionHeaderEntrySize) + SectionHeaderEntrySize)])
                    NewUPLEntry = SectionAlignment(NewUPLEntry, unpacked_NextSectionHeader.sh_addralign)
        SectionHeaderOffset = len(NewUPLEntry)
        RemoveNameOffset = 0
        # Add section header
        for Number in range(0, (SectionHeaderEntryNumber)):
            NewSHentry = AddSectionHeader32(SHentry, NewUPLEntrylen[Number], SectionHeaderEntrySize, Number, RemoveNameOffset, AddSectionName, StringIndexNumber)
            NewUPLEntry += NewSHentry
        NewUPLEntry += bytearray(AddNewSectionEntry32(LastUPLEntrylen, StringIndexValue, len(Binary_File), Alignment))
        # Modify number of sections and offset of section header in Elf header.
        # Modify offset in in Elf header.
        elf_header.e_shoff = SectionHeaderOffset
        elf_header.e_shnum += 1
        PHTableSize = elf_header.e_phentsize
        elf_header = elf_header.pack()
        UPLEntryBin = elf_header + NewUPLEntry[52:]
        # Modify offsets and address of program header table in elf.
        PHSegmentName = '.text'
        _, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, _ = FindSection(UPLEntryBin, PHSegmentName)
        UPLEntryBin = ModifyPHSegmentOffset32(UPLEntryBin, ElfHeaderOffset, PHSegmentName)
        # Modify offsets and address of program header table in elf. Its are stored at 0x08-0x0F and 0x10-0x17
        PHSegmentName = '.data'
        _, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, _ = FindSection(UPLEntryBin, PHSegmentName)
        UPLEntryBin = ModifyPHSegmentOffset32(UPLEntryBin, ElfHeaderOffset, PHSegmentName)
    fFileBinary.close()
    return UPLEntryBin

def ReplaceFv (UniversalPayloadEntry, FileBinary, AddSectionName, Alignment = 16):
    with open(UniversalPayloadEntry,'rb+') as f:
        UPLEntry = f.read()
        SectionNameOffset, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, _, StringIndexNumber, EI_CLASS = FindSection(UPLEntry, AddSectionName)
    # If elf is 64-bit objects.
    if (EI_CLASS == 2):
        # Remove section if it exists.
        if (SectionNameOffset != -1):
            RemoveSection64(UniversalPayloadEntry, AddSectionName)
        # Add section.
        NewUPLEntry = AddSection64(UniversalPayloadEntry, AddSectionName, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, StringIndexNumber, FileBinary, Alignment)
    # If elf is 32-bit objects.
    else:
        # Remove section if it exists.
        if (SectionNameOffset != -1):
            RemoveSection32(UniversalPayloadEntry, AddSectionName)
        # Add section.
        NewUPLEntry = AddSection32(UniversalPayloadEntry, AddSectionName, ElfHeaderOffset, SectionHeaderEntrySize, SectionHeaderEntryNumber, StringIndexNumber, FileBinary, Alignment)
    with open(UniversalPayloadEntry,'wb') as f:
        f.write(NewUPLEntry)
    return 0