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