* fpu.c, gdb-if.c, load.c, misc.c, syscalls.c (config.h): Include.
[deliverable/binutils-gdb.git] / sim / rx / load.c
1 /* load.c --- loading object files into the RX simulator.
2
3 Copyright (C) 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
5
6 This file is part of the GNU simulators.
7
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.
12
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.
17
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/>. */
20
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "bfd.h"
28 #include "libbfd.h"
29 #include "cpu.h"
30 #include "mem.h"
31 #include "elf/internal.h"
32 #include "elf/common.h"
33
34 /* A note about endianness and swapping...
35
36 The RX chip is CISC-like in that the opcodes are variable length
37 and are read as a stream of bytes. However, the chip itself shares
38 the code prefetch block with the data fetch block, so when it's
39 configured for big endian mode, the memory fetched for opcodes is
40 word-swapped. To compensate for this, the ELF file has the code
41 sections pre-swapped. Our BFD knows this, and for the convenience
42 of all the other tools, hides this swapping at a very low level.
43 I.e. it swaps words on the way out and on the way back in.
44
45 Fortunately the iovector routines are unaffected by this, so we
46 can use them to read in the segments directly, without having
47 to worry about byte swapping anything.
48
49 However, our opcode decoder and disassemblers need to swap the data
50 after reading it from the chip memory, just like the chip does.
51 All in all, the code words are swapped four times between the
52 assembler and our decoder.
53
54 If the chip is running in little-endian mode, no swapping is done
55 anywhere. Note also that the *operands* within opcodes are always
56 encoded in little-endian format. */
57
58 void
59 rx_load (bfd *prog)
60 {
61 unsigned long highest_addr_loaded = 0;
62 Elf_Internal_Phdr * phdrs;
63 long sizeof_phdrs;
64 int num_headers;
65 int i;
66
67 rx_big_endian = bfd_big_endian (prog);
68
69 /* Note we load by ELF program header not by BFD sections.
70 This is because BFD sections get their information from
71 the ELF section structure, which only includes a VMA value
72 and not an LMA value. */
73 sizeof_phdrs = bfd_get_elf_phdr_upper_bound (prog);
74 if (sizeof_phdrs == 0)
75 {
76 fprintf (stderr, "Failed to get size of program headers\n");
77 return;
78 }
79 phdrs = malloc (sizeof_phdrs);
80 if (phdrs == NULL)
81 {
82 fprintf (stderr, "Failed allocate memory to hold program headers\n");
83 return;
84 }
85 num_headers = bfd_get_elf_phdrs (prog, phdrs);
86 if (num_headers < 1)
87 {
88 fprintf (stderr, "Failed to read program headers\n");
89 return;
90 }
91
92 for (i = 0; i < num_headers; i++)
93 {
94 Elf_Internal_Phdr * p = phdrs + i;
95 char *buf;
96 bfd_vma size;
97 bfd_vma base;
98 file_ptr offset;
99
100 size = p->p_filesz;
101 if (size <= 0)
102 continue;
103
104 base = p->p_paddr;
105 if (verbose > 1)
106 fprintf (stderr, "[load segment: lma=%08x vma=%08x size=%08x]\n",
107 (int) base, (int) p->p_vaddr, (int) size);
108
109 buf = malloc (size);
110 if (buf == NULL)
111 {
112 fprintf (stderr, "Failed to allocate buffer to hold program segment\n");
113 continue;
114 }
115
116 offset = p->p_offset;
117 if (prog->iovec->bseek (prog, offset, SEEK_SET) != 0)
118 {
119 fprintf (stderr, "Failed to seek to offset %lx\n", (long) offset);
120 continue;
121 }
122 if (prog->iovec->bread (prog, buf, size) != size)
123 {
124 fprintf (stderr, "Failed to read %lx bytes\n", size);
125 continue;
126 }
127
128 mem_put_blk (base, buf, size);
129 free (buf);
130 if (highest_addr_loaded < base + size - 1 && size >= 4)
131 highest_addr_loaded = base + size - 1;
132 }
133
134 free (phdrs);
135
136 regs.r_pc = prog->start_address;
137
138 if (strcmp (bfd_get_target (prog), "srec") == 0
139 || regs.r_pc == 0)
140 {
141 regs.r_pc = mem_get_si (0xfffffffc);
142 heaptop = heapbottom = 0;
143 }
144
145 if (verbose > 1)
146 fprintf (stderr, "[start pc=%08x %s]\n",
147 (unsigned int) regs.r_pc,
148 rx_big_endian ? "BE" : "LE");
149 }
This page took 0.055578 seconds and 5 git commands to generate.