dc0f5da2c42d78f26861d5aaf5c4803abea9569c
[platform/upstream/gdb.git] / sim / rx / load.c
1 /* load.c --- loading object files into the RX simulator.
2
3 Copyright (C) 2005-2015 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 "load.h"
32 #include "elf/internal.h"
33 #include "elf/common.h"
34
35 /* Helper function for invoking a GDB-specified printf.  */
36 static void
37 xprintf (host_callback *callback, const char *fmt, ...)
38 {
39   va_list ap;
40
41   va_start (ap, fmt);
42
43   (*callback->vprintf_filtered) (callback, fmt, ap);
44
45   va_end (ap);
46 }
47
48 /* Given a file offset, look up the section name.  */
49 static const char *
50 find_section_name_by_offset (bfd *abfd, file_ptr filepos)
51 {
52   asection *s;
53
54   for (s = abfd->sections; s; s = s->next)
55     if (s->filepos == filepos)
56       return bfd_get_section_name (abfd, s);
57
58   return "(unknown)";
59 }
60
61 /* A note about endianness and swapping...
62
63    The RX chip is CISC-like in that the opcodes are variable length
64    and are read as a stream of bytes.  However, the chip itself shares
65    the code prefetch block with the data fetch block, so when it's
66    configured for big endian mode, the memory fetched for opcodes is
67    word-swapped.  To compensate for this, the ELF file has the code
68    sections pre-swapped.  Our BFD knows this, and for the convenience
69    of all the other tools, hides this swapping at a very low level.
70    I.e.  it swaps words on the way out and on the way back in.
71
72    Fortunately the iovector routines are unaffected by this, so we
73    can use them to read in the segments directly, without having
74    to worry about byte swapping anything.
75
76    However, our opcode decoder and disassemblers need to swap the data
77    after reading it from the chip memory, just like the chip does.
78    All in all, the code words are swapped four times between the
79    assembler and our decoder.
80
81    If the chip is running in little-endian mode, no swapping is done
82    anywhere.  Note also that the *operands* within opcodes are always
83    encoded in little-endian format.  */
84
85 void
86 rx_load (bfd *prog, host_callback *callback)
87 {
88   unsigned long highest_addr_loaded = 0;
89   Elf_Internal_Phdr * phdrs;
90   long sizeof_phdrs;
91   int num_headers;
92   int i;
93
94   rx_big_endian = bfd_big_endian (prog);
95
96   /* Note we load by ELF program header not by BFD sections.
97      This is because BFD sections get their information from
98      the ELF section structure, which only includes a VMA value
99      and not an LMA value.  */
100   sizeof_phdrs = bfd_get_elf_phdr_upper_bound (prog);
101   if (sizeof_phdrs == 0)
102     {
103       fprintf (stderr, "Failed to get size of program headers\n");
104       return;
105     }
106   phdrs = malloc (sizeof_phdrs);
107   if (phdrs == NULL)
108     {
109       fprintf (stderr, "Failed allocate memory to hold program headers\n");
110       return;
111     }
112   num_headers = bfd_get_elf_phdrs (prog, phdrs);
113   if (num_headers < 1)
114     {
115       fprintf (stderr, "Failed to read program headers\n");
116       return;
117     }
118   
119   for (i = 0; i < num_headers; i++)
120     {
121       Elf_Internal_Phdr * p = phdrs + i;
122       char *buf;
123       bfd_vma size;
124       bfd_vma base;
125       file_ptr offset;
126
127       size = p->p_filesz;
128       if (size <= 0)
129         continue;
130
131       base = p->p_paddr;
132       if (verbose > 1)
133         fprintf (stderr, "[load segment: lma=%08x vma=%08x size=%08x]\n",
134                  (int) base, (int) p->p_vaddr, (int) size);
135       if (callback)
136         xprintf (callback,
137                  "Loading section %s, size %#lx lma %08lx vma %08lx\n",
138                  find_section_name_by_offset (prog, p->p_offset),
139                  size, base, p->p_vaddr);
140
141       buf = malloc (size);
142       if (buf == NULL)
143         {
144           fprintf (stderr, "Failed to allocate buffer to hold program segment\n");
145           continue;
146         }
147       
148       offset = p->p_offset;
149       if (prog->iovec->bseek (prog, offset, SEEK_SET) != 0)
150         {
151           fprintf (stderr, "Failed to seek to offset %lx\n", (long) offset);
152           continue;
153         }
154       if (prog->iovec->bread (prog, buf, size) != size)
155         {
156           fprintf (stderr, "Failed to read %lx bytes\n", size);
157           continue;
158         }
159
160       mem_put_blk (base, buf, size);
161       free (buf);
162       if (highest_addr_loaded < base + size - 1 && size >= 4)
163         highest_addr_loaded = base + size - 1;
164     }
165
166   free (phdrs);
167
168   regs.r_pc = prog->start_address;
169
170   if (strcmp (bfd_get_target (prog), "srec") == 0
171       || regs.r_pc == 0)
172     {
173       regs.r_pc = mem_get_si (0xfffffffc);
174       heaptop = heapbottom = 0;
175     }
176
177   reset_decoder ();
178
179   if (verbose > 1)
180     fprintf (stderr, "[start pc=%08x %s]\n",
181              (unsigned int) regs.r_pc,
182              rx_big_endian ? "BE" : "LE");
183 }