3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 int exec_elf (unsigned char *image
, unsigned *entry
, unsigned *elf_data
, unsigned *elf_data_off
, unsigned *elf_bss
)
29 unsigned s
, r
, reloc_size
;
36 file
= (elf_file_t
*) image
;
38 if (!arch_elf_detect (file
))
41 /* find the BSS and allocate memory for it
42 This must be done BEFORE doing any relocations */
43 for (s
= 0; s
< file
->num_sects
; s
++) {
44 sect
= (elf_sect_t
*)(image
+ file
->shtab_offset
+
45 file
->shtab_ent_size
* s
);
47 if(sect
->type
!= 8) /* NOBITS */
53 DPRINT (DBG_ELF
, "ELF .bss section is too small (%d bytes)", r
);
57 *elf_bss
= (unsigned) sect
->virt_adr
;
60 DPRINT (DBG_ELF
, "Section .bss obtain wrong address: 0x%x", *elf_bss
);
64 /* clean memory for bss section */
65 memset (image
+sect
->offset
, 0, sect
->size
);
70 elf_sect_t
*rodata
= 0;
72 /* for each section... */
73 for (s
= 0; s
< file
->num_sects
; s
++) {
74 sect
= (elf_sect_t
*)(image
+ file
->shtab_offset
+ file
->shtab_ent_size
* s
);
76 if (sect
->type
== 1) {
77 if (sect
->sect_name
== 0x17 || sect
->sect_name
== 0x27) {
83 /* is it a relocation section?
84 xxx - we don't handle the extra addend for RELA relocations */
85 if(sect
->type
== 4) /* RELA */
87 else if(sect
->type
== 9)/* REL */
93 /* rodata section is not required */
95 DPRINT (DBG_ELF
, "rodata section does not exist");
97 /* find start of .text and make it the entry point */
100 for (s
= 0; s
< file
->num_sects
; s
++) {
101 sect
= (elf_sect_t
*) (image
+ file
->shtab_offset
+ file
->shtab_ent_size
* s
);
103 if (sect
->sect_name
!= 0x0b && sect
->sect_name
!= 0x1b)
106 (*entry
) = (unsigned) sect
->virt_adr
;
111 /* copy rodata section to our memory area section */
113 *elf_data
= (unsigned) rodata
->virt_adr
;
114 *elf_data_off
= (unsigned) rodata
->offset
;
116 *elf_data
= (unsigned) 0;
117 *elf_data_off
= (unsigned) 0;
121 printf ("ELF -> Can't find section .text, so entry point is unknown\n");