[PATCH 22/57][Arm][GAS] Add support for MVE instructions: vmlaldav, vmlalv, vmlsldav...
[binutils-gdb.git] / sim / rx / load.c
blob65451ca8216a57055430d519e6535301581ef43e
1 /* load.c --- loading object files into the RX simulator.
3 Copyright (C) 2005-2019 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
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.
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.
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/>. */
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
27 #include "bfd.h"
28 #include "cpu.h"
29 #include "mem.h"
30 #include "load.h"
31 #include "elf/internal.h"
32 #include "elf/common.h"
34 /* Helper function for invoking a GDB-specified printf. */
35 static void
36 xprintf (host_callback *callback, const char *fmt, ...)
38 va_list ap;
40 va_start (ap, fmt);
42 (*callback->vprintf_filtered) (callback, fmt, ap);
44 va_end (ap);
47 /* Given a file offset, look up the section name. */
48 static const char *
49 find_section_name_by_offset (bfd *abfd, file_ptr filepos)
51 asection *s;
53 for (s = abfd->sections; s; s = s->next)
54 if (s->filepos == filepos)
55 return bfd_get_section_name (abfd, s);
57 return "(unknown)";
60 /* A note about endianness and swapping...
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.
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.
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.
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. */
84 void
85 rx_load (bfd *prog, host_callback *callback)
87 unsigned long highest_addr_loaded = 0;
88 Elf_Internal_Phdr * phdrs;
89 long sizeof_phdrs;
90 int num_headers;
91 int i;
93 rx_big_endian = bfd_big_endian (prog);
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)
102 fprintf (stderr, "Failed to get size of program headers\n");
103 return;
105 phdrs = malloc (sizeof_phdrs);
106 if (phdrs == NULL)
108 fprintf (stderr, "Failed allocate memory to hold program headers\n");
109 return;
111 num_headers = bfd_get_elf_phdrs (prog, phdrs);
112 if (num_headers < 1)
114 fprintf (stderr, "Failed to read program headers\n");
115 return;
118 for (i = 0; i < num_headers; i++)
120 Elf_Internal_Phdr * p = phdrs + i;
121 char *buf;
122 bfd_vma size;
123 bfd_vma base;
124 file_ptr offset;
126 size = p->p_filesz;
127 if (size <= 0)
128 continue;
130 base = p->p_paddr;
131 if (verbose > 1)
132 fprintf (stderr, "[load segment: lma=%08x vma=%08x size=%08x]\n",
133 (int) base, (int) p->p_vaddr, (int) size);
134 if (callback)
135 xprintf (callback,
136 "Loading section %s, size %#lx lma %08lx vma %08lx\n",
137 find_section_name_by_offset (prog, p->p_offset),
138 size, base, p->p_vaddr);
140 buf = malloc (size);
141 if (buf == NULL)
143 fprintf (stderr, "Failed to allocate buffer to hold program segment\n");
144 continue;
147 offset = p->p_offset;
148 if (bfd_seek (prog, offset, SEEK_SET) != 0)
150 fprintf (stderr, "Failed to seek to offset %lx\n", (long) offset);
151 continue;
153 if (bfd_bread (buf, size, prog) != size)
155 fprintf (stderr, "Failed to read %lx bytes\n", size);
156 continue;
159 mem_put_blk (base, buf, size);
160 free (buf);
161 if (highest_addr_loaded < base + size - 1 && size >= 4)
162 highest_addr_loaded = base + size - 1;
165 free (phdrs);
167 regs.r_pc = prog->start_address;
169 if (strcmp (bfd_get_target (prog), "srec") == 0
170 || regs.r_pc == 0)
172 regs.r_pc = mem_get_si (0xfffffffc);
173 heaptop = heapbottom = 0;
176 reset_decoder ();
178 if (verbose > 1)
179 fprintf (stderr, "[start pc=%08x %s]\n",
180 (unsigned int) regs.r_pc,
181 rx_big_endian ? "BE" : "LE");