forked from nasa/elf2cfetbl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elf2cfetbl.c
2623 lines (2347 loc) · 82 KB
/
elf2cfetbl.c
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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
**
** GSC-18128-1, "Core Flight Executive Version 6.7"
**
** Copyright (c) 2006-2019 United States Government as represented by
** the Administrator of the National Aeronautics and Space Administration.
** All Rights Reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
**
** Purpose:
** This file implements the ELF file to Standard cFE Table file format tool
**
** Notes:
**
**
*/
/*
** Required header files.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <time.h>
#include <sys/stat.h>
#include <limits.h>
#include "ELF_Structures.h"
#include "cfe_tbl_filedef.h"
#include "elf2cfetbl_version.h"
#define MAX_SECTION_HDR_NAME_LEN (128)
#define TBL_DEF_SYMBOL_NAME "CFE_TBL_FileDef"
#define SUCCESS (0)
#define FAILED (1)
/* macro to construct 32 bit value from 4 chars */
#define U32FROM4CHARS(_C1, _C2, _C3, _C4) \
((uint32)(_C1) << 24 | (uint32)(_C2) << 16 | (uint32)(_C3) << 8 | (uint32)(_C4))
typedef struct
{
int32 Value;
char String[50];
} ElfStrMap;
/**
* Function Prototypes
*/
int32 ProcessCmdLineOptions(int argc, char *argv[]);
int32 GetSrcFilename(void);
int32 GetDstFilename(void);
int32 OpenSrcFile(void);
int32 OpenDstFile(void);
int32 GetElfHeader(void);
void SwapElfHeader(void);
int32 GetSectionHeader(int32 SectionIndex, union Elf_Shdr *SectionHeader);
void SwapSectionHeader(union Elf_Shdr *SectionHeader);
int32 GetSymbol(int32 SymbolIndex, union Elf_Sym *Symbol);
void SwapSymbol(union Elf_Sym *Symbol);
int32 GetStringFromMap(char *Result, ElfStrMap *Map, int32 Key);
void SwapUInt16(uint16 *ValueToSwap);
void SwapUInt32(uint32 *ValueToSwap);
void SwapUInt64(uint64 *ValueToSwap);
int32 AllocateSectionHeaders(void);
void DeallocateSectionHeaders(void);
int32 AllocateSymbols(void);
void DeallocateSymbols(void);
void FreeMemoryAllocations(void);
int32 GetTblDefInfo(void);
int32 OutputDataToTargetFile(void);
void OutputVersionInfo(void);
void OutputHelpInfo(void);
int32 LocateAndReadUserObject(void);
void PrintSymbol32(union Elf_Sym *Symbol);
void PrintSymbol64(union Elf_Sym *Symbol);
void PrintSectionHeader32(union Elf_Shdr *SectionHeader);
void PrintSectionHeader64(union Elf_Shdr *SectionHeader);
void PrintElfHeader32(union Elf_Ehdr ElfHeaderLcl);
void PrintElfHeader64(union Elf_Ehdr ElfHeaderLcl);
/**
* Global Variables
*/
char SrcFilename[PATH_MAX] = {""};
char DstFilename[PATH_MAX] = {""};
char TableName[38] = {""};
char Description[32] = {""};
char LineOfText[300] = {""};
bool Verbose = false;
bool ReportVersion = false;
bool OutputHelp = false;
bool ByteSwapRequired = false;
bool ScIDSpecified = false;
bool ProcIDSpecified = false;
bool AppIDSpecified = false;
bool ScEpochSpecified = false;
bool FileEpochSpecified = false;
bool TableNameOverride = false;
bool DescriptionOverride = false;
bool ThisMachineIsLittleEndian = true;
bool TargetMachineIsLittleEndian = true;
bool EnableTimeTagInHeader = false;
bool TargetWordsizeIs32Bit = true;
bool TableDataIsAllZeros = false;
FILE *SrcFileDesc = NULL;
FILE *DstFileDesc = NULL;
CFE_FS_Header_t FileHeader;
CFE_TBL_File_Hdr_t TableHeader;
union Elf_Ehdr ElfHeader;
union Elf_Shdr **SectionHeaderPtrs = NULL;
union Elf_Shdr SectionHeaderStringTable = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
int64 SectionHeaderStringTableDataOffset = 0;
char ** SectionNamePtrs = NULL;
struct stat SrcFileStats;
uint64_t StringTableDataOffset = 0;
int32 SymbolTableDataOffset = 0;
uint64_t NumSymbols = 0;
uint64_t SymbolTableEntrySize = 0;
union Elf_Sym ** SymbolPtrs = NULL;
char ** SymbolNames;
int32 TblDefSymbolIndex = -1;
CFE_TBL_FileDef_t TblFileDef;
int32 UserObjSymbolIndex = -1;
uint32 SpacecraftID = 0;
uint32 ProcessorID = 0;
uint32 ApplicationID = 0;
time_t EpochTime = 0;
typedef struct
{
uint32 Year;
uint32 Month;
uint32 Day;
uint32 Hour;
uint32 Minute;
uint32 Second;
} SpecifiedEpoch_t;
SpecifiedEpoch_t ScEpoch = {1970, 1, 1, 0, 0, 0};
SpecifiedEpoch_t FileEpoch = {1970, 1, 1, 0, 0, 0};
time_t EpochDelta;
time_t SrcFileTimeInScEpoch;
/**
* ELF Characteristic Maps
*/
ElfStrMap e_type_Map[] = {
{ET_NONE, "ET_NONE (0)"}, {ET_REL, "ET_REL (1)"}, {ET_EXEC, "ET_EXEC (2)"},
{ET_DYN, "ET_DYN (3)"}, {ET_CORE, "ET_CORE (4)"}, {0, "* Unknown Elf File Type (%d) *"},
};
ElfStrMap e_machine_Map[] = {
{EM_NONE, "EM_NONE ( 0)"},
{EM_M32, "EM_M32 ( 1)"},
{EM_SPARC, "EM_SPARC ( 2)"},
{EM_386, "EM_386 ( 3)"},
{EM_68K, "EM_68K ( 4)"},
{EM_88K, "EM_88K ( 5)"},
{EM_860, "EM_860 ( 7)"},
{EM_MIPS, "EM_MIPS ( 8)"},
{EM_S370, "EM_S370 ( 9)"},
{EM_MIPS_RS3_LE, "EM_MIPS_RS3_LE (10)"},
{EM_PARISC, "EM_PARISC (15)"},
{EM_VPP500, "EM_VPP500 (17)"},
{EM_SPARC32PLUS, "EM_SPARC32PLUS (18)"},
{EM_960, "EM_960 (19)"},
{EM_PPC, "EM_PPC (20)"},
{EM_PPC64, "EM_PPC64 (21)"},
{EM_S390, "EM_S390 (22)"},
{EM_SPU, "EM_SPU (23)"},
{EM_V800, "EM_V800 (36)"},
{EM_FR20, "EM_FR20 (37)"},
{EM_RH32, "EM_RH32 (38)"},
{EM_RCE, "EM_RCE (39)"},
{EM_ARM, "EM_ARM (40)"},
{EM_ALPHA, "EM_ALPHA (41)"},
{EM_SH, "EM_SH (42)"},
{EM_SPARCV9, "EM_SPARCV9 (43)"},
{EM_TRICORE, "EM_TRICORE (44)"},
{EM_ARC, "EM_ARC (45)"},
{EM_H8_300, "EM_H8_300 (46)"},
{EM_H8_300H, "EM_H8_300H (47)"},
{EM_H8S, "EM_H8S (48)"},
{EM_H8_500, "EM_H8_500 (49)"},
{EM_IA_64, "EM_IA_64 (50)"},
{EM_MIPS_X, "EM_MIPS_X (51)"},
{EM_COLDFIRE, "EM_COLDFIRE (52)"},
{EM_68HC12, "EM_68HC12 (53)"},
{EM_MMA, "EM_MMA (54)"},
{EM_PCP, "EM_PCP (55)"},
{EM_NCPU, "EM_NCPU (56)"},
{EM_NDR1, "EM_NDR1 (57)"},
{EM_STARCORE, "EM_STARCORE (58)"},
{EM_ME16, "EM_ME16 (59)"},
{EM_ST100, "EM_ST100 (60)"},
{EM_TINYJ, "EM_TINYJ (61)"},
{EM_X86_64, "EM_X86_64 (62)"},
{EM_PDSP, "EM_PDSP (63)"},
{EM_PDP10, "EM_PDP10 (64)"},
{EM_PDP11, "EM_PDP11 (65)"},
{EM_FX66, "EM_FX66 (66)"},
{EM_ST9PLUS, "EM_ST9PLUS (67)"},
{EM_ST7, "EM_ST7 (68)"},
{EM_68HC16, "EM_68HC16 (69)"},
{EM_68HC11, "EM_68HC11 (70)"},
{EM_68HC08, "EM_68HC08 (71)"},
{EM_68HC05, "EM_68HC05 (72)"},
{EM_SVX, "EM_SVX (73)"},
{EM_ST19, "EM_ST19 (74)"},
{EM_VAX, "EM_VAX (75)"},
{EM_CRIS, "EM_CRIS (76)"},
{EM_JAVELIN, "EM_JAVELIN (77)"},
{EM_FIREPATH, "EM_FIREPATH (78)"},
{EM_ZSP, "EM_ZSP (79)"},
{EM_MMIX, "EM_MMIX (80)"},
{EM_HUANY, "EM_HUANY (81)"},
{EM_PRISM, "EM_PRISM (82)"},
{EM_AVR, "EM_AVR (83)"},
{EM_FR30, "EM_FR30 (84)"},
{EM_D10V, "EM_D10V (85)"},
{EM_D30V, "EM_D30V (86)"},
{EM_V850, "EM_V850 (87)"},
{EM_M32R, "EM_M32R (88)"},
{EM_MN10300, "EM_MN10300 (89)"},
{EM_MN10200, "EM_MN10200 (90)"},
{EM_PJ, "EM_PJ (91)"},
{EM_OPENRISC, "EM_OPENRISC (92)"},
{EM_ARC_COMPACT, "EM_ARC_COMPACT (93)"},
{EM_XTENSA, "EM_XTENSA (94)"},
{EM_VIDEOCORE, "EM_VIDEOCORE (95)"},
{EM_TMM_GPP, "EM_TMM_GPP (96)"},
{EM_NS32K, "EM_NS32K (97)"},
{EM_TPC, "EM_TPC (98)"},
{EM_SNP1K, "EM_SNP1K (99)"},
{EM_ST200, "EM_ST200 (100)"},
{EM_IP2K, "EM_IP2K (101)"},
{EM_MAX, "EM_MAX (102)"},
{EM_CR, "EM_CR (103)"},
{EM_F2MC16, "EM_F2MC16 (104)"},
{EM_MSP430, "EM_MSP430 (105)"},
{EM_BLACKFIN, "EM_BLACKFIN (106)"},
{EM_SE_C33, "EM_SE_C33 (107)"},
{EM_SEP, "EM_SEP (108)"},
{EM_ARCA, "EM_ARCA (109)"},
{EM_UNICORE, "EM_UNICORE (110)"},
{EM_EXCESS, "EM_EXCESS (111)"},
{EM_DXP, "EM_DXP (112)"},
{EM_ALTERA_NIOS2, "EM_ALTERA_NIOS2 (113)"},
{EM_CRX, "EM_CRX (114)"},
{EM_XGATE, "EM_XGATE (115)"},
{EM_C166, "EM_C166 (116)"},
{EM_M16C, "EM_M16C (117)"},
{EM_DSPIC30F, "EM_DSPIC30F (118)"},
{EM_CE, "EM_CE (119)"},
{EM_M32C, "EM_M32C (120)"},
{EM_TSK3000, "EM_TSK3000 (131)"},
{EM_RS08, "EM_RS08 (132)"},
{EM_SHARC, "EM_SHARC (133)"},
{EM_ECOG2, "EM_ECOG2 (134)"},
{EM_SCORE7, "EM_SCORE7 (135)"},
{EM_DSP24, "EM_DSP24 (136)"},
{EM_VIDEOCORE3, "EM_VIDEOCORE3 (137)"},
{EM_LATTICEMICO32, "EM_LATTICEMICO32(138)"},
{EM_SE_C17, "EM_SE_C17 (139)"},
{EM_TI_C6000, "EM_TI_C6000 (140)"},
{EM_TI_C2000, "EM_TI_C2000 (141)"},
{EM_TI_C5500, "EM_TI_C5500 (142)"},
{EM_TI_ARP32, "EM_TI_ARP32 (143)"},
{EM_TI_PRU, "EM_TI_PRU (144)"},
{EM_MMDSP_PLUS, "EM_MMDSP_PLUS (160)"},
{EM_CYPRESS_M8C, "EM_CYPRESS_M8C (161)"},
{EM_R32C, "EM_R32C (162)"},
{EM_TRIMEDIA, "EM_TRIMEDIA (163)"},
{EM_QDSP6, "EM_QDSP6 (164)"},
{EM_8051, "EM_8051 (165)"},
{EM_STXP7X, "EM_STXP7X (166)"},
{EM_NDS32, "EM_NDS32 (167)"},
{EM_ECOG1, "EM_ECOG1 (168)"},
{EM_ECOG1X, "EM_ECOG1X (168)"},
{EM_MAXQ30, "EM_MAXQ30 (169)"},
{EM_XIMO16, "EM_XIMO16 (170)"},
{EM_MANIK, "EM_MANIK (171)"},
{EM_RX, "EM_RX (173)"},
{EM_METAG, "EM_METAG (174)"},
{EM_MCST_ELBRUS, "EM_MCST_ELBRUS (175)"},
{EM_ECOG16, "EM_ECOG16 (176)"},
{EM_CR16, "EM_CR16 (177)"},
{EM_ETPU, "EM_ETPU (178)"},
{EM_SLE9X, "EM_SLE9X (179)"},
{EM_L10M, "EM_L10M (180)"},
{EM_K10M, "EM_K10M (181)"},
{EM_AARCH64, "EM_AARCH64 (183)"},
{EM_AVR32, "EM_AVR32 (185)"},
{EM_STM8, "EM_STM8 (186)"},
{EM_TILE64, "EM_TILE64 (187)"},
{EM_TILEPRO, "EM_TILEPRO (188)"},
{EM_MICROBLAZE, "EM_MICROBLAZE (189)"},
{EM_CUDA, "EM_CUDA (190)"},
{EM_TILEGX, "EM_TILEGX (191)"},
{EM_CLOUDSHIELD, "EM_CLOUDSHIELD (192)"},
{EM_COREA_1ST, "EM_COREA_1ST (193)"},
{EM_COREA_2ND, "EM_COREA_2ND (194)"},
{EM_ARC_COMPACT2, "EM_ARC_COMPACT2 (195)"},
{EM_OPEN8, "EM_OPEN8 (196)"},
{EM_RL78, "EM_RL78 (197)"},
{EM_VIDEOCORE5, "EM_VIDEOCORE5 (198)"},
{EM_78KOR, "EM_78KOR (199)"},
{EM_56800EX, "EM_56800EX (200)"},
{EM_BA1, "EM_BA1 (201)"},
{EM_BA2, "EM_BA2 (202)"},
{EM_XCORE, "EM_XCORE (203)"},
{EM_MCHP_PIC, "EM_MCHP_PIC (204)"},
{EM_INTEL205, "EM_INTEL205 (205)"},
{EM_INTEL206, "EM_INTEL206 (206)"},
{EM_INTEL207, "EM_INTEL207 (207)"},
{EM_INTEL208, "EM_INTEL208 (208)"},
{EM_INTEL209, "EM_INTEL209 (209)"},
{EM_KM32, "EM_KM32 (210)"},
{EM_KMX32, "EM_KMX32 (211)"},
{EM_KMX16, "EM_KMX16 (212)"},
{EM_KMX8, "EM_KMX8 (213)"},
{EM_KVARC, "EM_KVARC (214)"},
{EM_CDP, "EM_CDP (215)"},
{EM_COGE, "EM_COGE (216)"},
{EM_COOL, "EM_COOL (217)"},
{EM_NORC, "EM_NORC (218)"},
{EM_CSR_KALIMBA, "EM_CSR_KALIMBA (219)"},
{EM_Z80, "EM_Z80 (220)"},
{EM_VISIUM, "EM_VISIUM (221)"},
{EM_FT32, "EM_FT32 (222)"},
{EM_MOXIE, "EM_MOXIE (223)"},
{EM_AMDGPU, "EM_AMDGPU (224)"},
{EM_RISCV, "EM_RISCV (243)"},
{0, "* Unknown Machine Type (%d) *"},
};
// Elf Header helper functions
uint8_t get_e_ident(const union Elf_Ehdr *ElfHeaderLcl, int index)
{
if (TargetWordsizeIs32Bit)
{
return ElfHeaderLcl->Ehdr32.e_ident[index];
}
else
{
return ElfHeaderLcl->Ehdr64.e_ident[index];
}
}
uint16_t get_e_type(const union Elf_Ehdr *ElfHeaderLcl)
{
if (TargetWordsizeIs32Bit)
{
return ElfHeaderLcl->Ehdr32.e_type;
}
else
{
return ElfHeaderLcl->Ehdr64.e_type;
}
}
uint16_t get_e_machine(const union Elf_Ehdr *ElfHeaderLcl)
{
if (TargetWordsizeIs32Bit)
{
return ElfHeaderLcl->Ehdr32.e_machine;
}
else
{
return ElfHeaderLcl->Ehdr64.e_machine;
}
}
uint32_t get_e_version(const union Elf_Ehdr *ElfHeaderLcl)
{
if (TargetWordsizeIs32Bit)
{
return ElfHeaderLcl->Ehdr32.e_version;
}
else
{
return ElfHeaderLcl->Ehdr64.e_version;
}
}
uint16_t get_e_shstrndx(const union Elf_Ehdr *ElfHeaderLcl)
{
if (TargetWordsizeIs32Bit)
{
return ElfHeaderLcl->Ehdr32.e_shstrndx;
}
else
{
return ElfHeaderLcl->Ehdr64.e_shstrndx;
}
}
uint16_t get_e_shnum(const union Elf_Ehdr *ElfHeaderLcl)
{
if (TargetWordsizeIs32Bit)
{
return ElfHeaderLcl->Ehdr32.e_shnum;
}
else
{
return ElfHeaderLcl->Ehdr64.e_shnum;
}
}
// Elf Section Header helper functions
uint32_t get_sh_name(const union Elf_Shdr *SectionHeader)
{
if (TargetWordsizeIs32Bit)
{
return SectionHeader->Shdr32.sh_name;
}
else
{
return SectionHeader->Shdr64.sh_name;
}
}
uint32_t get_sh_type(const union Elf_Shdr *SectionHeader)
{
if (TargetWordsizeIs32Bit)
{
return SectionHeader->Shdr32.sh_type;
}
else
{
return SectionHeader->Shdr64.sh_type;
}
}
void print_sh_flags(const union Elf_Shdr *SectionHeader)
{
char VerboseStr[60];
sprintf(VerboseStr, "/");
if (TargetWordsizeIs32Bit)
{
if ((SectionHeader->Shdr32.sh_flags & SHF_WRITE) == SHF_WRITE)
{
sprintf(VerboseStr, "SHF_WRITE/");
}
if ((SectionHeader->Shdr32.sh_flags & SHF_ALLOC) == SHF_ALLOC)
{
strcat(VerboseStr, "SHF_ALLOC/");
}
if ((SectionHeader->Shdr32.sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR)
{
strcat(VerboseStr, "SHF_EXECINSTR/");
}
printf(" sh_flags = %s\n", VerboseStr);
}
else
{
if ((SectionHeader->Shdr64.sh_flags & SHF_WRITE) == SHF_WRITE)
{
sprintf(VerboseStr, "SHF_WRITE/");
}
if ((SectionHeader->Shdr64.sh_flags & SHF_ALLOC) == SHF_ALLOC)
{
strcat(VerboseStr, "SHF_ALLOC/");
}
if ((SectionHeader->Shdr64.sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR)
{
strcat(VerboseStr, "SHF_EXECINSTR/");
}
printf(" sh_flags = %s\n", VerboseStr);
}
}
uint64_t get_sh_offset(const union Elf_Shdr *SectionHeader)
{
if (TargetWordsizeIs32Bit)
{
return SectionHeader->Shdr32.sh_offset;
}
else
{
return SectionHeader->Shdr64.sh_offset;
}
}
uint64_t get_sh_size(const union Elf_Shdr *SectionHeader)
{
if (TargetWordsizeIs32Bit)
{
return SectionHeader->Shdr32.sh_size;
}
else
{
return SectionHeader->Shdr64.sh_size;
}
}
uint64_t get_sh_entsize(const union Elf_Shdr *SectionHeader)
{
if (TargetWordsizeIs32Bit)
{
return SectionHeader->Shdr32.sh_entsize;
}
else
{
return SectionHeader->Shdr64.sh_entsize;
}
}
// Elf_Sym helper functions
uint32_t get_st_name(const union Elf_Sym *Symbol)
{
if (TargetWordsizeIs32Bit)
{
return Symbol->Sym32.st_name;
}
else
{
return Symbol->Sym64.st_name;
}
}
uint64_t get_st_value(const union Elf_Sym *Symbol)
{
if (TargetWordsizeIs32Bit)
{
return Symbol->Sym32.st_value;
}
else
{
return Symbol->Sym64.st_value;
}
}
uint64_t get_st_size(const union Elf_Sym *Symbol)
{
if (TargetWordsizeIs32Bit)
{
return Symbol->Sym32.st_size;
}
else
{
return Symbol->Sym64.st_size;
}
}
void set_st_size(union Elf_Sym *Symbol, uint64_t new_value)
{
if (TargetWordsizeIs32Bit)
{
Symbol->Sym32.st_size = (uint32_t)new_value;
if (Symbol->Sym32.st_size != new_value)
{
printf("ERROR: Sym32.st_size can not hold %lu\n", (long unsigned int)new_value);
}
}
else
{
Symbol->Sym64.st_size = new_value;
}
}
uint16_t get_st_shndx(const union Elf_Sym *Symbol)
{
if (TargetWordsizeIs32Bit)
{
return Symbol->Sym32.st_shndx;
}
else
{
return Symbol->Sym64.st_shndx;
}
}
/**
*
*/
int main(int argc, char *argv[])
{
int32 Status = SUCCESS;
int32 i = 0;
Status = ProcessCmdLineOptions(argc, argv);
if (Status != SUCCESS)
return Status;
if (ReportVersion)
OutputVersionInfo();
Status = GetSrcFilename();
if (OutputHelp)
OutputHelpInfo();
if (Status != SUCCESS)
return Status;
Status = OpenSrcFile();
if (Status != SUCCESS)
return Status;
Status = GetElfHeader();
if (Status != SUCCESS)
return Status;
/* Get the string section header first */
Status = GetSectionHeader(get_e_shstrndx(&ElfHeader), &SectionHeaderStringTable);
if (Status != SUCCESS)
return Status;
if (TargetWordsizeIs32Bit)
{
SectionHeaderStringTableDataOffset = SectionHeaderStringTable.Shdr32.sh_offset;
}
else
{
SectionHeaderStringTableDataOffset = SectionHeaderStringTable.Shdr64.sh_offset;
}
/* Allocate memory for all of the ELF object file section headers */
Status = AllocateSectionHeaders();
if (Status != SUCCESS)
{
FreeMemoryAllocations();
return Status;
}
/* Read in each section header from input file */
for (i = 0; i < get_e_shnum(&ElfHeader); i++)
{
Status = GetSectionHeader(i, SectionHeaderPtrs[i]);
if (Status != SUCCESS)
{
FreeMemoryAllocations();
return Status;
}
}
if (StringTableDataOffset == 0)
{
printf("Error! Unable to locate ELF string table for symbol names\n");
return EXIT_FAILURE;
}
/* Allocate memory for all of the symbol table entries */
Status = AllocateSymbols();
if (Status != SUCCESS)
{
FreeMemoryAllocations();
return Status;
}
/* Read in each symbol table entry */
for (i = 0; i < NumSymbols; i++)
{
Status = GetSymbol(i, SymbolPtrs[i]);
if (Status != SUCCESS)
{
FreeMemoryAllocations();
return Status;
}
}
if (TblDefSymbolIndex == -1)
{
printf("Error! Unable to locate '%s' object in '%s'.\n", TBL_DEF_SYMBOL_NAME, SrcFilename);
FreeMemoryAllocations();
return EXIT_FAILURE;
}
/* Read in the definition of the table file */
Status = GetTblDefInfo();
if (Status != SUCCESS)
{
FreeMemoryAllocations();
return Status;
}
Status = GetDstFilename();
if (Status != SUCCESS)
return Status;
Status = OpenDstFile();
if (Status != SUCCESS)
return Status;
Status = LocateAndReadUserObject();
if (Status != SUCCESS)
{
FreeMemoryAllocations();
return Status;
}
Status = OutputDataToTargetFile();
FreeMemoryAllocations();
return SUCCESS;
}
/**
*
*/
int32 AllocateSectionHeaders(void)
{
int32 Status = SUCCESS;
int32 i = 0;
if (get_e_shnum(&ElfHeader) == 0)
{
printf("Error! Failed to locate any Section Headers in '%s'!\n", SrcFilename);
Status = FAILED;
}
else
{
SectionHeaderPtrs = (union Elf_Shdr **)malloc(sizeof(union Elf_Shdr *) * get_e_shnum(&ElfHeader));
if (SectionHeaderPtrs == NULL)
{
printf("Error! Insufficient memory for number of Sections in '%s'!\n", SrcFilename);
Status = FAILED;
}
SectionNamePtrs = (char **)malloc(sizeof(char *) * get_e_shnum(&ElfHeader));
if (SectionNamePtrs == NULL)
{
printf("Error! Insufficient memory for number of Sections in '%s'!\n", SrcFilename);
Status = FAILED;
}
if (Status == SUCCESS)
{
/* Initialize all of the pointers to NULL */
for (i = 0; i < get_e_shnum(&ElfHeader); i++)
{
SectionHeaderPtrs[i] = NULL;
SectionNamePtrs[i] = NULL;
}
/* Allocate memory for each header */
for (i = 0; i < get_e_shnum(&ElfHeader); i++)
{
SectionHeaderPtrs[i] = (union Elf_Shdr *)malloc(sizeof(union Elf_Shdr));
if (SectionHeaderPtrs[i] == NULL)
{
printf("Error! Insufficient memory to store Section Headers\n");
Status = FAILED;
}
SectionNamePtrs[i] = (char *)malloc(MAX_SECTION_HDR_NAME_LEN);
if (SectionNamePtrs[i] == NULL)
{
printf("Error! Insufficient memory to store Section Names\n");
Status = FAILED;
}
}
}
}
return Status;
}
/**
*
*/
void DeallocateSectionHeaders(void)
{
int32 i = 0;
if (SectionHeaderPtrs != NULL)
{
while ((i < get_e_shnum(&ElfHeader)) && (SectionHeaderPtrs[i] != NULL))
{
free(SectionHeaderPtrs[i]);
i++;
}
free(SectionHeaderPtrs);
}
i = 0;
if (SectionNamePtrs != NULL)
{
while ((i < get_e_shnum(&ElfHeader)) && (SectionNamePtrs[i] != NULL))
{
free(SectionNamePtrs[i]);
i++;
}
free(SectionNamePtrs);
}
}
/**
*
*/
int32 AllocateSymbols(void)
{
int32 Status = SUCCESS;
int32 i = 0;
if (NumSymbols == 0)
{
printf("Error! Failed to locate any Symbols in '%s'!\n", SrcFilename);
Status = FAILED;
}
else
{
SymbolPtrs = malloc(sizeof(union Elf_Sym *) * NumSymbols);
if (SymbolPtrs == NULL)
{
printf("Error! Insufficient memory for number of Symbols in '%s'!\n", SrcFilename);
Status = FAILED;
}
SymbolNames = malloc(sizeof(char *) * NumSymbols);
if (SymbolNames == NULL)
{
printf("Error! Insufficient memory for number of Symbols in '%s'!\n", SrcFilename);
Status = FAILED;
}
for (i = 0; i < NumSymbols; i++)
{
SymbolPtrs[i] = NULL;
SymbolNames[i] = NULL;
}
if (Status == SUCCESS)
{
/* Allocate memory for each symbol */
for (i = 0; i < NumSymbols; i++)
{
SymbolPtrs[i] = (union Elf_Sym *)malloc(sizeof(union Elf_Sym));
if (SymbolPtrs[i] == NULL)
{
printf("Error! Insufficient memory to store Symbol Headers\n");
Status = FAILED;
}
}
}
}
return Status;
}
/**
*
*/
void DeallocateSymbols(void)
{
int32 i = 0;
if (SymbolPtrs != NULL)
{
while ((i < NumSymbols) && (SymbolPtrs[i] != NULL))
{
free(SymbolPtrs[i]);
if (SymbolNames[i] != NULL)
{
free(SymbolNames[i]);
}
i++;
}
free(SymbolPtrs);
}
}
/**
*
*/
void FreeMemoryAllocations(void)
{
DeallocateSymbols();
DeallocateSectionHeaders();
if (SrcFileDesc != NULL)
{
fclose(SrcFileDesc);
}
if (DstFileDesc != NULL)
{
fclose(DstFileDesc);
}
}
/**
*
*/
int32 ProcessCmdLineOptions(int ArgumentCount, char *Arguments[])
{
int32 Status = SUCCESS;
bool InputFileSpecified = false;
bool OutputFileSpecified = false;
int i = 1;
char * EndPtr;
uint32 MaxDay;
struct tm FileEpochTm;
struct tm ScEpochTm;
time_t FileEpochInSecs;
time_t ScEpochInSecs;
while ((i < ArgumentCount) && (Status == SUCCESS))
{
if ((Arguments[i][0] == '-') && (Arguments[i][1] == 't'))
{
/* Extract the Table Name Override */
strncpy(TableName, &Arguments[i][2], sizeof(TableName) - 1);
TableName[sizeof(TableName) - 1] = 0;
TableNameOverride = true;
}
else if ((Arguments[i][0] == '-') && (Arguments[i][1] == 'd'))
{
/* Extract the Description Override */
strncpy(Description, &Arguments[i][2], sizeof(Description) - 1);
Description[sizeof(Description) - 1] = 0;
DescriptionOverride = true;
}
else if ((Arguments[i][0] == '-') && (Arguments[i][1] == 's'))
{
SpacecraftID = strtoul(&Arguments[i][2], &EndPtr, 0);
if (EndPtr != &Arguments[i][2])
{
ScIDSpecified = true;
}
else
{
printf("Error!, Spacecraft ID of '%s' cannot be interpreted as an integer.\n", &Arguments[i][2]);
Status = false;
}
}
else if ((Arguments[i][0] == '-') && (Arguments[i][1] == 'S'))
{
if (strlen(&Arguments[i][2]) == 4)
{
SpacecraftID = U32FROM4CHARS(Arguments[i][2], Arguments[i][3], Arguments[i][4], Arguments[i][5]);
ScIDSpecified = true;
}
else
{
printf("Error!, Spacecraft ID of '%s' does not have exactly 4 characters.\n", &Arguments[i][2]);
Status = false;
}
}
else if ((Arguments[i][0] == '-') && (Arguments[i][1] == 'a'))
{
ApplicationID = strtoul(&Arguments[i][2], &EndPtr, 0);
if (EndPtr != &Arguments[i][2])
{
AppIDSpecified = true;
}
else
{
printf("Error!, Application ID of '%s' cannot be interpreted as an integer.\n", &Arguments[i][2]);
Status = false;
}
}
else if ((Arguments[i][0] == '-') && (Arguments[i][1] == 'p'))
{
ProcIDSpecified = true;
ProcessorID = strtoul(&Arguments[i][2], &EndPtr, 0);
if (EndPtr != &Arguments[i][2])
{