create.c
4.25 KB
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
/*==============================================================================
create.c
create elf sections.
==============================================================================*/
#include <elf.h>
#include <libelf.h>
#include <sys/fcntl.h>
#include <string.h>
#include <compact_reloc.h>
#include <time.h>
#include <sym.h>
#include <malloc.h>
#include "global.h"
#include "update.h"
#include "const.h"
#include "proc.h"
static Elf_Scn *xlate_section = (Elf_Scn *) NULL;
static Elf32_Shdr *xlate_shdr32 = (Elf32_Shdr *) NULL;
static Elf64_Shdr *xlate_shdr64 = (Elf64_Shdr *) NULL;
/*------------------------------------------------------------------------------
Find the highest offset in elf sections.
------------------------------------------------------------------------------*/
Uint32 find_end_of_elf(Elf *elf)
{
Elf_Scn *section;
Uint32 end = 0;
int i;
for(i=1; (section = elf_getscn(elf,i)) != NULL; i++)
{
if (IS_64BIT())
{
Elf64_Shdr *shdr;
shdr = elf64_getshdr(section);
end = MAX(end, (shdr->sh_offset + shdr->sh_size));
}
else
{
Elf32_Shdr *shdr;
shdr = elf32_getshdr(section);
end = MAX(end, (shdr->sh_offset + shdr->sh_size));
}
}
return end;
}
/*------------------------------------------------------------------------------
Add a new section name to the .shstrtab section
------------------------------------------------------------------------------*/
int add_section_name(Elf *elf, char *name)
{
Elf_Scn *section;
Elf_Data *data;
int str_offset;
char *s;
Uint32 length = strlen(name)+1;
int i;
if (IS_64BIT())
{
Elf64_Ehdr *ehdr64;
Elf64_Shdr *shdr64;
ehdr64 = elf64_getehdr(elf);
section = elf_getscn(elf, ehdr64->e_shstrndx);
shdr64 = elf64_getshdr(section);
shdr64->sh_size += length;
}
else
{
Elf32_Ehdr *ehdr32;
Elf32_Shdr *shdr32;
ehdr32 = elf32_getehdr(elf);
section = elf_getscn(elf, ehdr32->e_shstrndx);
shdr32 = elf32_getshdr(section);
shdr32->sh_size += length;
}
data = elf_getdata(section,0);
/*------------------
expand .strtab section
------------------*/
data->d_buf = realloc(data->d_buf, data->d_size + length);
strcpy(data->d_buf + data->d_size, name);
str_offset = data->d_size;
expand_section(section, length);
return (str_offset);
}
/*------------------------------------------------------------------------------
Create Xlate section
------------------------------------------------------------------------------*/
void create_xlate_section(void)
{
int str_offset;
Elf *elf = bin.new_elf;
Uint32 end_of_elf;
str_offset = add_section_name(elf, MIPS_XLATE);
/* note, this has to be called after the new section string is
added and before the new xlate section is created */
end_of_elf = find_end_of_elf(elf);
xlate_section = elf_newscn(elf);
if (IS_64BIT())
{
xlate_shdr64 = elf64_getshdr(xlate_section);
xlate_shdr64->sh_name = str_offset;
xlate_shdr64->sh_type = SHT_MIPS_XLATE;
xlate_shdr64->sh_flags = 0;
xlate_shdr64->sh_offset = end_of_elf;
xlate_shdr64->sh_size = 0;
xlate_shdr64->sh_link = SHN_UNDEF;
xlate_shdr64->sh_info = 0;
xlate_shdr64->sh_addralign = 0x01;
xlate_shdr64->sh_entsize = 0;
}
else
{
xlate_shdr32 = elf32_getshdr(xlate_section);
xlate_shdr32->sh_name = str_offset;
xlate_shdr32->sh_type = SHT_MIPS_XLATE;
xlate_shdr32->sh_flags = 0;
xlate_shdr32->sh_offset = end_of_elf;
xlate_shdr32->sh_size = 0;
xlate_shdr32->sh_link = SHN_UNDEF;
xlate_shdr32->sh_info = 0;
xlate_shdr32->sh_addralign = 0x01;
xlate_shdr32->sh_entsize = 0;
}
}
/*------------------------------------------------------------------------------
Create Xlate section
------------------------------------------------------------------------------*/
void add_xlate_data(void *data_buf, Uint32 data_size)
{
Elf_Data *data;
data = elf_newdata(xlate_section);
data->d_buf = data_buf;
data->d_size = data_size;
if (IS_64BIT())
{
xlate_shdr64->sh_size += data_size;
}
else
{
xlate_shdr32->sh_size += data_size;
}
}