Commit b37837317fb3177755c592591d7f53826c6afae5

Authored by bellard
1 parent 5fe141fd

use generic ELF loader


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1832 c046a42c-6fe2-441c-8c8c-71466251a162
hw/elf_ops.h deleted 100644 → 0
1   -#ifdef BSWAP_NEEDED
2   -static void glue(bswap_ehdr, SZ)(struct elfhdr *ehdr)
3   -{
4   - bswap16s(&ehdr->e_type); /* Object file type */
5   - bswap16s(&ehdr->e_machine); /* Architecture */
6   - bswap32s(&ehdr->e_version); /* Object file version */
7   - bswapSZs(&ehdr->e_entry); /* Entry point virtual address */
8   - bswapSZs(&ehdr->e_phoff); /* Program header table file offset */
9   - bswapSZs(&ehdr->e_shoff); /* Section header table file offset */
10   - bswap32s(&ehdr->e_flags); /* Processor-specific flags */
11   - bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
12   - bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
13   - bswap16s(&ehdr->e_phnum); /* Program header table entry count */
14   - bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
15   - bswap16s(&ehdr->e_shnum); /* Section header table entry count */
16   - bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
17   -}
18   -
19   -static void glue(bswap_phdr, SZ)(struct elf_phdr *phdr)
20   -{
21   - bswap32s(&phdr->p_type); /* Segment type */
22   - bswapSZs(&phdr->p_offset); /* Segment file offset */
23   - bswapSZs(&phdr->p_vaddr); /* Segment virtual address */
24   - bswapSZs(&phdr->p_paddr); /* Segment physical address */
25   - bswapSZs(&phdr->p_filesz); /* Segment size in file */
26   - bswapSZs(&phdr->p_memsz); /* Segment size in memory */
27   - bswap32s(&phdr->p_flags); /* Segment flags */
28   - bswapSZs(&phdr->p_align); /* Segment alignment */
29   -}
30   -
31   -static void glue(bswap_shdr, SZ)(struct elf_shdr *shdr)
32   -{
33   - bswap32s(&shdr->sh_name);
34   - bswap32s(&shdr->sh_type);
35   - bswapSZs(&shdr->sh_flags);
36   - bswapSZs(&shdr->sh_addr);
37   - bswapSZs(&shdr->sh_offset);
38   - bswapSZs(&shdr->sh_size);
39   - bswap32s(&shdr->sh_link);
40   - bswap32s(&shdr->sh_info);
41   - bswapSZs(&shdr->sh_addralign);
42   - bswapSZs(&shdr->sh_entsize);
43   -}
44   -
45   -static void glue(bswap_sym, SZ)(struct elf_sym *sym)
46   -{
47   - bswap32s(&sym->st_name);
48   - bswapSZs(&sym->st_value);
49   - bswapSZs(&sym->st_size);
50   - bswap16s(&sym->st_shndx);
51   -}
52   -#endif
53   -
54   -static int glue(find_phdr, SZ)(struct elfhdr *ehdr, int fd, struct elf_phdr *phdr, elf_word type)
55   -{
56   - int i, retval;
57   -
58   - retval = lseek(fd, ehdr->e_phoff, SEEK_SET);
59   - if (retval < 0)
60   - return -1;
61   -
62   - for (i = 0; i < ehdr->e_phnum; i++) {
63   - retval = read(fd, phdr, sizeof(*phdr));
64   - if (retval < 0)
65   - return -1;
66   - glue(bswap_phdr, SZ)(phdr);
67   - if (phdr->p_type == type)
68   - return 0;
69   - }
70   - return -1;
71   -}
72   -
73   -static void * glue(find_shdr, SZ)(struct elfhdr *ehdr, int fd, struct elf_shdr *shdr, elf_word type)
74   -{
75   - int i, retval;
76   -
77   - retval = lseek(fd, ehdr->e_shoff, SEEK_SET);
78   - if (retval < 0)
79   - return NULL;
80   -
81   - for (i = 0; i < ehdr->e_shnum; i++) {
82   - retval = read(fd, shdr, sizeof(*shdr));
83   - if (retval < 0)
84   - return NULL;
85   - glue(bswap_shdr, SZ)(shdr);
86   - if (shdr->sh_type == type)
87   - return qemu_malloc(shdr->sh_size);
88   - }
89   - return NULL;
90   -}
91   -
92   -static void * glue(find_strtab, SZ)(struct elfhdr *ehdr, int fd, struct elf_shdr *shdr, struct elf_shdr *symtab)
93   -{
94   - int retval;
95   -
96   - retval = lseek(fd, ehdr->e_shoff + sizeof(struct elf_shdr) * symtab->sh_link, SEEK_SET);
97   - if (retval < 0)
98   - return NULL;
99   -
100   - retval = read(fd, shdr, sizeof(*shdr));
101   - if (retval < 0)
102   - return NULL;
103   - glue(bswap_shdr, SZ)(shdr);
104   - if (shdr->sh_type == SHT_STRTAB)
105   - return qemu_malloc(shdr->sh_size);;
106   - return NULL;
107   -}
108   -
109   -static int glue(read_program, SZ)(int fd, struct elf_phdr *phdr, void *dst, elf_word entry)
110   -{
111   - int retval;
112   - retval = lseek(fd, phdr->p_offset + entry - phdr->p_vaddr, SEEK_SET);
113   - if (retval < 0)
114   - return -1;
115   - return read(fd, dst, phdr->p_filesz);
116   -}
117   -
118   -static int glue(read_section, SZ)(int fd, struct elf_shdr *s, void *dst)
119   -{
120   - int retval;
121   -
122   - retval = lseek(fd, s->sh_offset, SEEK_SET);
123   - if (retval < 0)
124   - return -1;
125   - retval = read(fd, dst, s->sh_size);
126   - if (retval < 0)
127   - return -1;
128   - return 0;
129   -}
130   -
131   -static void * glue(process_section, SZ)(struct elfhdr *ehdr, int fd, struct elf_shdr *shdr, elf_word type)
132   -{
133   - void *dst;
134   -
135   - dst = glue(find_shdr, SZ)(ehdr, fd, shdr, type);
136   - if (!dst)
137   - goto error;
138   -
139   - if (glue(read_section, SZ)(fd, shdr, dst))
140   - goto error;
141   - return dst;
142   - error:
143   - qemu_free(dst);
144   - return NULL;
145   -}
146   -
147   -static void * glue(process_strtab, SZ)(struct elfhdr *ehdr, int fd, struct elf_shdr *shdr, struct elf_shdr *symtab)
148   -{
149   - void *dst;
150   -
151   - dst = glue(find_strtab, SZ)(ehdr, fd, shdr, symtab);
152   - if (!dst)
153   - goto error;
154   -
155   - if (glue(read_section, SZ)(fd, shdr, dst))
156   - goto error;
157   - return dst;
158   - error:
159   - qemu_free(dst);
160   - return NULL;
161   -}
162   -
163   -static void glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd)
164   -{
165   - struct elf_shdr symtab, strtab;
166   - struct elf_sym *syms;
167   -#if (SZ == 64)
168   - struct elf32_sym *syms32;
169   -#endif
170   - struct syminfo *s;
171   - int nsyms, i;
172   - char *str;
173   -
174   - /* Symbol table */
175   - syms = glue(process_section, SZ)(ehdr, fd, &symtab, SHT_SYMTAB);
176   - if (!syms)
177   - return;
178   -
179   - nsyms = symtab.sh_size / sizeof(struct elf_sym);
180   -#if (SZ == 64)
181   - syms32 = qemu_mallocz(nsyms * sizeof(struct elf32_sym));
182   -#endif
183   - for (i = 0; i < nsyms; i++) {
184   - glue(bswap_sym, SZ)(&syms[i]);
185   -#if (SZ == 64)
186   - syms32[i].st_name = syms[i].st_name;
187   - syms32[i].st_info = syms[i].st_info;
188   - syms32[i].st_other = syms[i].st_other;
189   - syms32[i].st_shndx = syms[i].st_shndx;
190   - syms32[i].st_value = syms[i].st_value & 0xffffffff;
191   - syms32[i].st_size = syms[i].st_size & 0xffffffff;
192   -#endif
193   - }
194   - /* String table */
195   - str = glue(process_strtab, SZ)(ehdr, fd, &strtab, &symtab);
196   - if (!str)
197   - goto error_freesyms;
198   -
199   - /* Commit */
200   - s = qemu_mallocz(sizeof(*s));
201   -#if (SZ == 64)
202   - s->disas_symtab = syms32;
203   - qemu_free(syms);
204   -#else
205   - s->disas_symtab = syms;
206   -#endif
207   - s->disas_num_syms = nsyms;
208   - s->disas_strtab = str;
209   - s->next = syminfos;
210   - syminfos = s;
211   - return;
212   - error_freesyms:
213   -#if (SZ == 64)
214   - qemu_free(syms32);
215   -#endif
216   - qemu_free(syms);
217   - return;
218   -}
hw/magic-load.c deleted 100644 → 0
1   -#include "vl.h"
2   -#include "disas.h"
3   -#include "exec-all.h"
4   -
5   -struct exec
6   -{
7   - uint32_t a_info; /* Use macros N_MAGIC, etc for access */
8   - uint32_t a_text; /* length of text, in bytes */
9   - uint32_t a_data; /* length of data, in bytes */
10   - uint32_t a_bss; /* length of uninitialized data area, in bytes */
11   - uint32_t a_syms; /* length of symbol table data in file, in bytes */
12   - uint32_t a_entry; /* start address */
13   - uint32_t a_trsize; /* length of relocation info for text, in bytes */
14   - uint32_t a_drsize; /* length of relocation info for data, in bytes */
15   -};
16   -
17   -#ifdef BSWAP_NEEDED
18   -static void bswap_ahdr(struct exec *e)
19   -{
20   - bswap32s(&e->a_info);
21   - bswap32s(&e->a_text);
22   - bswap32s(&e->a_data);
23   - bswap32s(&e->a_bss);
24   - bswap32s(&e->a_syms);
25   - bswap32s(&e->a_entry);
26   - bswap32s(&e->a_trsize);
27   - bswap32s(&e->a_drsize);
28   -}
29   -#else
30   -#define bswap_ahdr(x) do { } while (0)
31   -#endif
32   -
33   -#define N_MAGIC(exec) ((exec).a_info & 0xffff)
34   -#define OMAGIC 0407
35   -#define NMAGIC 0410
36   -#define ZMAGIC 0413
37   -#define QMAGIC 0314
38   -#define _N_HDROFF(x) (1024 - sizeof (struct exec))
39   -#define N_TXTOFF(x) \
40   - (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
41   - (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
42   -#define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0)
43   -#define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
44   -#define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1))
45   -
46   -#define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)
47   -
48   -#define N_DATADDR(x) \
49   - (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \
50   - : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x))))
51   -
52   -
53   -#define ELF_CLASS ELFCLASS32
54   -#define ELF_DATA ELFDATA2MSB
55   -#define ELF_ARCH EM_SPARC
56   -
57   -#include "elf.h"
58   -
59   -#ifndef BSWAP_NEEDED
60   -#define bswap_ehdr32(e) do { } while (0)
61   -#define bswap_phdr32(e) do { } while (0)
62   -#define bswap_shdr32(e) do { } while (0)
63   -#define bswap_sym32(e) do { } while (0)
64   -#ifdef TARGET_SPARC64
65   -#define bswap_ehdr64(e) do { } while (0)
66   -#define bswap_phdr64(e) do { } while (0)
67   -#define bswap_shdr64(e) do { } while (0)
68   -#define bswap_sym64(e) do { } while (0)
69   -#endif
70   -#endif
71   -
72   -#define SZ 32
73   -#define elf_word uint32_t
74   -#define bswapSZs bswap32s
75   -#include "elf_ops.h"
76   -
77   -#ifdef TARGET_SPARC64
78   -#undef elfhdr
79   -#undef elf_phdr
80   -#undef elf_shdr
81   -#undef elf_sym
82   -#undef elf_note
83   -#undef elf_word
84   -#undef bswapSZs
85   -#undef SZ
86   -#define elfhdr elf64_hdr
87   -#define elf_phdr elf64_phdr
88   -#define elf_note elf64_note
89   -#define elf_shdr elf64_shdr
90   -#define elf_sym elf64_sym
91   -#define elf_word uint64_t
92   -#define bswapSZs bswap64s
93   -#define SZ 64
94   -#include "elf_ops.h"
95   -#endif
96   -
97   -int load_elf(const char *filename, uint8_t *addr)
98   -{
99   - struct elf32_hdr ehdr;
100   - int retval, fd;
101   - Elf32_Half machine;
102   -
103   - fd = open(filename, O_RDONLY | O_BINARY);
104   - if (fd < 0)
105   - goto error;
106   -
107   - retval = read(fd, &ehdr, sizeof(ehdr));
108   - if (retval < 0)
109   - goto error;
110   -
111   - if (ehdr.e_ident[0] != 0x7f || ehdr.e_ident[1] != 'E'
112   - || ehdr.e_ident[2] != 'L' || ehdr.e_ident[3] != 'F')
113   - goto error;
114   - machine = tswap16(ehdr.e_machine);
115   - if (machine == EM_SPARC || machine == EM_SPARC32PLUS) {
116   - struct elf32_phdr phdr;
117   -
118   - bswap_ehdr32(&ehdr);
119   -
120   - if (find_phdr32(&ehdr, fd, &phdr, PT_LOAD))
121   - goto error;
122   - retval = read_program32(fd, &phdr, addr, ehdr.e_entry);
123   - if (retval < 0)
124   - goto error;
125   - load_symbols32(&ehdr, fd);
126   - }
127   -#ifdef TARGET_SPARC64
128   - else if (machine == EM_SPARCV9) {
129   - struct elf64_hdr ehdr64;
130   - struct elf64_phdr phdr;
131   -
132   - lseek(fd, 0, SEEK_SET);
133   -
134   - retval = read(fd, &ehdr64, sizeof(ehdr64));
135   - if (retval < 0)
136   - goto error;
137   -
138   - bswap_ehdr64(&ehdr64);
139   -
140   - if (find_phdr64(&ehdr64, fd, &phdr, PT_LOAD))
141   - goto error;
142   - retval = read_program64(fd, &phdr, phys_ram_base + ehdr64.e_entry, ehdr64.e_entry);
143   - if (retval < 0)
144   - goto error;
145   - load_symbols64(&ehdr64, fd);
146   - }
147   -#endif
148   -
149   - close(fd);
150   - return retval;
151   - error:
152   - close(fd);
153   - return -1;
154   -}
155   -
156   -int load_aout(const char *filename, uint8_t *addr)
157   -{
158   - int fd, size, ret;
159   - struct exec e;
160   - uint32_t magic;
161   -
162   - fd = open(filename, O_RDONLY | O_BINARY);
163   - if (fd < 0)
164   - return -1;
165   -
166   - size = read(fd, &e, sizeof(e));
167   - if (size < 0)
168   - goto fail;
169   -
170   - bswap_ahdr(&e);
171   -
172   - magic = N_MAGIC(e);
173   - switch (magic) {
174   - case ZMAGIC:
175   - case QMAGIC:
176   - case OMAGIC:
177   - lseek(fd, N_TXTOFF(e), SEEK_SET);
178   - size = read(fd, addr, e.a_text + e.a_data);
179   - if (size < 0)
180   - goto fail;
181   - break;
182   - case NMAGIC:
183   - lseek(fd, N_TXTOFF(e), SEEK_SET);
184   - size = read(fd, addr, e.a_text);
185   - if (size < 0)
186   - goto fail;
187   - ret = read(fd, addr + N_DATADDR(e), e.a_data);
188   - if (ret < 0)
189   - goto fail;
190   - size += ret;
191   - break;
192   - default:
193   - goto fail;
194   - }
195   - close(fd);
196   - return size;
197   - fail:
198   - close(fd);
199   - return -1;
200   -}
201   -
hw/sun4m.c
... ... @@ -26,6 +26,7 @@
26 26 #define KERNEL_LOAD_ADDR 0x00004000
27 27 #define CMDLINE_ADDR 0x007ff000
28 28 #define INITRD_LOAD_ADDR 0x00800000
  29 +#define PROM_SIZE_MAX (256 * 1024)
