OpenCores
URL https://opencores.org/ocsvn/thor/thor/trunk

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [software/] [AS64/] [source/] [Elf.hpp] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2014  Robert Finch, Stratford
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
// A64 - Assembler
9
//  - 64 bit CPU
10
//
11
// This source file is free software: you can redistribute it and/or modify 
12
// it under the terms of the GNU Lesser General Public License as published 
13
// by the Free Software Foundation, either version 3 of the License, or     
14
// (at your option) any later version.                                      
15
//                                                                          
16
// This source file is distributed in the hope that it will be useful,      
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
19
// GNU General Public License for more details.                             
20
//                                                                          
21
// You should have received a copy of the GNU General Public License        
22
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
23
//                                                                          
24
// ============================================================================
25
//
26
#pragma once
27
 
28
#include <string.h>
29
 
30
class clsElf64Header {
31
public:
32
    enum {
33
        ELFCLASS32 = 1,
34
        ELFCLASS64 = 2
35
    };
36
    enum {
37
        ELFDATA2LSB = 1,
38
        ELFDATA2MSB = 2
39
    };
40
 
41
    char e_ident[16];
42
    int16_t e_type;
43
    int16_t e_machine;
44
    int32_t e_version;
45
    int64_t e_entry;            // program entry point
46
    int64_t e_phoff;            // offset in file to program header
47
    int64_t e_shoff;            // offset in file to section header
48
    int32_t e_flags;            // 
49
    int16_t e_ehsize;           // size of ELF header
50
    int16_t e_phentsize;        // size of program header entry
51
    int16_t e_phnum;            // number of program header entries
52
    int16_t e_shentsize;        // size of section header entry
53
    int16_t e_shnum;            // number of section header entries
54
    int16_t e_shstrndx;         // section name string table index
55
 
56
    clsElf64Header() {
57
        memset((void*)e_ident,0,sizeof(e_ident));
58
        e_ident[0] = 127;
59
        e_ident[1] = 'E';
60
        e_ident[2] = 'L';
61
        e_ident[3] = 'F';
62
        e_type = 0;
63
        e_machine = 0;
64
        e_version = 1;
65
        e_entry = 0;
66
        e_phoff = 0;
67
        e_shoff = 0;
68
        e_flags = 0;
69
        e_ehsize = 0;
70
        e_phentsize = 0;
71
        e_phnum = 0;
72
        e_shentsize = 0;
73
        e_shnum = 0;
74
        e_shstrndx = 0;
75
    };
76
 
77
    void Write(FILE *fp) {
78
         fwrite((void *)e_ident,1,16,fp);
79
         fwrite((void *)&e_type,1,sizeof(e_type),fp);
80
         fwrite((void *)&e_machine,1,sizeof(e_machine),fp);
81
         fwrite((void *)&e_version,1,sizeof(e_version),fp);
82
         fwrite((void *)&e_entry,1,sizeof(e_entry),fp);
83
         fwrite((void *)&e_phoff,1,sizeof(e_phoff),fp);
84
         fwrite((void *)&e_shoff,1,sizeof(e_shoff),fp);
85
         fwrite((void *)&e_flags,1,sizeof(e_flags),fp);
86
         fwrite((void *)&e_ehsize,1,sizeof(e_ehsize),fp);
87
         fwrite((void *)&e_phentsize,1,sizeof(e_phentsize),fp);
88
         fwrite((void *)&e_phnum,1,sizeof(e_phnum),fp);
89
         fwrite((void *)&e_shentsize,1,sizeof(e_shentsize),fp);
90
         fwrite((void *)&e_shnum,1,sizeof(e_shnum),fp);
91
         fwrite((void *)&e_shstrndx,1,sizeof(e_shstrndx),fp);
92
    };
93
 
94
    void Read(FILE *fp) {
95
         fread((void *)e_ident,1,16,fp);
96
         fread((void *)&e_type,1,sizeof(e_type),fp);
97
         fread((void *)&e_machine,1,sizeof(e_machine),fp);
98
         fread((void *)&e_version,1,sizeof(e_version),fp);
99
         fread((void *)&e_entry,1,sizeof(e_entry),fp);
100
         fread((void *)&e_phoff,1,sizeof(e_phoff),fp);
101
         fread((void *)&e_shoff,1,sizeof(e_shoff),fp);
102
         fread((void *)&e_flags,1,sizeof(e_flags),fp);
103
         fread((void *)&e_ehsize,1,sizeof(e_ehsize),fp);
104
         fread((void *)&e_phentsize,1,sizeof(e_phentsize),fp);
105
         fread((void *)&e_phnum,1,sizeof(e_phnum),fp);
106
         fread((void *)&e_shentsize,1,sizeof(e_shentsize),fp);
107
         fread((void *)&e_shnum,1,sizeof(e_shnum),fp);
108
         fread((void *)&e_shstrndx,1,sizeof(e_shstrndx),fp);
109
    };
110
};
111
 
