1 |
61 |
csantifort |
/*----------------------------------------------------------------
|
2 |
|
|
// //
|
3 |
|
|
// elfsplitter.c //
|
4 |
|
|
// //
|
5 |
|
|
// This file is part of the Amber project //
|
6 |
|
|
// http://www.opencores.org/project,amber //
|
7 |
|
|
// //
|
8 |
|
|
// Description //
|
9 |
|
|
// Used by the boot loader to split an elf file and copy it //
|
10 |
|
|
// to the correct memory locations ready for execution. //
|
11 |
|
|
// //
|
12 |
|
|
// Author(s): //
|
13 |
|
|
// - Conor Santifort, csantifort.amber@gmail.com //
|
14 |
|
|
// //
|
15 |
|
|
//////////////////////////////////////////////////////////////////
|
16 |
|
|
// //
|
17 |
|
|
// Copyright (C) 2010 Authors and OPENCORES.ORG //
|
18 |
|
|
// //
|
19 |
|
|
// This source file may be used and distributed without //
|
20 |
|
|
// restriction provided that this copyright statement is not //
|
21 |
|
|
// removed from the file and that any derivative work contains //
|
22 |
|
|
// the original copyright notice and the associated disclaimer. //
|
23 |
|
|
// //
|
24 |
|
|
// This source file is free software; you can redistribute it //
|
25 |
|
|
// and/or modify it under the terms of the GNU Lesser General //
|
26 |
|
|
// Public License as published by the Free Software Foundation; //
|
27 |
|
|
// either version 2.1 of the License, or (at your option) any //
|
28 |
|
|
// later version. //
|
29 |
|
|
// //
|
30 |
|
|
// This source is distributed in the hope that it will be //
|
31 |
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied //
|
32 |
|
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
|
33 |
|
|
// PURPOSE. See the GNU Lesser General Public License for more //
|
34 |
|
|
// details. //
|
35 |
|
|
// //
|
36 |
|
|
// You should have received a copy of the GNU Lesser General //
|
37 |
|
|
// Public License along with this source; if not, download it //
|
38 |
|
|
// from http://www.opencores.org/lgpl.shtml //
|
39 |
|
|
// //
|
40 |
|
|
----------------------------------------------------------------*/
|
41 |
|
|
|
42 |
|
|
#define MEM_BUF_ENTRIES 32
|
43 |
|
|
|
44 |
|
|
/*
|
45 |
|
|
ELF File Structure
|
46 |
|
|
A single segment usually consist of several sections. E.g., a loadable
|
47 |
|
|
read-only segment could contain sections for executable code, read-only
|
48 |
|
|
data, and symbols for the dynamic linker. Relocatable files have section
|
49 |
|
|
header tables. Executable files have program header tables. Shared object
|
50 |
|
|
files have both. Sections are intended for further processing by a linker,
|
51 |
|
|
while the segments are intended to be mapped into memory.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
#define EI_NIDENT 16
|
55 |
|
|
#define SHT_PROGBITS 1
|
56 |
|
|
#define SHT_NOBITS 8
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
/* Main ELF Header Table */
|
60 |
|
|
typedef struct {
|
61 |
|
|
unsigned char e_ident[EI_NIDENT]; /* bytes 0 to 15 */
|
62 |
|
|
unsigned short e_e_type; /* bytes 15 to 16 */
|
63 |
|
|
unsigned short e_machine; /* bytes 17 to 18 */
|
64 |
|
|
unsigned int e_version; /* bytes 19 to 22 */
|
65 |
|
|
unsigned int e_entry; /* bytes 23 to 26 */
|
66 |
|
|
unsigned int e_phoff; /* bytes 27 to 30 */
|
67 |
|
|
unsigned int e_shoff; /* bytes 31 to 34 */
|
68 |
|
|
unsigned int e_flags; /* bytes 35 to 38 */
|
69 |
|
|
unsigned short e_ehsize; /* bytes 39 to 40 */
|
70 |
|
|
unsigned short e_phentsize; /* bytes 41 to 42 */
|
71 |
|
|
unsigned short e_phnum; /* bytes 43 to 44 (2B to 2C) */
|
72 |
|
|
unsigned short e_shentsize; /* bytes 45 to 46 */
|
73 |
|
|
unsigned short e_shnum; /* bytes 47 to 48 */
|
74 |
|
|
unsigned short e_shstrndx; /* bytes 49 to 50 */
|
75 |
|
|
} ElfHeader;
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
/* Section Headers */
|
79 |
|
|
typedef struct {
|
80 |
|
|
unsigned int sh_name; /* section name - index into string table */
|
81 |
|
|
unsigned int sh_type; /* SHT_... */
|
82 |
|
|
unsigned int sh_flags; /* SHF_... */
|
83 |
|
|
unsigned int sh_addr; /* virtual address */
|
84 |
|
|
unsigned int sh_offset; /* file offset */
|
85 |
|
|
unsigned int sh_size; /* section size */
|
86 |
|
|
unsigned int sh_link; /* misc info */
|
87 |
|
|
unsigned int sh_info; /* misc info */
|
88 |
|
|
unsigned int sh_addralign; /* memory alignment */
|
89 |
|
|
unsigned int sh_entsize; /* entry size if table */
|
90 |
|
|
} Elf32_Shdr;
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
/* Buffer to hold interrupt vector memory values
|
94 |
|
|
Can't copy these into mem0 locations until ready to pass control
|
95 |
|
|
t new program
|
96 |
|
|
*/
|
97 |
|
|
typedef struct {
|
98 |
|
|
char data;
|
99 |
|
|
int valid;
|
100 |
|
|
} mem_entry_t;
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
typedef struct {
|
104 |
|
|
mem_entry_t entry [MEM_BUF_ENTRIES];
|
105 |
|
|
} mem_buf_t;
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
/* global vectors */
|
109 |
|
|
mem_buf_t* elf_mem0_g;
|
110 |
|
|
|
111 |
|
|
/* function prototypes */
|
112 |
|
|
int elfsplitter (char*, socket_t*);
|