1 |
68 |
Agner |
/**************************** converters.h ********************************
|
2 |
|
|
* Author: Agner Fog
|
3 |
|
|
* Date created: 2017-04-17
|
4 |
|
|
* Last modified: 2018-03-30
|
5 |
|
|
* Version: 1.10
|
6 |
|
|
* Project: Binary tools for ForwardCom instruction set
|
7 |
|
|
* Module: converters.h
|
8 |
|
|
* Description:
|
9 |
|
|
* Header file for file conversion classes.
|
10 |
|
|
*
|
11 |
|
|
* Copyright 2006-2020 GNU General Public License http://www.gnu.org/licenses
|
12 |
|
|
*****************************************************************************/
|
13 |
|
|
|
14 |
|
|
/******************************* Classes ********************************
|
15 |
|
|
|
16 |
|
|
This header file declares various classes for interpreting and converting
|
17 |
|
|
different types of object files. These classes are all derived from the
|
18 |
|
|
container class CFileBuffer, declared in containers.h.
|
19 |
|
|
|
20 |
|
|
See containers.h for an explanation of the container classes and the
|
21 |
|
|
operators >> and << which can transfer a data buffer from an object of one
|
22 |
|
|
class to an object of another class.
|
23 |
|
|
|
24 |
|
|
*****************************************************************************/
|
25 |
|
|
|
26 |
|
|
#pragma once
|
27 |
|
|
|
28 |
|
|
// Buffer for symbol names during assembly, linking, and library operations
|
29 |
|
|
// symbolNameBuffer is made global in order to make it accessible to bool operator < (ElfFWC_Sym2 const &, ElfFWC_Sym2 const &)
|
30 |
|
|
// It is defined in assem1.cpp
|
31 |
|
|
extern CTextFileBuffer symbolNameBuffer; // Buffer for symbol names
|
32 |
|
|
|
33 |
|
|
|
34 |
|
|
// Structure for string index entry in library
|
35 |
|
|
struct SSymbolEntry {
|
36 |
|
|
uint32_t name; // name as offset into symbolNameBuffer
|
37 |
|
|
uint32_t library; // library index (1-based) or 0 if object file
|
38 |
|
|
uint32_t member; // module or library member offset
|
39 |
|
|
uint32_t sectioni; // section index within module
|
40 |
|
|
uint32_t symindex; // index into symbol table
|
41 |
|
|
uint16_t st_other; // attributes: STV_SECT_ATTR
|
42 |
|
|
uint8_t st_type; // symbol type
|
43 |
|
|
uint8_t st_bind; // symbol binding
|
44 |
|
|
uint8_t status; // 1: has no value yet. 2: has been matched. 4: unresolved
|
45 |
|
|
};
|
46 |
|
|
|
47 |
|
|
// operator for sorting string entries
|
48 |
|
|
inline bool operator < (SSymbolEntry const & a, SSymbolEntry const & b) {
|
49 |
|
|
int compare = strcmp(symbolNameBuffer.getString(a.name), symbolNameBuffer.getString(b.name));
|
50 |
|
|
if (compare) return compare < 0; // compare names
|
51 |
|
|
if ((a.st_bind | b.st_bind) & STB_IGNORE) return false; // ignore binding
|
52 |
|
|
return (a.st_bind & STB_WEAK) < (b.st_bind & STB_WEAK); // strong before weak
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
// operator for comparing string entries. Compares name only
|
56 |
|
|
inline bool operator == (SSymbolEntry const & a, SSymbolEntry const & b) {
|
57 |
|
|
return strcmp(symbolNameBuffer.getString(a.name), symbolNameBuffer.getString(b.name)) == 0;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
// Class CConverter contains the input file.
|
62 |
|
|
// It redirects the task specified on the command line to some other converter class
|
63 |
|
|
class CConverter : public CFileBuffer {
|
64 |
|
|
public:
|
65 |
|
|
CConverter(); // Constructor
|
66 |
|
|
void go(); // Main action
|
67 |
|
|
protected:
|
68 |
|
|
void readInputFile(); // read input file
|
69 |
|
|
void dumpELF(); // Dump x86 ELF file
|
70 |
|
|
void disassemble(); // Disassemble ForwardCom ELF file
|
71 |
|
|
void assemble(); // Assemble ForwardCom assembly file
|
72 |
|
|
void link(); // Link object files into executable file
|
73 |
|
|
void emulate(); // emulate and run executable file
|
74 |
|
|
void lib(); // Build or modify function libraries
|
75 |
|
|
};
|
76 |
|
|
|
77 |
|
|
// Class for interpreting and dumping ELF files
|
78 |
|
|
class CELF : public CFileBuffer {
|
79 |
|
|
public:
|
80 |
|
|
CELF(); // Default constructor
|
81 |
|
|
void parseFile(); // Parse file buffer
|
82 |
|
|
void dump(int options); // Dump file
|
83 |
|
|
void makeLinkMap(FILE * stream); // Write a link map
|
84 |
|
|
void listSymbols(CMemoryBuffer * strings, CDynamicArray<SSymbolEntry> * index, uint32_t m, uint32_t l, int scope); // Make list of public and external symbols
|
85 |
|
|
ElfFwcSym * getSymbol(uint32_t symindex); // Get a symbol record
|
86 |
|
|
int split(); // Split ELF file into containers
|
87 |
|
|
int join(ElfFwcEhdr * header); // Join containers into ELF file
|
88 |
|
|
void reset(); // Reset everything
|
89 |
|
|
uint32_t addSection(ElfFwcShdr & section, CMemoryBuffer const & strings, CMemoryBuffer const & data); // Add section header and section data
|
90 |
|
|
void extendSection(ElfFwcShdr & section, CMemoryBuffer const & data); // Extend previously added section
|
91 |
|
|
void insertFiller(uint64_t numBytes); // Insert alignment fillers between sections
|
92 |
|
|
void addModuleNames(CDynamicArray<uint32_t>& moduleNames, CDynamicArray<uint32_t>& libraryNames);
|
93 |
|
|
void updateModuleNames(CDynamicArray<ElfFwcShdr> &newSectionHeaders, CMemoryBuffer &newShStrtab); // Put module names and library names into section string table for relinkable sections
|
94 |
|
|
// Add module name and library name to relinkable sections
|
95 |
|
|
void addProgHeader(ElfFwcPhdr & header); // Add program header
|
96 |
|
|
uint32_t addSymbol(ElfFwcSym & symbol, CMemoryBuffer const & strings); // Add a symbol
|
97 |
|
|
void addRelocation(ElfFwcReloc & relocation); // Add a relocation
|
98 |
|
|
void removePrivateSymbols(int debugOptions); // remove local symbols and adjust relocation records with new symbol indexes
|
99 |
|
|
CDynamicArray<ElfFwcShdr> const & getSectionHeaders() const { return sectionHeaders; } // get sectionHeaders for copying. Used by disassembly listing
|
100 |
|
|
CDynamicArray<ElfFwcSym> const & getSymbols() const { return symbols; } // get symbols buffer for copying
|
101 |
|
|
CDynamicArray<ElfFwcReloc> const & getRelocations() const { return relocations; } // get relocations buffer for copying
|
102 |
|
|
CMemoryBuffer const & getStringBuffer() const { return stringBuffer; } // get string buffer for copying
|
103 |
|
|
CMemoryBuffer const & getDataBuffer() const { return dataBuffer; } // get data buffer for copying
|
104 |
|
|
CFileBuffer & makeHexBuffer(); // make hexadecimal code file
|
105 |
|
|
CDynamicArray<ElfFwcSym> symbols; // List of symbols
|
106 |
|
|
uint32_t moduleName; // Name of file or module as index into cmd.fileNameBuffer
|
107 |
|
|
uint32_t library; // Library index if this module is extracted from a library
|
108 |
|
|
bool relinkable; // module can be replaced by relinking
|
109 |
|
|
const char * symbolName(uint32_t index); // Get name of symbol
|
110 |
|
|
protected:
|
111 |
|
|
char * secStringTable; // Section header string table
|
112 |
|
|
uint32_t secStringTableLen; // Length of section header string table
|
113 |
|
|
uint32_t nSections; // Number of sections
|
114 |
|
|
int sectionHeaderSize; // Size of each section header
|
115 |
|
|
uint32_t symbolTableOffset; // Offset to symbol table
|
116 |
|
|
uint32_t symbolTableEntrySize; // Entry size of symbol table
|
117 |
|
|
uint32_t symbolTableEntries; // Number of symbols
|
118 |
|
|
uint32_t symbolStringTableOffset; // Offset to symbol string table
|
119 |
|
|
uint32_t symbolStringTableSize; // Size of symbol string table
|
120 |
|
|
ElfFwcEhdr fileHeader; // Copy of file header
|
121 |
|
|
CDynamicArray<ElfFwcShdr> sectionHeaders; // Copy of section headers
|
122 |
|
|
CDynamicArray<ElfFwcPhdr> programHeaders; // Copy of program headers
|
123 |
|
|
CDynamicArray<ElfFwcReloc> relocations; // List of relocations
|
124 |
|
|
CMemoryBuffer stringBuffer; // String buffer with names of symbols and sections
|
125 |
|
|
CMemoryBuffer dataBuffer; // Raw data for sections
|
126 |
|
|
CDynamicArray<uint32_t> moduleNames; // module names as index into cmd.fileNameBuffer. used by join function
|
127 |
|
|
CDynamicArray<uint32_t> libraryNames; // library names as index into cmd.stringBuffer. used by join function
|
128 |
|
|
friend class CLinker; // Give linker access to internal buffers
|
129 |
|
|
friend void operator >> (CELF & a, CELF & b); // Transfer ownership of buffer
|
130 |
|
|
friend void operator << (CELF & a, CELF & b); // Transfer ownership of buffer
|
131 |
|
|
};
|
132 |
|
|
|
133 |
|
|
// transfer ownership of data buffer and properties except what can be generated by the split function
|
134 |
|
|
inline void operator >> (CELF & a, CELF & b) {
|
135 |
|
|
static_cast<CFileBuffer&>(a) >> static_cast<CFileBuffer&>(b);
|
136 |
|
|
b.moduleName = a.moduleName;
|
137 |
|
|
b.library = a.library;
|
138 |
|
|
b.relinkable = a.relinkable;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
inline void operator << (CELF & b, CELF & a) {
|
142 |
|
|
a >> b;
|
143 |
|
|
}
|