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