update32.c
14 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*==============================================================================
Module: update32.c
$Revision: 1.1.1.2 $
$Date: 2002/10/29 08:07:15 $
$Author: blythe $
$Source: /root/leakn64/depot/rf/sw/bbplayer/tools/gcord/com/update32.c,v $
==============================================================================*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "global.h"
#include "option.h"
#include "update.h"
/*------------------------------------------------------------------------------
find a section by it's name
------------------------------------------------------------------------------*/
Elf_Scn *find_section_by_name32(Elf *elf, char *name)
{
Elf32_Ehdr *ehdr32;
Elf_Scn *section;
Elf_Data *data;
char *shstrtab;
int i;
ehdr32 = elf32_getehdr(elf);
section = elf_getscn(elf, ehdr32->e_shstrndx);
data = elf_getdata(section, 0);
shstrtab = data->d_buf;
for (i = 0; (section = elf_getscn(elf, i)) != NULL; i++){
char *section_name;
Elf32_Shdr *shdr32 = elf32_getshdr(section);
section_name = shstrtab + shdr32->sh_name;
if ( option.debug )
printf( "section %s section 0x%x\n", section_name, section);
if (!strcmp(section_name, name)) {
return (section);
}
}
return (Elf_Scn *) 0;
}
/*------------------------------------------------------------------------------
look in a section, return the physical location given an address
------------------------------------------------------------------------------*/
static Uint32 *get_pointer_in_section32(Elf *elf, char *name, Uint32 addr)
{
Elf_Scn *section;
Elf32_Shdr *shdr;
Elf_Data *data;
section = find_section_by_name32(elf, name);
if (section == NULL)
return 0;
shdr = elf32_getshdr(section);
data = elf_getdata(section, 0);
if ( addr >= shdr->sh_addr &&
addr < shdr->sh_addr + shdr->sh_size){
return (Uint32 *) (data->d_buf + (Uint32) (addr - shdr->sh_addr));
}
return (Uint32 *) 0;
}
/*------------------------------------------------------------------------------
return the physical location given an address
------------------------------------------------------------------------------*/
Uint32 *get_textdata_by_addr32(Elf *elf, Uint32 addr)
{
Uint32 *pointer;
GCORDSECTPTR thisSect = gcordSectList;
/*------------------
check if it's in one of our segs
------------------*/
while ( thisSect )
{
pointer = get_pointer_in_section32(elf, thisSect->name, addr);
if ( pointer )
return pointer;
thisSect = thisSect->next;
}
/*------------------
check if it's in .data
------------------*/
pointer = get_pointer_in_section32(elf, ELF_DATA, addr);
if (pointer)
return (pointer);
/*------------------
check if it's in .rodata
------------------*/
pointer = get_pointer_in_section32(elf, ELF_RODATA, addr);
if (pointer)
return (pointer);
/*------------------
check if it's in .text
------------------*/
pointer = get_pointer_in_section32(elf, ELF_REL_TEXT, addr);
if (pointer)
return (pointer);
/*------------------
check if it's in .sdata
------------------*/
pointer = get_pointer_in_section32(elf, MIPS_SDATA, addr);
if (pointer)
return (pointer);
#if 0
/*------------------
check if it's in .srdata
------------------*/
pointer = get_pointer_in_section32(elf, MIPS_SRDATA, addr);
if (pointer)
return (pointer);
#endif
/*------------------
check if it's in .got
------------------*/
pointer = get_pointer_in_section32(elf, ELF_GOT, addr);
if (pointer)
return (pointer);
error("cannot find buffer for address %#llx", (Uint64) addr);
}
/*------------------------------------------------------------------------------
update the program entry point
------------------------------------------------------------------------------*/
void update_elf_header32(void)
{
Elf32_Ehdr *ehdr;
ehdr = elf32_getehdr(bin.new_elf);
if (IN_TEXT(ehdr->e_entry)){
ehdr->e_entry = NEW_ADDR(ehdr->e_entry);
}else{
error("e_entry not in .text");
}
}
/*------------------------------------------------------------------------------
chek if the content is a text, update if so.
------------------------------------------------------------------------------*/
void check_for_text_and_update32(Uint32 *value)
{
if (IN_TEXT(*value))
{
*value = NEW_ADDR(*value);
}
else if (IN_BROAD_TEXT(*value) && *value > (Uint32) bin.text_hi)
{
*value += bin.text_expand_offset;
}
}
/*------------------------------------------------------------------------------
Expand a section and move all sections after it by "offset"..
------------------------------------------------------------------------------*/
void expand_section32(Elf_Scn *move_section, Uint32 offset)
{
Elf *elf = bin.new_elf;
Elf_Scn *section;
Elf32_Ehdr *ehdr;
Elf_Data *data;
int move = 0;
int i;
ehdr = elf32_getehdr(elf);
for (i = 0; (section = elf_getscn(elf, i)) != NULL; i++)
{
Elf32_Shdr *shdr = elf32_getshdr(section);
if (move == 1)
{
shdr->sh_offset += offset;
}
else if (section == move_section)
{
/* change the offset in elf header, if it's after the section */
if (ehdr->e_shoff > shdr->sh_offset)
{
ehdr->e_shoff += offset;
}
data = elf_getdata(section, 0);
shdr->sh_size += offset;
data->d_size += offset;
move = 1;
}
}
}
/*------------------------------------------------------------------------------
update .dynamic section
------------------------------------------------------------------------------*/
void update_dynamic32(Elf *elf)
{
Elf_Data *data;
Elf32_Dyn *dynamic;
data = find_section_data_by_name(bin.new_elf, ELF_DYNAMIC);
if (!data)
return;
dynamic = (Elf32_Dyn *) data->d_buf;
for (; dynamic->d_tag != DT_NULL; dynamic++){
switch (dynamic->d_tag){
case DT_MIPS_TIME_STAMP:
dynamic->d_un.d_val = time(0); /* change the time stamp */
break;
case DT_MIPS_FLAGS:
bin.dt_flags = dynamic->d_un.d_val;
if (dynamic->d_un.d_val & RHF_CORD)
warning("%s is a corded binary",option.in_file);
dynamic->d_un.d_val |= RHF_CORD;
/*-------------------------
dynamic->d_un.d_val &= ~RHF_QUICKSTART;
-------------------------*/
break;
case DT_INIT:
case DT_FINI:
/* change the init and fini address */
if (IN_TEXT(dynamic->d_un.d_val)){
dynamic->d_un.d_val = NEW_ADDR(dynamic->d_un.d_val);
}
default:
break;
}
}
}
/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
void update_got32(void)
{
Elf_Data *data;
Elf32_Got *got;
int i;
data = find_section_data_by_name(bin.new_elf, ELF_GOT);
if (!data)
return;
got = (Elf32_Got *) data->d_buf;
/*------------------
1. The first entry is used by rld for lazy text resolution,
it may contain non-zero entry in rld, that's why we start from 1.
2. heuristic to find hidden symbol.
------------------*/
for (i = 1; i < bin.dt_local_gotno; i++)
{
if (got[i].g_index & 0xffff)
break;
}
if (i != bin.dt_local_gotno)
{
/* hidden symbol found. perform fail-proof checking */
Elf_Scn *section;
Elf32_Sym *sym;
Elf_Data *data;
section = find_section_by_name32(bin.new_elf, ELF_DYNSYM);
if (section == NULL)
return;
data = elf_getdata(section, 0);
sym = (Elf32_Sym *) data->d_buf;
if (sym[bin.dt_gotsym-(bin.dt_local_gotno-i)].st_other != STO_HIDDEN )
{
warning("incorrect hidden symbol");
}
}
/*------------------
We have already skipped the local got entry.
Now update the global entry (and possibly hidden symbols).
------------------*/
for (; i < data->d_size/sizeof(Elf32_Got); i++)
{
check_for_text_and_update32(&got[i].g_index);
}
}
/*------------------------------------------------------------------------------
TO_DO: this is not verified.
------------------------------------------------------------------------------*/
void update_reldyn32(Elf *elf)
{
Elf32_Rel *rel;
Elf_Data *data;
int i;
data = find_section_data_by_name(bin.new_elf, ELF_REL_DYN);
if (data == NULL)
return;
rel = (Elf32_Rel *) data->d_buf;
for (i = 0 ; i < data->d_size/sizeof(Elf32_Rel); i++)
{
Uint32 addr = rel[i].r_offset;
Uint32 *content;
switch(ELF32_R_TYPE(rel[i].r_info))
{
case R_MIPS_REL32:
content = get_textdata_by_addr32(bin.new_elf, (Uint32) addr);
check_for_text_and_update32(content);
case R_MIPS_NONE:
break;
default:
warning("unknown elf rel type");
}
}
}
/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
void update_dynsym32(void)
{
Elf32_Sym *sym;
Elf_Data *data;
int i;
data = find_section_data_by_name(bin.new_elf, ELF_DYNSYM);
if (!data)
return;
sym = (Elf32_Sym *) data->d_buf;
for (i = 0; i < data->d_size/sizeof(Elf32_Sym); i++)
{
check_for_text_and_update32(&sym[i].st_value);
}
}
/*------------------------------------------------------------------------------
Update .symtab section
------------------------------------------------------------------------------*/
void update_symtab32(void)
{
Elf32_Sym *sym;
Elf_Data *data;
int i;
if ( option.debug )
printf( "Updating symtab: addrs %x..%x\n", bin.text_lo, bin.text_hi );
data = find_section_data_by_name(bin.new_elf, ELF_SYMTAB);
if (data == NULL)
return;
sym = (Elf32_Sym *) data->d_buf;
for (i = 0; i < data->d_size/sizeof(Elf32_Sym); i++)
{
if ((sym[i].st_shndx != SHN_ABS) &&
(sym[i].st_value != 0) &&
(sym[i].st_shndx != SHN_COMMON ||
sym[i].st_value>MAX_COMMON_ALIGNMENT))
{
/* symbol with valid addresses */
check_for_text_and_update32(&sym[i].st_value);
}
}
}
/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
void expand_text32(void)
{
Elf *elf = bin.new_elf;
Elf32_Ehdr *ehdr;
Elf32_Phdr *phdr;
Elf_Scn *section;
Elf32_Shdr *shdr;
Elf_Data *data;
char *shstrtab;
int i;
int move_flag = 0;
ehdr = elf32_getehdr(elf);
section = elf_getscn(elf, ehdr->e_shstrndx);
data = elf_getdata(section, 0);
shstrtab = data->d_buf;
for (i = 0; (section = elf_getscn(elf, i)) != NULL; i++)
{
char *section_name;
Elf32_Shdr *shdr = elf32_getshdr(section);
section_name = shstrtab + shdr->sh_name;
if (!strcmp(section_name, ".text"))
{
/* TO_DO: multiple .text sections */
shdr->sh_size += bin.text_expand_size;
data = elf_getdata(section, 0);
data->d_size += bin.text_expand_size;
/* move all sections after this */
ASSERT(move_flag == 0);
move_flag =1;
}
else if (move_flag && bin.text_expand_offset)
{
shdr->sh_offset += bin.text_expand_offset;
/* move the virtual address only for text */
if (shdr->sh_flags & SHF_EXECINSTR)
shdr->sh_addr += bin.text_expand_offset;
}
}
phdr = elf32_getphdr(elf);
move_flag = 0;
for (i=0; i < ehdr->e_phnum; i++)
{
if (phdr[i].p_type == PT_LOAD && (phdr[i].p_flags & PF_R)
&& (phdr[i].p_flags & PF_X))
{
/* move all segment after this */
ASSERT(move_flag == 0);
move_flag = 1;
phdr[i].p_filesz += bin.text_expand_offset;
phdr[i].p_memsz += bin.text_expand_offset;
}
else if (move_flag)
{
phdr[i].p_offset += bin.text_expand_offset;
}
}
section = find_section_by_name32(elf, MIPS_MDEBUG);
if (section)
{
shdr = elf32_getshdr(section);
/* to fool tools into thinking that mdebug section does not exist */
shdr->sh_name =0;
data = elf_getdata(section, 0);
move_mdebug(data->d_buf, data->d_size, shdr->sh_offset + data->d_off);
}
}
/*------------------------------------------------------------------------------
1. Process events section.
2. Update contents in events section.
------------------------------------------------------------------------------*/
void update_events32(Elf *elf)
{
}
/*------------------------------------------------------------------------------
The runtime procedure table is used by ADA,
COBOL and PL/1 for exception handling. This
table is tacked on the end of the .data section
and is pointed to by the symbols:
_procedure_table - table location
_procedure_table_size - number of table entries
if the value of _procedure_table is zero, the
table will be in a non-loadable part of the object file
(currently hidden not in a section) and its file offset
is stored in the program header.
------------------------------------------------------------------------------*/
void update_rpt32(Elf *elf)
{
}