112
typedef struct {
113
    int64_t p_type;             // type of segment
114
    int64_t p_flags;            // segment attributes
115
    int64_t p_offset;           // offset in file
116
    int64_t p_vaddr;            // virtual address
117
    int64_t p_paddr;            // reserved
118
    int64_t p_filesz;           // size of segment in file
119
    int64_t p_memsz;            // size of segment in memory
120
    int64_t p_align;            // alignment of segment
121
} Elf64Phdr;
122
 
123
class clsElf64Shdr {
124
public:
125
    enum {
126
        SHT_PROGBITS = 1,
127
        SHT_SYMTAB = 2,
128
        SHT_STRTAB = 3,
129
        SHT_REL = 9,
130
    };
131
    enum {
132
        SHF_WRITE = 1,
133
        SHF_ALLOC = 2,
134
        SHF_EXECINSTR = 4
135
    };
136
    int32_t sh_name;
137
    int32_t sh_type;
138
    int64_t sh_flags;
139
    int64_t sh_addr;
140
    int64_t sh_offset;
141
    int64_t sh_size;
142
    int32_t sh_link;
143
    int32_t sh_info;
144
    int64_t sh_addralign;
145
    int64_t sh_entsize;
146
 
147
    void Write(FILE *fp) {
148
         fwrite((void*)&sh_name,1,sizeof(sh_name),fp);
149
         fwrite((void*)&sh_type,1,sizeof(sh_type),fp);
150
         fwrite((void*)&sh_flags,1,sizeof(sh_flags),fp);
151
         fwrite((void*)&sh_addr,1,sizeof(sh_addr),fp);
152
         fwrite((void*)&sh_offset,1,sizeof(sh_offset),fp);
153
         fwrite((void*)&sh_size,1,sizeof(sh_size),fp);
154
         fwrite((void*)&sh_link,1,sizeof(sh_link),fp);
155
         fwrite((void*)&sh_info,1,sizeof(sh_info),fp);
156
         fwrite((void*)&sh_addralign,1,sizeof(sh_addralign),fp);
157
         fwrite((void*)&sh_entsize,1,sizeof(sh_entsize),fp);
158
    };
159
    void Read(FILE *fp) {
160
         fread((void*)&sh_name,1,sizeof(sh_name),fp);
161
         fread((void*)&sh_type,1,sizeof(sh_type),fp);
162
         fread((void*)&sh_flags,1,sizeof(sh_flags),fp);
163
         fread((void*)&sh_addr,1,sizeof(sh_addr),fp);
164
         fread((void*)&sh_offset,1,sizeof(sh_offset),fp);
165
         fread((void*)&sh_size,1,sizeof(sh_size),fp);
166
         fread((void*)&sh_link,1,sizeof(sh_link),fp);
167
         fread((void*)&sh_info,1,sizeof(sh_info),fp);
168
         fread((void*)&sh_addralign,1,sizeof(sh_addralign),fp);
169
         fread((void*)&sh_entsize,1,sizeof(sh_entsize),fp);
170
    };
171
};
172
 
173
#define STB_GLOBAL     1
174
 
175
typedef struct {
176
    int32_t st_name;
177
    int8_t st_info;
178
    int8_t st_other;
179
    int16_t st_shndx;
180
    int64_t st_value;
181
    int64_t st_size;
182
} Elf64Symbol;
183
 
