1 /*========================== begin_copyright_notice ============================
2 
3 Copyright (C) 2017-2021 Intel Corporation
4 
5 SPDX-License-Identifier: MIT
6 
7 ============================= end_copyright_notice ===========================*/
8 
9 #pragma once
10 
11 #include "CLElfTypes.h"
12 #include <queue>
13 #include <string>
14 
15 #if defined(_WIN32) && (__KLOCWORK__ == 0)
16   #define ELF_CALL __stdcall
17 #else
18   #define ELF_CALL
19 #endif
20 
21 using namespace std;
22 
23 namespace CLElfLib
24 {
25 static const unsigned int g_scElfHeaderAlignment    = 16;   // allocation alignment restriction
26 static const unsigned int g_scInitialElfSize        = 2048; // initial elf size (in bytes)
27 static const unsigned int g_scInitNumSectionHeaders = 8;
28 
29 struct SSectionNode
30 {
31     E_SH_TYPE    Type;
32     unsigned int Flags;
33     string Name;
34     char* pData;
35     unsigned int DataSize;
36 
SSectionNodeSSectionNode37     SSectionNode()
38     {
39         Type     = SH_TYPE_NULL;
40         Flags    = 0;
41         pData    = NULL;
42         DataSize = 0;
43     }
44 
~SSectionNodeSSectionNode45     ~SSectionNode()
46     {
47     }
48 };
49 
50 /******************************************************************************\
51 
52  Class:         CElfWriter
53 
54  Description:   Class to provide simpler interaction with the ELF standard
55                 binary object.  SElf64Header defines the ELF header type and
56                 SElf64SectionHeader defines the section header type.
57 
58 \******************************************************************************/
59 class CElfWriter
60 {
61 public:
62     static CElfWriter* ELF_CALL Create(
63         E_EH_TYPE type,
64         E_EH_MACHINE machine,
65         Elf64_Xword flags );
66 
67     static void ELF_CALL Delete( CElfWriter* &pElfWriter );
68 
69     E_RETVAL ELF_CALL AddSection(
70         SSectionNode* pSectionNode );
71 
72     E_RETVAL ELF_CALL ResolveBinary(
73         char* const pBinary,
74         size_t& dataSize );
75 
76     E_RETVAL ELF_CALL Initialize();
77     E_RETVAL ELF_CALL PatchElfHeader( char* const pBinary );
78 
79 protected:
80     ELF_CALL CElfWriter(
81         E_EH_TYPE type,
82         E_EH_MACHINE machine,
83         Elf64_Xword flags );
84 
85     ELF_CALL ~CElfWriter();
86 
87     E_EH_TYPE m_type;
88     E_EH_MACHINE m_machine;
89     Elf64_Xword m_flags;
90 
91     std::queue<SSectionNode*> m_nodeQueue;
92 
93     unsigned int m_dataSize;
94     unsigned int m_numSections;
95     size_t       m_stringTableSize;
96     size_t       m_totalBinarySize;
97 };
98 
99 /******************************************************************************\
100 
101  Class:         CElfWriterDeleter
102 
103  Description:   Dummy class to be used with unique_ptr to call Delete on
104                 CElfReader when going out of scope.
105 
106 \******************************************************************************/
107 class CElfWriterDeleter
108 {
109 public:
operator()110   void operator()(CElfWriter* ptr) const
111   {
112     CElfWriter::Delete(ptr);
113   }
114 };
115 
116 } // namespace ELFLib
117