29 30 #define PROM_ADDR 0xffd00000
30 31 #define PROM_FILENAMEB "proll.bin"
31 32 #define PROM_FILENAMEE "proll.elf"
... ... @@ -263,9 +264,12 @@ static void sun4m_init(int ram_size, int vga_ram_size, int boot_device,
263 264 slavio_misc = slavio_misc_init(PHYS_JJ_SLAVIO, PHYS_JJ_ME_IRQ);
264 265  
265 266 prom_offset = ram_size + vram_size;
  267 + cpu_register_physical_memory(PROM_ADDR,
  268 + (PROM_SIZE_MAX + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK,
  269 + prom_offset | IO_MEM_ROM);
266 270  
267 271 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, PROM_FILENAMEE);
268   - ret = load_elf(buf, phys_ram_base + prom_offset);
  272 + ret = load_elf(buf, 0);
269 273 if (ret < 0) {
270 274 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, PROM_FILENAMEB);
271 275 ret = load_image(buf, phys_ram_base + prom_offset);
... ... @@ -275,12 +279,10 @@ static void sun4m_init(int ram_size, int vga_ram_size, int boot_device,
275 279 buf);
276 280 exit(1);
277 281 }
278   - cpu_register_physical_memory(PROM_ADDR, (ret + TARGET_PAGE_SIZE) & TARGET_PAGE_MASK,
279   - prom_offset | IO_MEM_ROM);
280 282  
281 283 kernel_size = 0;
282 284 if (linux_boot) {
283   - kernel_size = load_elf(kernel_filename, phys_ram_base + KERNEL_LOAD_ADDR);
  285 + kernel_size = load_elf(kernel_filename, -0xf0000000);
284 286 if (kernel_size < 0)
285 287 kernel_size = load_aout(kernel_filename, phys_ram_base + KERNEL_LOAD_ADDR);
286 288 if (kernel_size < 0)
... ...
hw/sun4u.c
... ... @@ -27,6 +27,7 @@
27 27 #define KERNEL_LOAD_ADDR 0x00404000
28 28 #define CMDLINE_ADDR 0x003ff000
29 29 #define INITRD_LOAD_ADDR 0x00300000
  30 +#define PROM_SIZE_MAX (256 * 1024)
30 31 #define PROM_ADDR 0x1fff0000000ULL
31 32 #define APB_SPECIAL_BASE 0x1fe00000000ULL
32 33 #define APB_MEM_BASE 0x1ff00000000ULL
... ... @@ -277,9 +278,12 @@ static void sun4u_init(int ram_size, int vga_ram_size, int boot_device,
277 278 cpu_register_physical_memory(0, ram_size, 0);
278 279  
279 280 prom_offset = ram_size + vga_ram_size;
  281 + cpu_register_physical_memory(PROM_ADDR,
  282 + (PROM_SIZE_MAX + TARGET_PAGE_SIZE) & TARGET_PAGE_MASK,
  283 + prom_offset | IO_MEM_ROM);
280 284  
281 285 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, PROM_FILENAMEE);
282   - ret = load_elf(buf, phys_ram_base + prom_offset);
  286 + ret = load_elf(buf, 0);
283 287 if (ret < 0) {
284 288 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, PROM_FILENAMEB);
285 289 ret = load_image(buf, phys_ram_base + prom_offset);
... ... @@ -289,13 +293,12 @@ static void sun4u_init(int ram_size, int vga_ram_size, int boot_device,
289 293 buf);
290 294 exit(1);
291 295 }
292   - cpu_register_physical_memory(PROM_ADDR, (ret + TARGET_PAGE_SIZE) & TARGET_PAGE_MASK,
293   - prom_offset | IO_MEM_ROM);
294 296  
295 297 kernel_size = 0;
296 298 initrd_size = 0;
297 299 if (linux_boot) {
298   - kernel_size = load_elf(kernel_filename, phys_ram_base + KERNEL_LOAD_ADDR);
  300 + /* XXX: put correct offset */
  301 + kernel_size = load_elf(kernel_filename, 0);
299 302 if (kernel_size < 0)
300 303 kernel_size = load_aout(kernel_filename, phys_ram_base + KERNEL_LOAD_ADDR);
301 304 if (kernel_size < 0)
... ...