184
typedef struct {
185
    int64_t r_offset;
186
    int64_t r_info;
187
} Elf64rel;
188
 
189
typedef struct {
190
    int64_t r_offset;
191
    int64_t r_info;
192
    int64_t r_addend;
193
} Elf64rela;
194
 
195
// My own constructions
196
typedef struct {
197
    clsElf64Shdr hdr;
198
    int64_t length;
199
    int64_t index;
200
    int64_t address;
201
    int64_t start;
202
    int64_t end;
203
    uint8_t bytes[10000000];
204
} Elf64Section;
205
 
206
class clsElf64Section {
207
public:
208
    clsElf64Shdr hdr;
209
    int64_t length;
210
    int64_t index;
211
    int64_t address;
212
    int64_t start;
213
    int64_t end;
214
    uint8_t bytes[10000000];
215
    uint8_t storebyte;
216
public:
217
    clsElf64Section() {
218
        length = 0;
219
        index = 0;
220
        start = 0;
221
        end = 0;
222
        address = 0;
223
        memset(bytes,0,sizeof(bytes));
224
        storebyte = 1;
225
    };
226
    void Clear() {
227
        length = 0;
228
        index = 0;
229
        start = 0;
230
        end = 0;
231
        address = 0;
232
        memset(bytes,0,sizeof(bytes));
233
    };
234
    void AddByte(int64_t byt) {
235
        if (storebyte)
236
                bytes[index] = byt & 255LL;
237
        if (index==0)
238
            start = address;
239
        index++;
240
        address++;
241
        if (address > end)
242
            end = address;
243
    };
244
    void AddChar(int64_t chr) {
245
        AddByte(chr & 255LL);
246
        AddByte((chr >> 8) & 255LL);
247
    };
248
    void AddHalf(int64_t chr) {
249
        AddChar(chr & 0xFFFFLL);
250
        AddChar((chr >> 16) & 0xFFFFLL);
251
    };
252
    void AddWord(int64_t wd) {
253
        AddHalf(wd & 0xFFFFFFFFLL);
254
        AddHalf((wd >> 32) & 0xFFFFFFFFLL);
255
    };
256
    void Add(Elf64Symbol *sym) {
257
        AddHalf(sym->st_name);
258
        AddByte(sym->st_info);
259
        AddByte(sym->st_other);
260
        AddChar(sym->st_shndx);
261
        AddWord(sym->st_value);
262
        AddWord(sym->st_size);
263
    };
264
    void AddRel(int64_t addr, int64_t info) {
265
        AddWord(addr);
266
        AddWord(info);
267
    };
268
    void Write(FILE *fp) {
269
        fwrite((void *)bytes,1,(size_t)hdr.sh_size,fp);
270
    };
271
    void Read(FILE *fp) {
272
        fread((void *)bytes,1,(size_t)hdr.sh_size,fp);
273
    };
274
};
275
 
276
class clsElf64File
277
{
278
public:
279
    clsElf64Header hdr;
280
    clsElf64Section *sections[256];
281
 
282
    void AddSection(clsElf64Section *sect) {
283
        sections[hdr.e_shnum] = sect;
284
        hdr.e_shnum++;
285
    };
286
 
287
    void WriteSectionHeaderTable(FILE *fp) {
288
        int nn;
289
 
290
        for (nn = 0; nn < hdr.e_shnum; nn++) {
291
            sections[nn]->hdr.Write(fp);
292
        }
293
    };
294
 
295
    void Write(FILE *fp) {
296
        int nn;
297
 
298
        hdr.Write(fp);
299
        fseek(fp, 512, SEEK_SET);
300
        for (nn = 0; nn < hdr.e_shnum; nn++) {
301
            fseek(fp, (size_t)sections[nn]->hdr.sh_offset, SEEK_SET);
302
            sections[nn]->Write(fp);
303
        }
304
        fseek(fp, (size_t)hdr.e_shoff, SEEK_SET);
305
        WriteSectionHeaderTable(fp);
306
    };
307
};
308
 
309
 
310
#define Elf64HdrSz   64
311
#define Elf64pHdrSz  64
312
#define Elf64ShdrSz  64
313
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.