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