source: trunk/IPs/systemC/Environment/Data/src/loadexec.c @ 120

Last change on this file since 120 was 117, checked in by rosiere, 16 years ago

1) Platforms : add new organization for test
2) Load_Store_Unit : add array to count nb_check in store_queue
3) Issue_queue and Core_Glue : rewrite the issue network
4) Special_Register_Unit : add reset value to register CID
5) Softwares : add multicontext test
6) Softwares : add SPECINT
7) Softwares : add MiBench?
7) Read_queue : inhib access for r0
8) Change Core_Glue (network) - dont yet support priority and load balancing scheme

  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1/*
2 * File       : loadexec.c
3 * Author     : Frédéric Pétrot
4 *
5 * Load an executable into an array using the GNU BFD
6 *
7 * $Log: loadexec.c,v $
8 * Revision 1.3  2006/06/08 14:32:41  nipo
9 * Use _loading_ memory address rather than _virtual_ memory address when
10 * loading objects.
11 *
12 * Patch from Alexandre Becoulet
13 *
14 * Revision 1.2  2006/02/21 10:37:40  buchmann
15 * Changes :
16 * - random optimizations
17 *   - now uses conditional expressions instead of if statements
18 *   - disables some useless assignments
19 *   - now uses shift operators instead of arithmetic operators
20 * - fix documentations
21 *
22 * Revision 1.1.1.1  2005/01/27 13:42:45  wahid
23 * First project import
24 * Wahid
25 *
26 * Revision 1.1  2003/03/10 10:38:05  fred
27 * Adding the bfd loader for good.
28 *
29 */
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <bfd.h>
35#include "../../Common/include/Debug.h"
36
37#ifndef TRUE
38#define TRUE true
39#endif
40
41#ifndef FALSE
42#define FALSE false
43#endif
44
45/* Loading the sections sections of file file into the array
46 * pointed to by emem.
47 * esize is the expected size, but it can be modified by the function if it too
48 * small for the sections
49 * All sizes and addresses are given in bytes
50 * eoffset is the address of the section with the lowest address among
51 * all required sections.
52 * Code should be self explanatory once you've read the 230 pages of
53 * the BFD documentation :) */
54
55typedef struct raminfo {
56  void *mem;
57  unsigned int  size;
58  unsigned int  ladr;
59  unsigned int  hadr;
60  const char *file;
61  const char **sections;
62} raminfo;
63
64static void bindsection(bfd *exec, asection *sect, PTR x)
65{
66   int i;
67   raminfo *rinfo = x; 
68
69   for (i = 0; rinfo->sections[i]; i++)
70      if (!strcmp(sect->name, rinfo->sections[i]))
71         break;
72   if (!rinfo->sections[i])
73      return;
74
75   if ((sect->flags & SEC_LOAD) && !(sect->flags & SEC_IN_MEMORY)) {
76      bfd_get_section_contents(exec, sect,
77                               rinfo->mem + (sect->lma - rinfo->ladr),
78                               0, bfd_section_size(exec, sect));
79      sect->contents = rinfo->mem;
80   }
81}
82
83static void sectionssize(bfd *exec, asection *sect, PTR x)
84{
85   int i;
86   raminfo *rinfo = x; 
87
88   for (i = 0; rinfo->sections[i]; i++)
89      if (!strcmp(sect->name, rinfo->sections[i]))
90         break;
91   if (!rinfo->sections[i])
92      return;
93
94   rinfo->size += bfd_section_size(exec, sect);
95   rinfo->ladr  = sect->lma < rinfo->ladr ? sect->lma : rinfo->ladr;
96   rinfo->hadr  = sect->lma + rinfo->size > rinfo->hadr ?
97                  sect->lma + rinfo->size : rinfo->hadr;
98}
99
100void loadexec(void **emem, int *esize, int *eoffset, const char *file, const char **sections)
101{
102  bfd      *exec;
103  static   int x;
104  int      i;
105  raminfo  ringo;
106
107   if (!x)
108      bfd_init();
109
110   exec = bfd_openr(file, NULL);
111
112   if (!exec) {
113      cerr("Cannot open File '%s'\n", file);
114      exit(1);
115   }
116
117   if (bfd_check_format(exec, bfd_object) != TRUE && !(exec->flags & EXEC_P)) {
118      cerr("File %s is not an executable file\n",
119            file); //bfd_get_filename(exec));
120      exit(1);
121   }
122
123#if 0
124   cout("Loading sections ");
125   for (i = 0; sections[i]; i++)
126     __cout("%s%s", sections[i], ((sections[i+1]!=NULL)?", ":" "));
127   __cout("from \"%s\"\n",bfd_get_filename(exec));
128   //cout("of executable '%s' for '%s' architecture in format '%s'\n",
129   //        bfd_get_filename(exec), bfd_printable_name(exec), exec->xvec->name);
130#endif
131
132   /* Set input parameters to bindsection */
133   ringo.file     = file;
134   ringo.sections = sections;
135   ringo.size     = 0;
136   ringo.ladr     = ~0;
137   ringo.hadr     = 0;
138   bfd_map_over_sections(exec, sectionssize, &ringo);
139   /* Get output parameters from sectionssize */
140   if (ringo.size < ringo.hadr - ringo.ladr)
141      ringo.size = ringo.hadr - ringo.ladr;
142   *esize         = ringo.size > *esize ? ringo.size : *esize;
143   *eoffset       = ringo.ladr;
144   ringo.mem      = malloc(*esize * sizeof(char));
145   /* Start over again from the start of the memory */
146   bfd_map_over_sections(exec, bindsection, &ringo);
147   /* Get output parameters from bindsection */
148
149   *emem          = ringo.mem;
150
151   if (bfd_close(exec) == FALSE) {
152      bfd_perror(exec->filename);
153      exit(1);
154   }
155}
Note: See TracBrowser for help on using the repository browser.